0

I tried running a simple multiprocessing script but it doesn't seem to work

import multiprocessing
import time

start = time.perf_counter()

def run():
    print('sleeping for 1 second')
    time.sleep(1)
    print('done sleeping')

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

p1.start()
p2.start()
p1.join()
p2.join()

finish = time.perf_counter()

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

The error message says

AttributeError: Can't get attribute 'run' on <module '__main__' (built-in)>

(more than that)

KAOTU
  • 31
  • 7
  • 1
    Are you sure this is all the code you have? I ran it myself, and it seems to be working. – Agent Biscutt Oct 23 '21 at 08:59
  • whats your command to run this ? – milad_vayani Oct 23 '21 at 10:35
  • Possibly either [this question](https://stackoverflow.com/questions/41385708/multiprocessing-example-giving-attributeerror) or [this question](https://stackoverflow.com/questions/48593694/python-multiprocessing-returning-attributeerror-when-following-documentation-cod) might help. – sj95126 Oct 23 '21 at 13:39
  • Yea this is all my code – KAOTU Oct 24 '21 at 11:04
  • I tried running it in an ide and python, still doesn't work – KAOTU Oct 24 '21 at 11:05
  • As @sj95126 suggested, put function `run` in some other file such as *worker.py* and then `from worker import run`. – Booboo Oct 30 '21 at 10:41

0 Answers0