[core] Execution contexts are now context managers.
This commit is contained in:
@ -58,6 +58,13 @@ class LoopingExecutionContext(Wrapper):
|
|||||||
# XXX enhancers
|
# XXX enhancers
|
||||||
self._enhancers = get_enhancers(self.wrapped)
|
self._enhancers = get_enhancers(self.wrapped)
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
self.start()
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type=None, exc_val=None, exc_tb=None):
|
||||||
|
self.stop()
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
if self.started:
|
if self.started:
|
||||||
raise RuntimeError('Cannot start a node twice ({}).'.format(get_name(self)))
|
raise RuntimeError('Cannot start a node twice ({}).'.format(get_name(self)))
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
from concurrent.futures import Executor, ProcessPoolExecutor, ThreadPoolExecutor
|
from concurrent.futures import Executor, ProcessPoolExecutor, ThreadPoolExecutor
|
||||||
|
|
||||||
from bonobo.constants import BEGIN, END
|
from bonobo.constants import BEGIN, END
|
||||||
@ -29,19 +28,16 @@ class ExecutorStrategy(Strategy):
|
|||||||
futures = []
|
futures = []
|
||||||
|
|
||||||
for plugin_context in context.plugins:
|
for plugin_context in context.plugins:
|
||||||
|
|
||||||
def _runner(plugin_context=plugin_context):
|
def _runner(plugin_context=plugin_context):
|
||||||
|
with plugin_context:
|
||||||
try:
|
try:
|
||||||
plugin_context.start()
|
|
||||||
plugin_context.loop()
|
plugin_context.loop()
|
||||||
plugin_context.stop()
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
print_error(exc, traceback.format_exc(), context=plugin_context)
|
print_error(exc, traceback.format_exc(), context=plugin_context)
|
||||||
|
|
||||||
futures.append(executor.submit(_runner))
|
futures.append(executor.submit(_runner))
|
||||||
|
|
||||||
for node_context in context.nodes:
|
for node_context in context.nodes:
|
||||||
|
|
||||||
def _runner(node_context=node_context):
|
def _runner(node_context=node_context):
|
||||||
try:
|
try:
|
||||||
node_context.start()
|
node_context.start()
|
||||||
|
|||||||
Reference in New Issue
Block a user