cold10
cold10

Reputation: 140

Multiprocessing Processes killing instantly?

I am trying to use Python multiprocessing module. Here is some example code

from multiprocessing import Process 
 
def foo(): 
    print("Hello" )
 
def foo2(): 
    print("Hello again" )
 
if __name__ == '__main__':
    print('program started')
    global p1, p2
    p1 = Process(target=foo) 
    p2 = Process(target=foo2) 
    print(p1.is_alive())
    
    p1.start() 
    p2.start() 

    p1.join() 
    p2.join() 

As you can see, foo and foo2 are simple functions. I added the __name__ guard after people suggested it: Python multiprocessing processes instantly die after being started.

When I run this on my computer, I get the following output:

program started
False

The code seems to be working, so can somebody please explain?

I am using MacOS.

Upvotes: 3

Views: 290

Answers (2)

Roland Smith
Roland Smith

Reputation: 43495

Note that a process is alive from the moment its start method is called. So it is normal that is_alive returns False.

Now, if you run a program from an IDE, that might have unintended side effects. In case of problems, always run your script from the command-line first.

Upvotes: 2

cold10
cold10

Reputation: 140

I FIGURED IT OUT!!

Not sure why, (can somebody help me research this?), but it seems to be IDLE's fault:

I changed IDE's to VS Code, and it worked perfectly!

Upvotes: -1

Related Questions