0

I am watching the tutorial https://www.youtube.com/watch?v=fKl2JW_qrso (shown at 8.18 min) about multiprocessing. I am running the code copied from there:

import multiprocessing
import time
import os

start = time.perf_counter()


def do_something():

    print("sleeping 1 sec...")
    time.sleep(1)

    print("done sleeping...")


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

p1.start()
p2.start()

finish = time.perf_counter()

print("finished in " + str(round((finish-start),2))   + " seconds(s)")

os.system("pause") 

But when I run it on my computer I have the following error message (but the same code works on the tutorial):

An attempt has been made to start a new process before the current process has finished its bootstrapping phase. This probably means that you are not using fork to start your child processes and you have forgotten to use the proper idiom in the main module: if name == 'main': freeze_support() ... The "freeze_support()" line can be omitted if the program is not going to be frozen to produce an executable.

blu potatos
  • 175
  • 9

1 Answers1

0

Thanks for all your comments. Finally it works. So basically in windows it needs to be written in a slightly different way:

import multiprocessing
import time
import os

start = time.perf_counter()


def do_something():

    print("sleeping 1 sec...")
    time.sleep(1)

    print("done sleeping...")

if  __name__ == "__main__":
    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("finished in " + str(round((finish-start),2))   + " seconds(s)")

    os.system("pause")
Pushparaj
  • 415
  • 8
  • 21
blu potatos
  • 175
  • 9