0

I am trying to understand the multiprocessing in Python and trying to replicate the results of the below code(taken from Corey Schafer youtube video). The code is running but the multiprocessing module is not working. I am using windows 10 and python version 3.8.5 and my laptop has 8 cores.

import time
import multiprocessing
import workers4
tic=time.time()

processes=[]
if __name__ == '__main__':
    for _ in range(50):
        p=multiprocessing.Process(target=workers4.do_something,args=[20])
        p.start()
        processes.append(p)
    
    for process in processes:
        process.join()

toc=time.time()
elapsed=toc-tic
print("\nFinished in ",elapsed," seconds")

The file workers4.py has the function 'do_something' which makes the code sleep for some specific time value as passed through the argument.

def do_something(seconds):
    print(f "Sleeping {seconds}second(s)...")
    time.sleep(seconds)
    print("Done sleeping...")

On running the above code, it shows only
Finished in 0.8727371692657471 seconds

and the below statements in do_something function in the workers.py file are not printed at all not even once.

print(f "Sleeping {seconds}second(s)..."), 
print("Done sleeping...") 

It seems that the do_something function is not getting executed as with a sleep time of 20sec for every process , how can the code execute in 0.8727 secs. Also there are 50 processes in the above code as in the for loop. Can someone help what is the issue and how to rectify it ??

Jakub Szlaur
  • 1,852
  • 10
  • 39
Simrandeep Bahal
  • 109
  • 1
  • 2
  • 8

2 Answers2

0

So I ran this with a few changes and this is running fine. The changes are that I do not have a worker4.py file, changed the waiting time to 1 second. The most significant change was to change the print(f "Sleeping {seconds}second(s)...") line to print(f"Sleeping {seconds}second(s)...") as I got a syntax error due to the space between the f and the first quote.

import time
import multiprocessing
tic=time.time()

processes=[]

def do_something(seconds):
    print(f"Sleeping {seconds}second(s)...")
    time.sleep(seconds)
    print("Done sleeping...")

if __name__ == '__main__':
    for _ in range(50):
        p=multiprocessing.Process(target=do_something,args=[1])
        p.start()
        processes.append(p)
    
    for process in processes:
        process.join()

toc=time.time()
elapsed=toc-tic
print("\nFinished in ",elapsed," seconds")

Got the following output

Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Sleeping 1second(s)...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...

Finished in  1.7538056373596191  seconds
Quizzarex
  • 167
  • 2
  • 12
0

I tried to execute your script with both command line and using the IDE (specifically Spyder) and I can observe the following differences in behavior.

When I execute the script using command line using python3 mp.py which has your code, I can see the output as expected:

Finished in  0.0  seconds
Sleeping 2 second(s)...

Finished in  0.0  seconds
Sleeping 2 second(s)...

Finished in  0.0  seconds
Sleeping 2 second(s)...

Finished in  0.0  seconds
Sleeping 2 second(s)...

Finished in  0.0  seconds
Sleeping 2 second(s)...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...
Done sleeping...

Finished in  3.7699832916259766  seconds

When I execute the same script using the Run button in Spyder IDE, I see only the following output:

Finished in  2.8714535236358643  seconds

I tried to look for the difference in behavior and found several threads that describe this behavior.

no-multiprocessing-print-outputs-spyder

There is a specific comments regarding Windows as well:

That printing with multiprocessing on Windows doesn't work as expected

Also, some IDEs require additional configuration as described for IDLE

Based on the above observations, I would suggest to try to run the script via command line to verify that the script itself work as expected and try to see if there are any additional configuration needed in the IDE to make it work.

Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35