0

I'm working with multiprocessing in Python and I run the following code:

import multiprocessing 
import os 
  
def worker1(): 
    print("ID of process running worker1: {}".format(os.getpid())) 
  
def worker2(): 
    print("ID of process running worker2: {}".format(os.getpid())) 
  
if __name__ == "__main__": 
    print("ID of main process: {}".format(os.getpid())) 
    p1 = multiprocessing.Process(target=worker1) 
    p2 = multiprocessing.Process(target=worker2) 
    p1.start() 
    p2.start() 
  
    print("ID of process p1: {}".format(p1.pid)) 
    print("ID of process p2: {}".format(p2.pid)) 
  
    p1.join() 
    p2.join() 
  
    print("Both processes finished execution!") 
  
    print("Process p1 is alive: {}".format(p1.is_alive())) 
    print("Process p2 is alive: {}".format(p2.is_alive())) 

But I got the following result:

ID of main process: 10956
ID of process p1: 14508
ID of process p2: 16000
Both processes finished execution!
Process p1 is alive: False
Process p2 is alive: False

Why didn't two functions worker1() and worker2() run?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Ehsan
  • 45
  • 6
  • In what environment are you running this? Do you use IDLE? – MisterMiyagi Jun 20 '20 at 16:01
  • What version of python are you using? I just ran this and got the correct output. I'm using version 3.7.4 – profPlum Jun 20 '20 at 16:01
  • @azro Because it should print theses sentences too: "ID of process running worker1: {}" and "ID of process running worker2: {}" – Ehsan Jun 20 '20 at 16:01
  • I got correct output too – azro Jun 20 '20 at 16:02
  • @MisterMiyagi I run it in Spyder 4 and python 3.7 – Ehsan Jun 20 '20 at 16:03
  • @profPlum I'm using python 3.7.6 – Ehsan Jun 20 '20 at 16:05
  • Are you running Windows? – tdelaney Jun 20 '20 at 16:07
  • @tdelaney Yes windows 10. – Ehsan Jun 20 '20 at 16:08
  • Hmm I installed your version of spyder & python but I still get correct output. This is on mac though... – profPlum Jun 20 '20 at 16:09
  • @profPlum - this is windows specific. Since the functions are in the spyder console, python can't reimport them when it spawns a new python for the subprocess. – tdelaney Jun 20 '20 at 16:11
  • `print()` is not a good way to test multiprocessing. Depending on the OS and how multiprocessing is used you might only see the prints of the main process. – Klaus D. Jun 20 '20 at 16:12
  • Does this answer your question? [No multiprocessing print outputs (Spyder)](https://stackoverflow.com/questions/48078722/no-multiprocessing-print-outputs-spyder) – MisterMiyagi Jun 20 '20 at 16:12
  • @profPlum I really don't know why it doesn't run on windows correctly!! Now I run it on Linux server and I got correct result!! – Ehsan Jun 20 '20 at 16:13
  • You'll find this question asked multiple times here on SO (I googled "python spyder multiprocessing") with lots of good solutions. Basically, in Windows, python needs to run a new version of python.exe and import your module. But it can't because its a spyder console thing. I think this is a dup of. for instance, https://stackoverflow.com/questions/48078722/no-multiprocessing-print-outputs-spyder – tdelaney Jun 20 '20 at 16:13

0 Answers0