-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added executors for async operations
- Loading branch information
1 parent
f6a787e
commit a14cc44
Showing
4 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from flask_executor import Executor | ||
from injector import inject | ||
|
||
|
||
class ExecutorInterface: | ||
|
||
def submit(self, fn, *args, **kwargs): | ||
raise NotImplementedError | ||
|
||
|
||
class FlaskExecutorAdapter(ExecutorInterface): | ||
""" | ||
Adapter for Flask Executor, suitable for I/O-bound tasks that benefit from asynchronous execution. | ||
Use this executor for tasks that involve waiting for I/O operations (e.g., network requests, file I/O), | ||
where the overhead of creating new threads or processes is justified by the time spent waiting. | ||
""" | ||
|
||
@inject | ||
def __init__(self, executor: Executor): | ||
self.executor = executor | ||
|
||
def submit(self, fn, *args, **kwargs): | ||
return self.executor.submit(fn, *args, **kwargs) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from concurrent.futures import ProcessPoolExecutor | ||
|
||
from injector import inject | ||
|
||
|
||
class ProcessPoolExecutorInterface: | ||
|
||
def submit(self, fn, *args, **kwargs): | ||
raise NotImplementedError | ||
|
||
|
||
class ProcessPoolExecutorAdapter(ProcessPoolExecutorInterface): | ||
""" | ||
Adapter for Python's ProcessPoolExecutor, suitable for CPU-bound tasks that benefit from parallel execution. | ||
Use this executor for tasks that require significant computation and can be efficiently parallelized across multiple CPUs. | ||
Not for I/O-bound tasks like database queries, file I/O, or network requests, as the overhead of creating and managing processes can outweigh the benefits. | ||
This executor is ideal for scenarios where the task execution time would significantly benefit from being distributed | ||
across multiple processes, thereby bypassing the GIL (Global Interpreter Lock) and utilizing multiple CPU cores. | ||
Note: ProcessPoolExecutor is best used with tasks that are relatively heavy and can be executed independently of each other. | ||
""" | ||
|
||
def __init__(self, max_workers=None): | ||
self.executor = ProcessPoolExecutor(max_workers=max_workers) | ||
|
||
def submit(self, fn, *args, **kwargs): | ||
raise NotImplementedError( | ||
"ProcessPoolExecutorAdapter does not support 'submit' directly due to its nature. Use 'map' or other methods as needed." | ||
) | ||
|
||
def map(self, fn, *iterables, timeout=None, chunksize=1): | ||
return self.executor.map(fn, *iterables, timeout=timeout, chunksize=chunksize) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from concurrent.futures import ThreadPoolExecutor | ||
|
||
from injector import inject | ||
|
||
|
||
class ThreadPoolExecutorInterface: | ||
|
||
def submit(self, fn, *args, **kwargs): | ||
raise NotImplementedError | ||
|
||
|
||
class ThreadPoolExecutorAdapter(ThreadPoolExecutorInterface): | ||
""" | ||
Adapter for Python's ThreadPoolExecutor, suitable for I/O-bound tasks that can be performed concurrently. | ||
Use this executor for tasks that are largely waiting on I/O operations, such as database queries or file reads, | ||
where the GIL (Global Interpreter Lock) does not become a bottleneck. | ||
Not for CPU-bound tasks like heavy computation, as the GIL would prevent true parallel execution. | ||
This executor is particularly useful when you want more control over the number of concurrent threads | ||
than what Flask Executor provides, or when you're not working within a Flask application context. | ||
""" | ||
|
||
def __init__(self, max_workers=None): | ||
self.executor = ThreadPoolExecutor(max_workers=max_workers) | ||
|
||
def submit(self, fn, *args, **kwargs): | ||
return self.executor.submit(fn, *args, **kwargs) | ||
|
||
def map(self, fn, *iterables, timeout=None, chunksize=1): | ||
return self.executor.map(fn, *iterables, timeout=timeout, chunksize=chunksize) |
This file contains 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