So I've started trying my hand at Python's multiprocessing library. My goal was to speed up a slow function that compared a string against a large database of other strings and returned the most similar match. To do so, I attempted to write a function that split the task among different Process objects and set them running, using a shared variable to capture the results:
cores = cpu_count() # Number of cores in this computer, i.e. 4 sublistList = chunks(tasks,cores) # Split tasks into subprocessing arrays of evenly-sized chunks, the number of chunks equal to how many cores we have to process them # Create a multiprocessing function, since this is a large function that will take time and splitting it across cores will ease the load if __name__ == '__main__': freeze_support() # Make sure multiple applications don't spawn, this is necessary for Windows jobs = [] # Array of processes manager = Manager() # Create a manager returns = manager.list() # Shared list variable we use to get return results for i in range(0,cores): # For number of cores... p = Process(target=workerFunction,args=(w,sublistList[i],returns)) jobs.append(p) # Add to array of processes to run p.start() for p in jobs: p.join() However, when I run this code, it creates a new application window and then hangs indefinitely, which is completely bizarre behavior and not at all what I want. What could be causing this in my code? Is my worker function silently crashing and not alerting me? I have looked at a variety of other answers but none of the suggested answers seemed to fix this issue.
(If it's relevant to the question, I am an entry-level software engineer with a few years of programming experience in other languages, but am relatively new to Python. This is for a small indie game side project of mine.)
joinmethod waits for the subprocess to finish one way or another. It seems your worker is running infinitely.multiprocessmust never pending on any functionality that does not time out. On the other hand, you should also usetry: except:to capture error. Depends on requirements, you might need to use asynchronous processing.