0

I followed this youtube video by Corey Schafer to use the multiprocessing.

def test3(x):
    y=0;
    for ix in range (0,x):
        y=y+ix*2
        
    print(y,flush=True) #still didn't work
    time.sleep(1);
    
    return y;

However, the code does not print the result nor return the value.

import multiprocessing
cores = multiprocessing.cpu_count()
print(cores)

start =time.time()

p1=multiprocessing.Process(target=test3,args=(100000000,))
p2=multiprocessing.Process(target=test3,args=[100000000]) #the variations
p1.start()
p2.start()

p1.join()
p2.join()

finish =time.time()
print(f"Finished in {round(finish-start,2)} seconds()")

In the python multiprocessing it's not printing nor sleeping, and I tried several methods such as add more sleep() and use the flush() in the print but none of those were working.

Why the Processing was not working and how to fix it? and get a return value?

  • what is your output or error? when I run this I get `16 9999999900000000 9999999900000000 Finished in 4.54 seconds()` which I assume is the output you were looking for? – Nik Sep 04 '22 at 16:19
  • @Nik The program didn't seem to run at all finished in 0.08s. – ShoutOutAndCalculate Sep 04 '22 at 17:29
  • What is your environment? – Charchit Agarwal Sep 04 '22 at 18:51
  • @CharchitAgarwal It's anaconda jupyter 3.9.12. After reading a lot of posts, I found out there might be an issue with juputer and IPython https://stackoverflow.com/questions/641420/how-should-i-log-while-using-multiprocessing-in-python?rq=1 and https://stackoverflow.com/questions/50937362/multiprocessing-on-python-3-jupyter But the Pool method in the documentation https://docs.python.org/3.12/library/multiprocessing.html# also did not work, so it was rather confusing. Do we must run multiprocessing in a .py script? – ShoutOutAndCalculate Sep 04 '22 at 21:55

0 Answers0