Python Forum
Process doesn't work but Thread work !
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Process doesn't work but Thread work !
#1
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?
Reply
#2
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
mr_byte31 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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()
Reply
#4
(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.
Reply
#5
I got it.
the code doesn't work on IDLE.
I ran it from command line and it works !
buran likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why does datetime.strptime() not work in this situation Monomial 2 1,003 Apr-08-2025, 06:36 PM
Last Post: snippsat
  PIP doesn't work YKR 1 795 Mar-28-2025, 02:10 PM
Last Post: snippsat
  Excel password recovery tool for work OTH 6 5,066 Mar-06-2025, 03:49 PM
Last Post: Pedroski55
  I'm trying to install python 3.11.11 on windows 10 - it doesn't work Petonique 2 2,524 Feb-04-2025, 05:42 PM
Last Post: snippsat
  Generators don't work svalencic 7 1,201 Jan-16-2025, 04:16 PM
Last Post: DeaD_EyE
  Trying to Make Steganography Program Work For All Payload Types Stegosaurus 0 1,330 Sep-26-2024, 12:43 PM
Last Post: Stegosaurus
  Can't get graph code to work properly. KDDDC2DS 1 734 Sep-16-2024, 09:17 PM
Last Post: deanhystad
  Questions about while loop - why does one work, and the other variant does not Swgeek 2 989 Aug-29-2024, 07:57 AM
Last Post: Swgeek
  How to make my Telegram bot stop working at 16:15 and not work on Fridays? hus73 2 1,534 Aug-10-2024, 12:06 PM
Last Post: hus73
  Executable file compiled by PyInstaller does not work on Windows 7 amusaber 1 2,145 Jul-11-2024, 02:59 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020