I'm creating two instances of a process over here but when I'm running this program I'm getting only main function output.
import multiprocessing
import time
def sleepy_man():
print("Starting to Sleep")
time.sleep(1)
print("Done Sleeping")
tic = time.time()
p1 = multiprocessing.Process(target=sleepy_man)
p2 = multiprocessing.Process(target=sleepy_man)
p1.start()
p2.start()
toc = time.time()
print("Done in {:.4f} seconds".format(toc-tic))
Output
Done in 0.0554 seconds
I was doing it for practice from this blog only. Source: https://www.analyticsvidhya.com/blog/2021/04/a-beginners-guide-to-multi-processing-in-python/
It is worth noting you would see the same behavior if you had somehow set p1.daemon = p2.daemon = True
.
It is also possibly due to output buffering, rather than logic errors. Two questions:
sys.stdout.flush()
or flush=True
to your print
, do you see different behavior?time python foobar.py
does it take .02s or 1s to run?Obviously, continuing your tutorial and correctly adding .join()
below will resolve the issue in a way that would be expected for normal usage.