VIN VIN - 1 month ago 21
Python Question

process not starting in python

I have created two processes but they are not starting according to this code.
any idea what is the problem?

import serial
from multiprocessing import Process

ser=serial.Serial('COM8',115200)

c=" "
out=" "

def pi():
print ("started")
out=" "
while 1:
# loop contents

def man():

while(1):
# loop contents

p1=Process(target=pi,args=())
p2=Process(target=man,args=())

p1.start()
p2.start()
p1.join()
p2.join()

Answer

I'll guess you're using windows...

Put your initalisation code in an if __name__ == '__main__': block:

import serial
from multiprocessing import Process

ser=serial.Serial('COM8',115200)

c=" "
out=" "

def pi():
    print ("started")
    out=" "
    while 1:
    #  loop contents

def man():

    while(1):
        # loop contents

if __name__ == '__main__':

    p1=Process(target=pi,args=())
    p2=Process(target=man,args=())

    p1.start() 
    p2.start()
    p1.join()
    p2.join()

On windows, to work around the lack of fork() each newly started subprocess has to import the __main__ module, so you'll run into an endless loop of spawning processes unless if you don't protect your initialistion code.