Parallelize
This commit is contained in:
		
							parent
							
								
									cd6197caa3
								
							
						
					
					
						commit
						349edecc4e
					
				
							
								
								
									
										46
									
								
								runall.py
								
								
								
								
							
							
						
						
									
										46
									
								
								runall.py
								
								
								
								
							|  | @ -1,4 +1,5 @@ | ||||||
| from itertools import product | from itertools import product | ||||||
|  | from multiprocessing import current_process, Pool | ||||||
| import signal | import signal | ||||||
| from subprocess import DEVNULL, PIPE, Popen, TimeoutExpired | from subprocess import DEVNULL, PIPE, Popen, TimeoutExpired | ||||||
| 
 | 
 | ||||||
|  | @ -7,6 +8,14 @@ chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'( | ||||||
| # Prevent zombie processes | # Prevent zombie processes | ||||||
| signal.signal(signal.SIGCHLD, signal.SIG_IGN) | signal.signal(signal.SIGCHLD, signal.SIG_IGN) | ||||||
| 
 | 
 | ||||||
|  | # Current test case pairs used for testing (local to worker process) | ||||||
|  | current_pairs = None | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def set_pairs(pairs): | ||||||
|  |     global current_pairs | ||||||
|  |     current_pairs = pairs | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| def check_pair(script, instr, outstr): | def check_pair(script, instr, outstr): | ||||||
|     process = Popen( |     process = Popen( | ||||||
|  | @ -29,29 +38,38 @@ def check_pair(script, instr, outstr): | ||||||
|         return False |         return False | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def check_pairs(script, pairs): | def check_script(script): | ||||||
|     for pair in pairs: |     for pair in current_pairs: | ||||||
|         if not check_pair(script, *pair): |         if not check_pair(script, *pair): | ||||||
|             return False |             return script, False | ||||||
|     return True |     return script, True | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def generate_scripts(max_length): | ||||||
|  |     for length in range(max_length + 1): | ||||||
|  |         for letters in product(chars, repeat=length): | ||||||
|  |             yield "".join(letters) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def find_script(pairs, max_length): | def find_script(pairs, max_length): | ||||||
|     candidates = [] |     candidates = [] | ||||||
|  | 
 | ||||||
|     chars_count = len(chars) |     chars_count = len(chars) | ||||||
|     total_options = int((chars_count ** (max_length + 1) - 1) / (chars_count - 1)) |     num_tasks = int((chars_count ** (max_length + 1) - 1) / (chars_count - 1)) | ||||||
|     current_option = 0 |     done_tasks = 0 | ||||||
| 
 | 
 | ||||||
|     for length in range(max_length + 1): |     with Pool(processes=8, initializer=set_pairs, initargs=(pairs,)) as pool: | ||||||
|         for letters in product(chars, repeat=length): |         for script, result in pool.imap_unordered( | ||||||
|             current_option += 1 |             check_script, | ||||||
|  |             generate_scripts(max_length), | ||||||
|  |             chunksize=10, | ||||||
|  |         ): | ||||||
|  |             done_tasks += 1 | ||||||
| 
 | 
 | ||||||
|             if current_option % 1000 == 0: |             if done_tasks % 10000 == 0: | ||||||
|                 print(f"Progress: {current_option}/{total_options}") |                 print(f"Progress: {done_tasks}/{num_tasks}") | ||||||
| 
 | 
 | ||||||
|             script = "".join(letters) |             if result: | ||||||
| 
 |  | ||||||
|             if check_pairs(script, pairs): |  | ||||||
|                 print("> Found candidate:", script) |                 print("> Found candidate:", script) | ||||||
|                 candidates.append(script) |                 candidates.append(script) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue