Skip to content

Instantly share code, notes, and snippets.

@r-wheeler
Forked from viksit/checkin.py
Created March 16, 2018 15:53
Show Gist options
  • Save r-wheeler/4ce38a68a8882fbaaa095ede20b28cc2 to your computer and use it in GitHub Desktop.
Save r-wheeler/4ce38a68a8882fbaaa095ede20b28cc2 to your computer and use it in GitHub Desktop.
grpc and gevent thread pool executor
class ThreadPoolExecutor(concurrent.futures.ThreadPoolExecutor):
"""
A version of :class:`concurrent.futures.ThreadPoolExecutor` that
always uses native threads, even when threading is monkey-patched.
.. versionadded:: 1.2a1
"""
def __init__(self, max_workers):
super(ThreadPoolExecutor, self).__init__(max_workers)
self._threadpool = ThreadPool(max_workers)
def submit(self, fn, *args, **kwargs):
future = super(ThreadPoolExecutor, self).submit(fn, *args, **kwargs)
with self._shutdown_lock:
work_item = self._work_queue.get()
assert work_item.fn is fn
self._threadpool.spawn(work_item.run)
return future
def shutdown(self, wait=True):
super(ThreadPoolExecutor, self).shutdown(wait)
self._threadpool.kill()
kill = shutdown # greentest compat
def _adjust_thread_count(self):
# Does nothing. We don't want to spawn any "threads",
# let the threadpool handle that.
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment