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
  print doesnt work in a function ony 2 233 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  Need some help trying to get my Python mortgage amortization calculator to work prope IamSirAskAlot 4 15,673 Feb-12-2024, 10:53 PM
Last Post: BerniceBerger
  Multiprocessing: Threads work well. Processes don't work. viyubu 11 1,666 Dec-03-2023, 08:50 PM
Last Post: snippsat
  Pydoc documentation doesnt work Cosmosso 5 4,263 Nov-25-2023, 11:17 PM
Last Post: vidito
  hi need help to make this code work correctly atulkul1985 5 702 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  How to copy work sheet data one workbook to other? sayyedkamran 2 645 Nov-03-2023, 09:10 AM
Last Post: Larz60+
  newbie question - can't make code work tronic72 2 626 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Why wont this path work one way, but will the other way? cubangt 2 623 Sep-01-2023, 04:14 PM
Last Post: cubangt
  PyOpenGL is not work PoseidonLin 5 1,177 Jul-12-2023, 02:51 PM
Last Post: deanhystad
  pip install requests doesnt work misodca 8 5,572 Jul-07-2023, 08:04 AM
Last Post: zyple

Forum Jump:

User Panel Messages

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