def get(self, timeout=None, propagate=True, interval=0.5, no_ack=True, follow_parents=True, callback=None, on_interval=None, EXCEPTION_STATES=states.EXCEPTION_STATES, PROPAGATE_STATES=states.PROPAGATE_STATES): """Wait until task is ready, and return its result. .. warning:: Waiting for tasks within a task may lead to deadlocks. Please read :ref:`task-synchronous-subtasks`. :keyword timeout: How long to wait, in seconds, before the operation times out. :keyword propagate: Re-raise exception if the task failed. :keyword interval: Time to wait (in seconds) before retrying to retrieve the result. Note that this does not have any effect when using the amqp result store backend, as it does not use polling. :keyword no_ack: Enable amqp no ack (automatically acknowledge message). If this is :const:`False` then the message will **not be acked**. :keyword follow_parents: Reraise any exception raised by parent task. :raises celery.exceptions.TimeoutError: if `timeout` is not :const:`None` and the result does not arrive within `timeout` seconds. If the remote call raised an exception then that exception will be re-raised. """ assert_will_not_block() _on_interval = promise() if follow_parents and propagate and self.parent: on_interval = promise(self._maybe_reraise_parent_error) self._maybe_reraise_parent_error() if on_interval: _on_interval.then(on_interval) if self._cache: if propagate: self.maybe_reraise() return self.result meta = self.backend.wait_for( self.id, timeout=timeout, interval=interval, on_interval=_on_interval, no_ack=no_ack, ) if meta: self._maybe_set_cache(meta) state = meta['status'] if state in PROPAGATE_STATES and propagate: raise meta['result'] if callback is not None: callback(self.id, meta['result']) return meta['result']
def get(self, timeout=None, propagate=True, interval=0.5, no_ack=True, follow_parents=True, callback=None, on_interval=None, EXCEPTION_STATES=states.EXCEPTION_STATES, PROPAGATE_STATES=states.PROPAGATE_STATES): """Wait until task is ready, and return its result. .. warning:: Waiting for tasks within a task may lead to deadlocks. Please read :ref:`task-synchronous-subtasks`. :keyword timeout: How long to wait, in seconds, before the operation times out. :keyword propagate: Re-raise exception if the task failed. :keyword interval: Time to wait (in seconds) before retrying to retrieve the result. Note that this does not have any effect when using the amqp result store backend, as it does not use polling. :keyword no_ack: Enable amqp no ack (automatically acknowledge message). If this is :const:`False` then the message will **not be acked**. :keyword follow_parents: Reraise any exception raised by parent task. :raises celery.exceptions.TimeoutError: if `timeout` is not :const:`None` and the result does not arrive within `timeout` seconds. If the remote call raised an exception then that exception will be re-raised. """ assert_will_not_block() _on_interval = promise() if follow_parents and propagate and self.parent: on_interval = promise(self._maybe_reraise_parent_error) self._maybe_reraise_parent_error() if on_interval: _on_interval.then(on_interval) if self._cache: if propagate: self.maybe_reraise() return self.result meta = self.backend.wait_for( self.id, timeout=timeout, interval=interval, on_interval=_on_interval, no_ack=no_ack, ) if meta: self._maybe_set_cache(meta) status = meta['status'] if status in PROPAGATE_STATES and propagate: raise meta['result'] if callback is not None: callback(self.id, meta['result']) return meta['result']
def autodiscover_tasks(self, packages=None, related_name='tasks', force=False): if force: return self._autodiscover_tasks(packages, related_name) signals.import_modules.connect(promise( self._autodiscover_tasks, (packages, related_name), ), weak=False, sender=self)
def autodiscover_tasks(self, packages, related_name='tasks', force=False): if force: return self._autodiscover_tasks(packages, related_name) signals.import_modules.connect(promise( self._autodiscover_tasks, (packages, related_name), ), weak=False, sender=self)
def basic_cancel(self, consumer_tag): # If we are busy reading messages we may experience # a race condition where a message is consumed after # cancelling, so we must delay this operation until reading # is complete (Issue celery/celery#1773). connection = self.connection if connection: if connection.cycle._in_protected_read: return connection.cycle.after_read.add(promise(self._basic_cancel, (consumer_tag,))) return self._basic_cancel(consumer_tag)
def send_ack(response, pid, job, fd, WRITE=WRITE, ERR=ERR): # Only used when synack is enabled. # Schedule writing ack response for when the fd is writeable. msg = Ack(job, fd, precalc[response]) callback = promise(write_generator_done) cor = _write_ack(fd, msg, callback=callback) mark_write_gen_as_active(cor) mark_write_fd_as_active(fd) callback.args = (cor,) add_writer(fd, cor)
def basic_cancel(self, consumer_tag): # If we are busy reading messages we may experience # a race condition where a message is consumed after # cancelling, so we must delay this operation until reading # is complete (Issue celery/celery#1773). if self.connection.cycle._in_protected_read: return self.connection.cycle.after_read.add( promise(self._basic_cancel, (consumer_tag, )), ) return self._basic_cancel(consumer_tag)
def autodiscover_tasks(self, packages=None, related_name='tasks', force=False): """Try to autodiscover and import modules with a specific name (by default 'tasks'). If the name is empty, this will be delegated to fixups (e.g. Django). For example if you have an (imagined) directory tree like this:: foo/__init__.py tasks.py models.py bar/__init__.py tasks.py models.py baz/__init__.py models.py Then calling ``app.autodiscover_tasks(['foo', bar', 'baz'])`` will result in the modules ``foo.tasks`` and ``bar.tasks`` being imported. :param packages: List of packages to search. This argument may also be a callable, in which case the value returned is used (for lazy evaluation). :keyword related_name: The name of the module to find. Defaults to "tasks", which means it look for "module.tasks" for every module in ``packages``. :keyword force: By default this call is lazy so that the actual autodiscovery will not happen until an application imports the default modules. Forcing will cause the autodiscovery to happen immediately. """ if force: return self._autodiscover_tasks(packages, related_name) signals.import_modules.connect(promise( self._autodiscover_tasks, (packages, related_name), ), weak=False, sender=self)
def call_soon(self, callback, *args): handle = promise(callback, args) self._ready.append(handle) return handle
def test_send_method__callback(self): callback = Mock(name='callback') p = promise(callback) self.c.send_method((50, 60), 'iB', (30, 0), callback=p) callback.assert_called_with()