Why can't we call Twisted twice?

From the following guide: http://krondo.com/blog/?p=1682

Delays help us avoid one of the problems that we identified when programming the callback. When we use deferred to control our callbacks, we simply cannot make a mistake by invoking both the callback and the error, or calling the callback twenty-seven times. We can try, but deferred ones will throw an exception right on us, instead of skipping our error on the callbacks themselves

Can someone give me a better explanation?

I noticed that this will not work, because in most cases in this tutorial the end of the callback also calls reactor.stop (). But why, although it does not make sense to call a reprieve twice? Why is this different than calling a chain of methods?

+7
python twisted
source share
1 answer

A Deferred represents the result of a query that may be available (now or in the future), but so far definitely not available.

Results pass through Deferred s through the callback chain. For example, in a synchronous program, you might have something like:

 response_bytes = make_request(...) response_dict = parse_json(response_bytes) response_object = construct_object(response_dict) return response_object.method() 

The translated code that Deferred returns is:

 response_bytes_later = make_request_async(...) response_dict_later = response_bytes_later.addCallback(parse_json) response_object_later = response_dict_later.addCallback(construct_object) return response_object_later.addCallback(lambda response_object: response_object.method()) 

Asking why you cannot start (or β€œcall back”), Deferred returned by make_request_async is like asking why you cannot return make_request several times to cause a callback. If you want to resubmit the request in the synchronous version, you need to call make_request again (and get a new result). If you want to send the request again in the asynchronous version, you need to call make_request_async again (and get a new Deferred ).

+7
source share

All Articles