It is important to understand exactly why your program isn't working. It has to do with the multiprocessing.Pool context manager, which you start with this line of code:
with multiprocessing.Pool(10) as pool:
# more code...
When your program exits from this block, the context manager calls the function multiprocessing.Pool.terminate
, which is documented as follows:
"Stops the worker processes immediately without completing outstanding work. When the pool object is garbage collected terminate() will be called immediately."
https://docs.python.org/3/library/multiprocessing.html#module-multiprocessing.pool
This is not what you want. You want to wait until the workers are done. To do that, place a call to pool.close() and another call to pool.join() inside your with block, like this:
import multiprocessing
def foo(x):
print(x)
if __name__ == '__main__':
multiprocessing.freeze_support()
with multiprocessing.Pool(10) as pool:
res = pool.apply_async(foo, args=("Hello",))
pool.close()
pool.join()
You must call pool.close() before calling pool.terminate(), as the docs indicate.
The worker Processes will start when you call apply_async, but starting up a new Process requires some work from the operating system. That takes a little bit of time. If you call Pool.terminate()
before the Processes have time to get going, you will kill the workers before they have a chance to do anything. That's the problem with your script.