Infinite loops in multiprocessing python

Viewed 18

How do I get the notworking() to work in mulitprossesing. My console is only logging whyisthis(). I am new to multiprosessing, and I am just not getting this, so I hope someone will give an easy solution.

from multiprocessing import Process
def whyisthis():
    while True:
        print(f'Why is this')
        time.sleep(5)


def notworking():
    while True:
        print(f'not working')


p1 = Process(target=whyisthis(), daemon=False, 
name='Why is this')
p2 = Process(target=notworking(), daemon=False, 
name='Not working')

if __name__ == '__main__':
    p1.start()
    p2.start()
    p1.join()
    p2.join()
1 Answers

The problem with your code lies here:

p1 = Process(target=whyisthis(), daemon=False, name="Why is this")

the target should be a funcion but in your code you execute the function here with brackets. That is like normal function calling so that's why your program just stuck on that line (or more precisely - execute whyisthis which never ends).

Correct way should be

p1 = Process(target=whyisthis, daemon=False, name="Why is this")
Related