0

Consider this working example, calling a function sequentially three times:

import time

start = time.perf_counter()

def do_something():
    print("Sleeping 1 second ...")
    time.sleep(1)
    print("Done Sleeping...\n")

do_something()
do_something()
do_something()

finish = time.perf_counter()

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

I get the expected output:

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

However, when I try to use the multiprocessing standard library to call it in parallel, I don't see any output. My attempt is:

import multiprocessing
import time

Nbre = multiprocessing.cpu_count()
print(f"Number of CPUs = {Nbre}")

start = time.perf_counter()

def do_something():
    print("Sleeping 1 second ...")
    time.sleep(1)
    print("Done Sleeping...\n")

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

p1.start()
p2.start()
p3.start()

p1.join()
p2.join()
p3.join()

finish = time.perf_counter()

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

and the result is:

    Number of CPUs = 8
    
    
    Finished in 0.12 second(s)

Why don't I see the print output from do_something? How can I fix the code?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • It seems to be working on Ubuntu. Just in case do `print('message',flush = True)`. Your print statement can be buffered, by putting [flash](https://docs.python.org/3/library/functions.html#print) it will tell not to buffer it and print right away – TaQ Aug 11 '22 at 00:27
  • I dont have an answer for you as to why its not working, but I can tell you based off the time it took to finished that the functions simply arent running – Christian Trujillo Aug 11 '22 at 00:27
  • It works like a charm. Is this a joke or something? – Elis Byberi Aug 11 '22 at 00:27
  • I don't know why it didn't work for me, I'm using IDLE – RACHID BEN ABDELMALEK Aug 11 '22 at 00:30
  • @ElisByberi instead of making less-than-nice comments about the code behaviour someone reports, please just vote or flag as not reproducible. There are any number of reasons why someone might have seen something appear not to work in the described manner - it just would depend on something that is outside of the code shown. – Karl Knechtel Aug 11 '22 at 00:31
  • "I'm using IDLE" - ah, that's exactly one of those "outside of the code shown" things then. I happen to know the exact issue. In the future, [please try](https://meta.stackoverflow.com/questions/261592) to research problems before asking. I found the linked duplicate, as the first result, by putting `idle multiprocessing` [into a search engine](https://duckduckgo.com/?q=idle+multiprocessing). – Karl Knechtel Aug 11 '22 at 00:32
  • IDLE could well be the problem, especially if you run it from a GUI where you wouldn't get a stdout to write to. – tdelaney Aug 11 '22 at 00:32
  • 1
    IMO: It should be a *standard* [debugging](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) step, for anyone running programs from any IDE - after creating an [mre] - to try the MRE from the console as well. It's the most reliable runtime environment, especially since you can be as explicit as you want, on the fly, about which Python installation to use. – Karl Knechtel Aug 11 '22 at 00:36
  • @KarlKnechtel He is using the IDLE! There was no other way to make him reveal the glory truth. Please, do not link to websites outside of the SO, they will go 404, I'm already tired fixing these dead links. – Elis Byberi Aug 11 '22 at 00:39
  • @ElisByberi I already closed the question as a duplicate of an existing question on SO. The debugging reference is one that is commonly handed out [and endorsed by the community](https://meta.stackoverflow.com/questions/313045/op-doesnt-know-how-to-debug), being written [by one of the biggest names on the site](https://stackoverflow.com/users/88656/eric-lippert). The search engine link is for demonstration purposes only. – Karl Knechtel Aug 11 '22 at 00:42

0 Answers0