[Twisted-Python] running code on its own and importing, howto install a different reactor?
steven meier
commercials24 at yahoo.de
Fri Jul 29 13:42:37 MDT 2016
On Fri, 2016-07-29 at 23:11 +0400, meejah wrote:
> I think the "recommended" way to get access to a reactor instance is to
> pass it in to methods that require it.
>
> While this can sometimes seem tedious it a) helps testing (because now
> you can easily pass a fake reactor object like Clock or a Mock instance)
> and b) shows you (and your users) which methods are (or might-be) async.
>
> So, try a signature like "def terrific_method(reactor):" instead. Now
> when you're writing tests, you can do this:
>
> fake_reactor = Clock()
> d = terrific_method(fake_reactor)
> fake_reactor.advance(2)
> # etc.
>
Ah yes, you answered a question of mine not so long ago earlier and told
me exactly that....sorry forgot.
workig example if anybody ever digs this out:
i modified the code like this:
working_code.py
from twisted.internet.defer import inlineCallbacks, Deferred,
returnValue
@inlineCallbacks
def print_it(reactor):
arg = yield terrific_method(reactor)
print arg
@inlineCallbacks
def terrific_method(reactor):
d = Deferred()
reactor.callLater(2, d.callback, 2)
result = yield d
returnValue(result)
if __name__ == '__main__':
import qt5reactor
qt5reactor.install()
from twisted.internet import reactor
reactor.callWhenRunning(print_it, reactor)
reactor.run()
main_code.py:
import qt5reactor
qt5reactor.install()
from twisted.internet import reactor
from working_code import print_it
print_it(reactor)
reactor.run()
now on to some tests...
More information about the Twisted-Python
mailing list