python - What is causing my Deferred to fire, having done nothing but add a callback, start the reactor, and connected an endpoint? -
i'm using if
-statement decide whether or not fire deferred
. has fired before check runs. output, looks deferred
firing upon running reactor.run()
. doesn't have trigger callback happening?
relevant code snippets:
class outpostburrownew(amp.amp, protocol): protocol = outpostgopher ... def rfidtest(self): print('checking tag - deferred called: %s' % str(self.defer.called)) (status,tagtype) = mifarereader.mfrc522_request(mifarereader.picc_reqidl) if status == mifarereader.mi_ok: status,uid = self.verify_card() cardid = '-'.join([str(x) x in uid]) print('card detected: %s' % str(cardid)) self.defer.callback() def main3(): ''' main3() testing reactor usage of rfid-card reading ''' burrow = outpostburrownew('client') burrow.protocol = outpostgopher burrow.connectendpoint() # deferred created, set on burrow.defer def printercallback(proto): # gets passed outpostgopher protocol print('main3.printercallback() - %s ' % str(time.time())) burrow.defer.addcallback(printercallback) looper = loopingcall(burrow.rfidtest) loopdefer = looper.start(1, now=false) print('deferred created, status: %s' % str(burrow.defer.called)) reactor.run()
output:
(env)pi@raspi ~/zenithproject/zenith $ sudo python indev/rfidread_remote.py deferred created, status: false gopher.connectionmade() called. main3.printercallback() - 1426884127.26 checking tag - deferred called: true checking tag - deferred called: true checking tag - deferred called: true ^cctrl+c captured, ending read.
i figured out when twisted endpoint (in case tcp4clientendpoint
) makes connection endpoint (here tcp4serverendpoint
), deferred
created connection fired (i.e. when protocol.connectionmade()
automatically called upon connecting).
this fixed not making connection until deferred
needs fired (alternatively, suppose create deferred
use), so:
class outpostburrownew(amp.amp, protocol): protocol = outpostgopher #... def rfidtest(self): #print("checking tag - deferred called: %s" % str(self.defer.called)) print('tag check') (status,tagtype) = mifarereader.mfrc522_request(mifarereader.picc_reqidl) if status == mifarereader.mi_ok: # print "card detected" status,uid = self.verifycard() cardid = '-'.join([str(x) x in uid]) print('card detected: %s' % str(cardid)) #self.defer.callback() self.connectendpoint() def main3(): ''' main3() testing reactor usage of rfid-card reading ''' burrow = outpostburrownew('client') burrow.protocol = outpostgopher #burrow.connectendpoint() # deferred created, set on burrow.defer looper = loopingcall(burrow.rfidtest) loopdef = looper.start(1, now=false) reactor.run()
which results in following:
(env)pi@raspi ~/zenithproject/zenith $ sudo python indev/rfidread_remote.py tag check tag check card detected: 133-197-223-29-130 gopher.connectionmade() called. tag check tag check
Comments
Post a Comment