Skip to content

Instantly share code, notes, and snippets.

@viksit
Created May 24, 2016 21:49
Show Gist options
  • Save viksit/f0a492d5f785c89dc33a60338e0ee14c to your computer and use it in GitHub Desktop.
Save viksit/f0a492d5f785c89dc33a60338e0ee14c 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