What does @ defer.inlineCallbacks mean when I don't need a return returning a value?

At scrapy.core.engine

Executing the ExecutionEngine Method

@defer.inlineCallbacks def start(self): """Start the execution engine""" assert not self.running, "Engine already running" self.start_time = time() yield self.signals.send_catch_log_deferred(signal=signals.engine_started) self.running = True self._closewait = defer.Deferred() yield self._closewait 

Why not use self.signals.send_catch_log_deferred(signal=signals.engine_started) directly, but instead of a lesson?

+5
source share
2 answers

Why not use self.signals.send_catch_log_deferred(signal=signals.engine_started) directly, but instead of a lesson?

Because send_catch_log_deferred returns a Deferred object. If you want to avoid yield , then you should use send_catch_log , but the point of use send_catch_log_deferred should allow listeners to return Deferred objects.

Signals that use send_catch_log cannot return Deferred objects, so they do not allow asynchronous operations.

Edit: For a good introduction to inlineCallbacks see: http://krondo.com/?p=2441

+4
source

@ defer.inlineCallbacks expects the decorated function to be a generator function and will call the generator function inside the decorated function (even returning it), does not make the function, the generator function. Study:

 def gen(): yield 1 def func(): return gen import dis dis.dis(gen) 2 0 LOAD_CONST 1 (1) 3 YIELD_VALUE 4 POP_TOP 5 LOAD_CONST 0 (None) 8 RETURN_VALUE dis.dis(func) 1 0 LOAD_GLOBAL 0 (gen) 3 RETURN_VALUE import inspect inspect.isgeneratorfunction(gen) True inspect.isgeneratorfunction(func) False 

Thus, the only way to satisfy @ defer.inlineCallbacks is either yield delayed from self.signals.send_catch_log_deferred (signal = signals.engine_started) or from another place.

+3
source

Source: https://habr.com/ru/post/1214713/


All Articles