Message342184
Here is a complete example.
Please consider this is a very simple example just to demonstrate what would be possible with my proposed changes.
In the following example you can see two things:
* saving informations about the context in which a workload was submitted to the Executor in the Future
* having a custom methods on the Future returned by the Executor
from concurrent.futures import Future, ThreadPoolExecutor
def workload(n):
# does some heavy calculations
return n
class Context:
def __init__(self, name):
self.name = name
class CustomFactory(Future):
def __init__(self):
super().__init__()
self.context = None
def is_from_context(self, ctx):
return self.context.name == ctx
def processed_result(self):
# processes the result
return self.result()
class ContextExecutor(ThreadPoolExecutor):
def __init__(self):
super().__init__(future_factory=CustomFactory)
def submit(self, workload, arg, context):
future = super().submit(workload, arg)
future.context = context
return future
context_executor = ContextExecutor()
futures = [
context_executor.submit(workload, 0, context=Context("A")),
context_executor.submit(workload, 1, context=Context("B")),
context_executor.submit(workload, 2, context=Context("A")),
]
futures_from_context_a = [f for f in futures if f.is_from_context("A")]
if all(f.done() for f in futures_from_context_a):
print("All Futures in context A are done")
processed_results = [f.processed_result() for f in futures_from_context_a]
print("Results:", processed_results) |
|
Date |
User |
Action |
Args |
2019-05-11 11:26:15 | stefanhoelzl | set | recipients:
+ stefanhoelzl, bquinlan, pitrou, asvetlov, tomMoral |
2019-05-11 11:26:15 | stefanhoelzl | set | messageid: <1557573975.92.0.818271074081.issue36512@roundup.psfhosted.org> |
2019-05-11 11:26:15 | stefanhoelzl | link | issue36512 messages |
2019-05-11 11:26:15 | stefanhoelzl | create | |
|