![]() |
Process doesn't work but Thread work ! - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Process doesn't work but Thread work ! (/thread-35304.html) |
Process doesn't work but Thread work ! - mr_byte31 - Oct-18-2021 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? RE: Process doesn't work but Thread work ! - buran - Oct-18-2021 the second snippet works for me. I get
RE: Process doesn't work but Thread work ! - deanhystad - Oct-18-2021 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() RE: Process doesn't work but Thread work ! - mr_byte31 - Oct-18-2021 (Oct-18-2021, 05:54 PM)buran Wrote: the second snippet works for me. I get 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. RE: Process doesn't work but Thread work ! - mr_byte31 - Oct-18-2021 I got it. the code doesn't work on IDLE. I ran it from command line and it works ! |