threading-demo.py 667 B

123456789101112131415161718192021222324252627282930313233
  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.ThreadPoolExecutor() 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. # threads = []
  14. # for _ in range(10):
  15. # t = threading.Thread(target=do_something, args=[1.5])
  16. # t.start()
  17. # threads.append(t)
  18. # for thread in threads:
  19. # thread.join()
  20. finish = time.perf_counter()
  21. print(f'Finished in {round(finish-start, 2)} second(s)')