0

I am studying about multi processing. When I run the below code, I wondered that starting process means process start immediately .

My code as follows:

import time
import multiprocessing

def do_something():
    print('Sleeping 1 seconds...')
    time.sleep(1)
    print('Completed...')

if __name__ == '__main__':

    start = time.perf_counter()

    processes = []
    for _ in range(4):
        p = multiprocessing.Process(target=do_something)
        p.start()
        processes.append(p)

    print('Hello~')

    for process in processes:
        process.join()

    finish = time.perf_counter()

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

As far as I know, 'start' method do start process. So, I expected that 'Hellow~' would be printed after starting do_something function as follows.

enter image description here


But actual result as follows:

enter image description here

As you can see, 'Hello~' is printed even before starting process!! Why does a such result occur?

Lover Math
  • 33
  • 7
  • 1
    Maybe it would help to clarify: https://stackoverflow.com/a/15086113/7571412 – ruslan_krivoshein May 16 '21 at 11:40
  • 1
    You can't tell anything about the order of processing of processes from looking at that output. `p.start()` does indeed start the new process. However, output from that process is buffered before being shown to you on the terminal. – rdas May 16 '21 at 11:40
  • 1
    Does this answer your question? [What is the use of join() in Python threading?](https://stackoverflow.com/questions/15085348/what-is-the-use-of-join-in-python-threading) – ruslan_krivoshein May 16 '21 at 11:43
  • @rdas Thank you for commenting, you mean while process is buffered, 'Hello~' is printed first? – Lover Math May 16 '21 at 11:43
  • @ruslan_krivoshein Maybe not~. Anyway thanks for your help~ – Lover Math May 16 '21 at 11:45

0 Answers0