Oct-18-2021, 06:14 PM
(This post was last modified: Oct-18-2021, 06:14 PM by deanhystad.)
Your second snippet crashes when I run it. Surprised it didn't crash for you.
Your code will work on linux but not windows. Windows doesn't have a fork, so processes work differently. To generate context in your new process, windows Python runs your Python program in the new process. This might require you to protect against creating some resources in the spawned process. This is the case in your example. Windows is spawning processes which in turn span processes which in turn span processes....
To fix the problem you just hide your main code behind "if __name__ == '__main__' like this:
Your code will work on linux but not windows. Windows doesn't have a fork, so processes work differently. To generate context in your new process, windows Python runs your Python program in the new process. This might require you to protect against creating some resources in the spawned process. This is the case in your example. Windows is spawning processes which in turn span processes which in turn span processes....
To fix the problem you just hide your main code behind "if __name__ == '__main__' like this:
import time import concurrent.futures numThreads = 3 def do_something(sec): print('start sleeping ',sec) time.sleep(sec) print('end sleeping ', sec) sec = [5,4,3,2,1] def main(): with concurrent.futures.ProcessPoolExecutor(max_workers = numThreads) as pool: pool.map(do_something, sec, timeout = 10) if __name__ == '__main__': main()