Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- from __future__ import print_function
- import gc
- import time
- import threading
- from more_executors import Executors
- def dump_threads():
- print("------------- threads ----------------")
- for thread in threading.enumerate():
- print(" {name} daemon={daemon} alive={alive}".format(
- name=thread.name, daemon=thread.daemon, alive=thread.is_alive()))
- print("")
- def run_and_discard(exc):
- futures = []
- for _ in range(0, 10):
- futures.append(exc.submit(lambda: 42))
- print("Created %s futures" % len(futures))
- def run_test(exc, shutdown=False):
- if shutdown:
- with exc() as executor:
- run_and_discard(executor)
- time.sleep(1)
- else:
- run_and_discard(exc())
- # Now we don't hold any references to either the executor or
- # any of its futures.
- # What happens?
- for i in range(0, 3):
- dump_threads()
- print("About to GC %s" % i)
- gc.collect()
- print("After GC %s" % i)
- time.sleep(5)
- print("============== thread pool ======================")
- run_test(lambda: Executors.thread_pool(max_workers=4))
- print("============== thread pool w/retry w/shutdown ==============")
- run_test(lambda: Executors.thread_pool(max_workers=4).with_retry(), shutdown=True)
- print("============== thread pool w/retry ==============")
- run_test(lambda: Executors.thread_pool(max_workers=4).with_retry())
Add Comment
Please, Sign In to add comment