Guest User

Untitled

a guest
Dec 19th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. from __future__ import print_function
  3. import gc
  4. import time
  5. import threading
  6. from more_executors import Executors
  7.  
  8.  
  9. def dump_threads():
  10. print("------------- threads ----------------")
  11. for thread in threading.enumerate():
  12. print(" {name} daemon={daemon} alive={alive}".format(
  13. name=thread.name, daemon=thread.daemon, alive=thread.is_alive()))
  14. print("")
  15.  
  16.  
  17. def run_and_discard(exc):
  18. futures = []
  19. for _ in range(0, 10):
  20. futures.append(exc.submit(lambda: 42))
  21. print("Created %s futures" % len(futures))
  22.  
  23.  
  24. def run_test(exc, shutdown=False):
  25. if shutdown:
  26. with exc() as executor:
  27. run_and_discard(executor)
  28. time.sleep(1)
  29. else:
  30. run_and_discard(exc())
  31.  
  32. # Now we don't hold any references to either the executor or
  33. # any of its futures.
  34. # What happens?
  35. for i in range(0, 3):
  36. dump_threads()
  37. print("About to GC %s" % i)
  38. gc.collect()
  39. print("After GC %s" % i)
  40. time.sleep(5)
  41.  
  42.  
  43. print("============== thread pool ======================")
  44. run_test(lambda: Executors.thread_pool(max_workers=4))
  45.  
  46. print("============== thread pool w/retry w/shutdown ==============")
  47. run_test(lambda: Executors.thread_pool(max_workers=4).with_retry(), shutdown=True)
  48.  
  49. print("============== thread pool w/retry ==============")
  50. run_test(lambda: Executors.thread_pool(max_workers=4).with_retry())
Add Comment
Please, Sign In to add comment