From f83aedf0f10dda41a32f5d26a517fb64d726872e Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 6 Jan 2018 00:21:25 +0100 Subject: [PATCH] Support for synchronous and asynchronous procedures --- platypush/__init__.py | 2 +- platypush/config/__init__.py | 9 +++++++-- platypush/message/request/__init__.py | 5 ++++- platypush/procedure/__init__.py | 22 ++++++++++++++-------- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/platypush/__init__.py b/platypush/__init__.py index e17f7751a..945045423 100644 --- a/platypush/__init__.py +++ b/platypush/__init__.py @@ -12,7 +12,7 @@ from .event.processor import EventProcessor from .message.event import Event, StopEvent from .message.request import Request from .message.response import Response -from .procedure import Procedure + __author__ = 'Fabio Manganiello ' __version__ = '0.6.5' diff --git a/platypush/config/__init__.py b/platypush/config/__init__.py index 6828ebee8..d98b27fcf 100644 --- a/platypush/config/__init__.py +++ b/platypush/config/__init__.py @@ -78,8 +78,13 @@ class Config(object): hook_name = '.'.join(key.split('.')[2:]) self.event_hooks[hook_name] = self._config[key] elif key.startswith('procedure.'): - procedure_name = '.'.join(key.split('.')[1:]) - self.procedures[procedure_name] = self._config[key] + tokens = key.split('.') + async = True if tokens[1] == 'async' else False + procedure_name = '.'.join(tokens[2:]) + self.procedures[procedure_name] = { + 'async': async, + 'actions': self._config[key] + } else: self.plugins[key] = self._config[key] diff --git a/platypush/message/request/__init__.py b/platypush/message/request/__init__.py index da7b7fb9d..3e711b065 100644 --- a/platypush/message/request/__init__.py +++ b/platypush/message/request/__init__.py @@ -60,7 +60,10 @@ class Request(Message): logging.info('Executing procedure request: {}'.format(self.action)) proc_name = self.action.split('.')[-1] proc_config = Config.get_procedures()[proc_name] - proc = Procedure.build(name=proc_name, requests=proc_config, backend=self.backend, id=self.id) + proc = Procedure.build(name=proc_name, requests=proc_config['actions'], + async=proc_config['async'], + backend=self.backend, id=self.id) + return proc.execute(*args, **kwargs) diff --git a/platypush/procedure/__init__.py b/platypush/procedure/__init__.py index a7781fde3..5e9cf950e 100644 --- a/platypush/procedure/__init__.py +++ b/platypush/procedure/__init__.py @@ -7,14 +7,17 @@ from ..message.response import Response class Procedure(object): """ Procedure class. A procedure is a pre-configured list of requests """ - def __init__(self, name, requests, backend=None): + def __init__(self, name, async, requests, backend=None): """ Params: name -- Procedure name + async -- Whether the actions in the procedure are supposed to + be executed sequentially or in parallel (True or False) requests -- List of platylist.message.request.Request objects """ self.name = name + self.async = async self.requests = requests self.backend = backend @@ -22,7 +25,7 @@ class Procedure(object): req.backend = self.backend @classmethod - def build(cls, name, requests, backend=None, id=None, **kwargs): + def build(cls, name, async, requests, backend=None, id=None, **kwargs): reqs = [] for request_config in requests: request_config['origin'] = Config.get('device_id') @@ -33,7 +36,7 @@ class Procedure(object): request = Request.build(request_config) reqs.append(request) - return cls(name=name, requests=reqs, backend=backend, **kwargs) + return cls(name=name, async=async, requests=reqs, backend=backend, **kwargs) def execute(self, n_tries=1): """ @@ -47,12 +50,15 @@ class Procedure(object): response = Response() for request in self.requests: - response = request.execute(n_tries, async=False, **context) - context = { k:v for (k,v) in response.output.items() } \ - if isinstance(response.output, dict) else {} + if self.async: + request.execute(n_tries, async=True, **context) + else: + response = request.execute(n_tries, async=False, **context) + context = { k:v for (k,v) in response.output.items() } \ + if isinstance(response.output, dict) else {} - context['output'] = response.output - context['errors'] = response.errors + context['output'] = response.output + context['errors'] = response.errors return response