Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. from concurrent.futures import ThreadPoolExecutor, as_completed
  2. from time import sleep
  3.  
  4. #wait for 5 seconds
  5. def return_after_5_secs(message):
  6. sleep(5)
  7. return message
  8.  
  9. #wait for 7 seconds
  10. def return_after_7_secs(message):
  11. sleep(8)
  12. return message
  13.  
  14. pool = ThreadPoolExecutor(3)
  15. future_list = list()
  16. #submitting the 5 seconds wait method
  17. future_list.append(pool.submit(return_after_5_secs, ("hello5")))
  18. #submitting the 7 seconds wait method
  19. future_list.append(pool.submit(return_after_7_secs, ("hello3")))
  20. #check immediately if any of the process completes
  21. print(future_list[0].done())
  22. print(future_list[1].done())
  23. # no one completes
  24. print("no one completes.. waiting for 6 sec..")
  25. #wait for 6 seconds
  26. sleep(6)
  27. print("waiting completes for 6 secs..")
  28. print(future_list[0].done())
  29. print(future_list[1].done())
  30. print("Result: " + future_list[0].result())
  31. # first job has completed
  32. print("again gonna wait for 3 secs..")
  33. sleep(3)
  34. print("wait completes for 3 secs..")
  35. #second job will also complete now
  36. print(future_list[0].done())
  37. print(future_list[1].done())
  38. print("Result: " + future_list[0].result()+" "+future_list[1].result())
  39. #shutting down the pool executor
  40. pool.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement