wip, aio: asyncio strategy (defunct, not fully implemented) and related refactorings.
This commit is contained in:
@ -6,7 +6,8 @@ In the future, the two strategies that would really benefit bonobo are subproces
|
||||
at home if you want to give it a shot.
|
||||
|
||||
"""
|
||||
from bonobo.execution.strategies.executor import ProcessPoolExecutorStrategy, ThreadPoolExecutorStrategy
|
||||
from bonobo.execution.strategies.executor import ProcessPoolExecutorStrategy, ThreadPoolExecutorStrategy, \
|
||||
AsyncThreadPoolExecutorStrategy
|
||||
from bonobo.execution.strategies.naive import NaiveStrategy
|
||||
|
||||
__all__ = [
|
||||
@ -17,6 +18,7 @@ STRATEGIES = {
|
||||
'naive': NaiveStrategy,
|
||||
'processpool': ProcessPoolExecutorStrategy,
|
||||
'threadpool': ThreadPoolExecutorStrategy,
|
||||
'aio_threadpool': AsyncThreadPoolExecutorStrategy,
|
||||
}
|
||||
|
||||
DEFAULT_STRATEGY = 'threadpool'
|
||||
|
||||
@ -1,10 +1,16 @@
|
||||
import asyncio
|
||||
import functools
|
||||
import logging
|
||||
import sys
|
||||
from concurrent.futures import Executor, ProcessPoolExecutor, ThreadPoolExecutor
|
||||
|
||||
from cached_property import cached_property
|
||||
|
||||
from bonobo import settings
|
||||
from bonobo.constants import BEGIN, END
|
||||
from bonobo.execution.contexts.graph import AsyncGraphExecutionContext
|
||||
from bonobo.execution.strategies.base import Strategy
|
||||
from bonobo.util import get_name
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -73,6 +79,35 @@ class ThreadPoolExecutorStrategy(ExecutorStrategy):
|
||||
return self.executor_factory(max_workers=len(graph))
|
||||
|
||||
|
||||
class AsyncThreadPoolExecutorStrategy(ThreadPoolExecutorStrategy):
|
||||
GraphExecutionContextType = AsyncGraphExecutionContext
|
||||
|
||||
def __init__(self, GraphExecutionContextType=None):
|
||||
if not settings.ALPHA.get():
|
||||
raise NotImplementedError(
|
||||
'{} is experimental, you need to explicitely activate it using ALPHA=True in system env.'.format(
|
||||
get_name(self)
|
||||
)
|
||||
)
|
||||
super().__init__(GraphExecutionContextType)
|
||||
|
||||
@cached_property
|
||||
def loop(self):
|
||||
return asyncio.get_event_loop()
|
||||
|
||||
def create_graph_execution_context(self, *args, **kwargs):
|
||||
return super(AsyncThreadPoolExecutorStrategy, self).create_graph_execution_context(
|
||||
*args, **kwargs, loop=self.loop
|
||||
)
|
||||
|
||||
def get_starter(self, executor, futures):
|
||||
return functools.partial(
|
||||
self.loop.run_in_executor,
|
||||
executor,
|
||||
super(AsyncThreadPoolExecutorStrategy, self).get_starter(executor, futures),
|
||||
)
|
||||
|
||||
|
||||
class ProcessPoolExecutorStrategy(ExecutorStrategy):
|
||||
executor_factory = ProcessPoolExecutor
|
||||
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
from bonobo.constants import BEGIN, END
|
||||
from bonobo.execution.strategies.base import Strategy
|
||||
|
||||
|
||||
@ -6,20 +5,6 @@ class NaiveStrategy(Strategy):
|
||||
# TODO: how to run plugins in "naive" mode ?
|
||||
|
||||
def execute(self, graph, **kwargs):
|
||||
context = self.create_graph_execution_context(graph, **kwargs)
|
||||
context.write(BEGIN, (), END)
|
||||
|
||||
# start
|
||||
context.start()
|
||||
|
||||
# loop
|
||||
nodes = list(context.nodes)
|
||||
while len(nodes):
|
||||
for node in nodes:
|
||||
node.loop()
|
||||
nodes = list(node for node in nodes if node.alive)
|
||||
|
||||
# stop
|
||||
context.stop()
|
||||
|
||||
with self.create_graph_execution_context(graph, **kwargs) as context:
|
||||
context.run_until_complete()
|
||||
return context
|
||||
|
||||
Reference in New Issue
Block a user