Search code examples
pythonmultiprocessingpython-multiprocessingpython-multithreading

Multiprocessing in python doesnt print any statements


Multithreading is printing the output but not multiprocessing. Searched stack overflow and answered questions didnt solve the problem.

Multiprocessing is not working.

from threading import Thread
import datetime
from multiprocessing import Process
import sys


import time

def func1():

    print('Working')
    time.sleep(5)
    global a
    a=10
    print(datetime.datetime.now())


def func2():
    print("Working")
    time.sleep(10)
    print(datetime.datetime.now())
    

p1 = Process(target=func1)
p1.start()
p2 = Process(target=func2)
p2.start()
p1.join()
p2.join()


print(a)

Even the print(a) is not printing the value. It says

NameError: name 'a' is not defined

Solution

  • As I commented, plain variables, be they global or not, won't magically travel between multiprocessing Processes. (Well, actually, that's a bit of a simplification and depends on the OS and multiprocessing spawner you're using, but I digress.)

    The simplest communication channel is a multiprocessing.Queue (that actually "magically" works between processes).

    As discussed in further comments,

    • you can't use multiprocessing in an IDE that doesn't save your script before executing it, since it requires being able to spawn a copy of the script, and if there's no script on disk, there's nothing to spawn.
    • on a similar note, you can't use multiprocessing very well from Jupyter notebooks, since they're not run as regular Python scripts, but via the Python kernel process Jupyter starts.

    Here's a simple adaptation of your code to actually pass data between the processes.

    Remember to guard your multiprocessing main() with if __name__ == "__main__".

    import datetime
    import time
    import multiprocessing
    
    
    def func1(q: multiprocessing.Queue):
        print("func1 thinking...")
        time.sleep(2)
        q.put(("func1", 10))
        print("func1 quit at", datetime.datetime.now())
    
    
    def func2(q: multiprocessing.Queue):
        for x in range(10):
            print("func2 working", x)
            q.put(("func2", x))
            time.sleep(0.3)
    
    
    def main():
        queue = multiprocessing.Queue()
        p1 = multiprocessing.Process(target=func1, args=(queue,))
        p2 = multiprocessing.Process(target=func2, args=(queue,))
        p1.start()
        p2.start()
        p1.join()
        p2.join()
        print("Subprocesses ended, reading their results...")
        while not queue.empty():
            print(queue.get())
    
    
    if __name__ == "__main__":
        main()
    

    The output is:

    func1 thinking...
    func2 working 0
    func2 working 1
    func2 working 2
    func2 working 3
    func2 working 4
    func2 working 5
    func2 working 6
    func1 quit at 2021-06-16 17:58:46.542275
    func2 working 7
    func2 working 8
    func2 working 9
    2021-06-16 17:58:47.577008
    Subprocesses ended, reading their results...
    ('func2', 0)
    ('func2', 1)
    ('func2', 2)
    ('func2', 3)
    ('func2', 4)
    ('func2', 5)
    ('func2', 6)
    ('func1', 10)
    ('func2', 7)
    ('func2', 8)
    ('func2', 9)