Skip to content

Instantly share code, notes, and snippets.

@loretoparisi
Last active April 10, 2025 20:19
Show Gist options
  • Save loretoparisi/c4de40a84f59ed77c6cb98cd0c499659 to your computer and use it in GitHub Desktop.
Save loretoparisi/c4de40a84f59ed77c6cb98cd0c499659 to your computer and use it in GitHub Desktop.
Python Thread Support with Bounder Thread Pool or Process Executor
import multiprocessing
import concurrent.futures
import threading
name = 'bounded_pool_executor'
class _BoundedPoolExecutor:
semaphore = None
def acquire(self):
self.semaphore.acquire()
def release(self, fn):
self.semaphore.release()
def submit(self, fn, *args, **kwargs):
self.acquire()
future = super().submit(fn, *args, **kwargs)
future.add_done_callback(self.release)
return future
class BoundedProcessPoolExecutor(_BoundedPoolExecutor, concurrent.futures.ProcessPoolExecutor):
def __init__(self, max_workers=None):
super().__init__(max_workers)
self.semaphore = multiprocessing.BoundedSemaphore(max_workers)
class BoundedThreadPoolExecutor(_BoundedPoolExecutor, concurrent.futures.ThreadPoolExecutor):
def __init__(self, max_workers=None):
super().__init__(max_workers)
self.semaphore = threading.BoundedSemaphore(max_workers)
@loretoparisi
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment