multiprocessing-demo.py 483 B

1234567891011121314151617181920212223
  1. import concurrent.futures
  2. import time
  3. start = time.perf_counter()
  4. def do_something(seconds):
  5. print(f'Sleeping {seconds} second(s)...')
  6. time.sleep(seconds)
  7. return f'Done Sleeping...{seconds}'
  8. with concurrent.futures.ProcessPoolExecutor() as executor:
  9. secs = [5, 4, 3, 2, 1]
  10. results = executor.map(do_something, secs)
  11. # for result in results:
  12. # print(result)
  13. finish = time.perf_counter()
  14. print(f'Finished in {round(finish-start, 2)} second(s)')