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 !
#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


Messages In This Thread
RE: Process doesn't work but Thread work ! - by deanhystad - Oct-18-2021, 06:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  equalto validator doesnt work robertkwild 1 251 Jun-02-2024, 06:16 AM
Last Post: Pedroski55
  I can't for the life of me get this basic If statement code to work CandleType1a 8 554 May-21-2024, 03:58 PM
Last Post: CandleType1a
  Extending list doesn't work as expected mmhmjanssen 2 384 May-09-2024, 05:39 PM
Last Post: Pedroski55
  Using xml.parsers.expat I can't get parser.ParseFile(f) to work Pedroski55 3 456 Apr-24-2024, 07:36 AM
Last Post: Pedroski55
  Making the tab key work like a tab key jackg 3 422 Apr-16-2024, 09:02 PM
Last Post: deanhystad
  print doesnt work in a function ony 2 473 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,976 Feb-12-2024, 10:53 PM
Last Post: BerniceBerger
  Multiprocessing: Threads work well. Processes don't work. viyubu 11 2,225 Dec-03-2023, 08:50 PM
Last Post: snippsat
  Pydoc documentation doesnt work Cosmosso 5 4,658 Nov-25-2023, 11:17 PM
Last Post: vidito
  hi need help to make this code work correctly atulkul1985 5 1,045 Nov-20-2023, 04:38 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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