Skip to content

Instantly share code, notes, and snippets.

@rohanpm
Created December 19, 2018 04:53
Show Gist options
  • Select an option

  • Save rohanpm/117dd79f7fa1e806f721e54d66f67773 to your computer and use it in GitHub Desktop.

Select an option

Save rohanpm/117dd79f7fa1e806f721e54d66f67773 to your computer and use it in GitHub Desktop.
#!/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())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment