Python Forum

Full Version: Process doesn't work but Thread work !
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am just learning to work with multi threads/process.

I wrote this code and it works well:
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]

with concurrent.futures.ThreadPoolExecutor(max_workers = numThreads) as pool:
    pool.map(do_something, sec,timeout = 1)
when I switch from Thread to Process. Nothing is printed on the screen.
I think the code is not working at all.
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]

with concurrent.futures.ProcessPoolExecutor(max_workers = numThreads) as pool:
    pool.map(do_something, sec,timeout = 1)
any reason for this?
the second snippet works for me. I get

Output:
start sleeping 5 start sleeping 4 start sleeping 3 end sleeping 3 start sleeping 2 end sleeping 4 start sleeping 1 end sleeping 1 end sleeping 5 end sleeping 2
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:
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()
(Oct-18-2021, 05:54 PM)buran Wrote: [ -> ]the second snippet works for me. I get

Output:
start sleeping 5 start sleeping 4 start sleeping 3 end sleeping 3 start sleeping 2 end sleeping 4 start sleeping 1 end sleeping 1 end sleeping 5 end sleeping 2

thanks for the confirmation.
this is strange. I am using Python 3.8. I also use it on Mac_OS
I am not sure if my installed Python has a problem.
I got it.
the code doesn't work on IDLE.
I ran it from command line and it works !