[Twisted-Python] returning value after addCallback
Hi, you could tell me that I should stop programming since I don't know properly OOP but I quite need to sort out this problem. Here is another piece of code I have problem with. Please be patient with me. Where is mistake in my code. It is not returning any reasonable result. Thank you for help. Lada -------------------------------------- code ------------------------------------------------- from twisted.internet import reactor, defer, threads from siptest import siptest_f test_opts = {'username':'test','transport':'udp','localport':'5085','password':'test'} domain = ['sip.1und1.de', 'sip.babble.net'] class PingTracker: def __init__(self): self.dns = None self.sip = None self.ping = None def gotSipServerResults(self, domain, test_opts): if domain: return siptest_f(domain, test_opts) raise ValueError("Error occured") def checkSIP(self, domain, test_opts): return threads.deferToThread(self.gotSipServerResults, domain, test_opts) def check(self, host, test_opts): checks = [] #checks.append(checkDNS().addCallback(self.setDNSResult)) checks.append(self.checkSIP(host, test_opts).addCallback(self.setSIPResult)) #checks.append(checkPing().addCallback(self.setPingResult)) return defer.gatherResults(checks) def setDNSResult(self, result): self.dns = result def setSIPResult(self, result): self.sip = result def setPingResult(self, result): self.ping = result def SipCallback(self): print self.sip test = PingTracker() d = test.check(domain[0], test_opts) print d # this returns Deferred... test.SipCallback() # this returns None reactor.run()
test = PingTracker() d = test.check(domain[0], test_opts)
print d # this returns Deferred...
test.SipCallback() # this returns None
Yes, because no Twisted code has run yet
reactor.run()
...because you haven't started the reactor. I don't think you properly understand how Twisted works. Your code is executed in response to events (packets being received, timers expiring, reactor startup/shutdown events). The reactor runs the event loop.
Phil Mayers wrote:
test = PingTracker() d = test.check(domain[0], test_opts)
print d # this returns Deferred...
test.SipCallback() # this returns None
Yes, because no Twisted code has run yet
reactor.run()
...because you haven't started the reactor.
So how should I run reactor then ? Yes, I have difficulties to get on the right "twisted track". I've seen many examples but probably didn't grasp the way how it works. Lada
I don't think you properly understand how Twisted works. Your code is executed in response to events (packets being received, timers expiring, reactor startup/shutdown events). The reactor runs the event loop.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (2)
-
Ladislav Andel
-
Phil Mayers