Stefan Hölzl <[email protected]> added the comment:
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)
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue36512>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com