diff --git a/platypush/__init__.py b/platypush/__init__.py index e17f7751af..9450454234 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 6828ebee80..d98b27fcf3 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 da7b7fb9d3..3e711b0655 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 a7781fde39..5e9cf950e4 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