0

I am currently learning how to use multiprocessing from Corey Schafer's YouTube video: https://www.youtube.com/watch?v=fKl2JW_qrso&ab_channel=CoreySchafer

I am at the 10-minute mark on the video and I had copied my codes exactly like what was shown in the video but the results I have gotten are frustratingly different.

import time
import multiprocessing

def do_something():
    print('Sleeping 1 second....')
    time.sleep(1)
    print('Done Sleeping...')

start = time.perf_counter()


p1 = multiprocessing.Process(target=do_something)
p2 = multiprocessing.Process(target=do_something)     

p1.start()
p2.start()

p1.join()
p2.join()



finish = time.perf_counter()


print(f'Finished in {round(finish-start, 2)} second(s)')

The result should've been:

Sleeping 1 second...
Sleeping 1 second...
Done Sleeping...
Done Sleeping...
Finished in 1.01 second(s)

But what I've gotten instead is:

Finished in 0.48 second(s)

It did not even print the 'Sleeping 1 second...' and 'Done Sleeping...' thing. So I'm guessing the function in the multiprocessing thing didn't even run?? Does anyone know the reason why my python code is behaving this way? Any help is much appreciated. Thanks

1 Answer 1

2

that's because it crashed ... because on windows you should put all "command" code inside an if __name__ == "__main__": block as follows, function definitions and imports should be before this line.

import time
import multiprocessing

def do_something():
    print('Sleeping 1 second....')
    time.sleep(1)
    print('Done Sleeping...')

if __name__ == "__main__":
    start = time.perf_counter()


    p1 = multiprocessing.Process(target=do_something)
    p2 = multiprocessing.Process(target=do_something)
    
    p1.start()
    p2.start()

    p1.join()
    p2.join()



    finish = time.perf_counter()


    print(f'Finished in {round(finish-start, 2)} second(s)')

Edit: when working in IDLE it is printing the output correctly ... just not to the correct screen, you should use VSCode or Pycharm instead ... or maybe run the code from terminal or any other IDE.

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

I tried it. This time, the result was "Finished in 1.57 second(s)..." instead of finishing in a split second like before. Still didn't print the "Sleeping 1 second..." and "Done Sleeping..." though. I made it run 3 seconds instead and the result became "Finished in 3.57 second(s)...". So it looks like the sleep function is working but it still won't print. How does one solve this? And also what's with the extra 0.57 seconds added to the time? Thanks!
@PoopinMahBusiness the time part is normal, the function will take at least 1 second, 1.57 is more than 1 second, as for the print part, i think this is because of your python version and operating system (and processor), so i cannot answer why without knowing them, but you should add flush=True to your print and try again as it could just be buffering
Unfortunately, the flush=True didn't change a thing :(. I just tried the code on my old laptop using an old CPU and running a different version of python (python 3.9 64 bit instead of version 3.11 64 bit) and the results were the same. Thank you for your help though!
@PoopinMahBusiness are you using a certain IDE or running from cmd ? It may be a problem with the IDE, standard output handling is hard to track, it may be printing to a different stdout than the one you expect the output on.
I was just running it on a normal Python IDLE. Not running from cmd or using visual studio or anything. Should I switch to something else?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.