Twisted tricks
While working on topka, I've used the twisted framework. Twisted tutorials are really good with lots of practical examples, anyway after using the framework for weeks, I gain enough experience to write a post about some tricks I've discovered.
Disclaimer: you will not find here something that is not already in the twisted docs.
Using deferred
While using twisted, it happens that you can be a situation where you can either return a
final value or do the computation asynchronously and return a Deferred
. The code looks like:
import twisted.internet.defer as defer def myFunc(): def treatRet(): ... ret = functionThatProcess(...) if isinstance(ret, defer.Deferred): ret.addCallback(treatRet) else: ret = treatRet(ret) return ret