ivapopova

multi_vs_single_with_sleep.py

May 3rd, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. import threading
  2. import time
  3.  
  4. def worker(x):
  5. tid = threading.currentThread().name
  6.  
  7. # do some hard and time consuming work:
  8. time.sleep(1)
  9. print("Worker {} is working with {}".format(tid, x))
  10.  
  11.  
  12. #################################################
  13. # Sequential Processing:
  14. #################################################
  15. t = time.time()
  16. worker(42)
  17. worker(84)
  18. print("Sequential Processing took:",time.time() - t,"\n")
  19.  
  20. #################################################
  21. # Multithreaded Processing:
  22. #################################################
  23. tmulti = time.time()
  24. tr1 = threading.Thread(target=worker, args=(42,))
  25. tr2 = threading.Thread(target=worker, args=(82,))
  26.  
  27. tr1.start();tr2.start()
  28. tr1.join(); tr2.join()
  29. print("Multithreaded Processing took:",time.time() - tmulti)
Advertisement
Add Comment
Please, Sign In to add comment