Last active
April 10, 2025 20:19
-
-
Save loretoparisi/c4de40a84f59ed77c6cb98cd0c499659 to your computer and use it in GitHub Desktop.
Python Thread Support with Bounder Thread Pool or Process Executor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thread_support.py