[Twisted-Python] what's wrong in my code?
hi, I got 'twisted.internet.defer.alreadycallederror' error back. here is my code, what's wrong in it? thanks for help. class snmpFactory: def __init__(self, filename): self.deferred = defer.Deferred() def readValue(self): vol = readValue(self.voltageIp, voltageOid) temp = readValue(self.powerIp, temperatureOid) self.deferred.callback((vol, temp)) self.deferred.addCallback(self.handleResult) def handleResult(self, (vol, temp)): slice = time.strftime("%H:%M:%S") print slice, ' the voltage is %s\nthe temperature is %s' % (vol, temp) s = snmpFactory() i = 1 while (i < 10): reactor.callLater(5, s.readSnmpValue) i += 1 time.sleep(10) reactor.run()
On 05/07/06, wang wei
hi, I got 'twisted.internet.defer.alreadycallederror' error back. here is my code, what's wrong in it? thanks for help.
There's actually a lot wrong with your code. I've dumped everything I can think of here. Don't worry, I get more helpful as you go further down the email. First, the simple stuff: - reactor is not imported - time is not imported - snmpFactory constructor takes an unused filename parameter - snmpFactory is constructed without passing the filename parameter - defer is not imported - the main while loop calls 'readSnmpValue' on an snmpFactory instance, but there is no readSnmpValue method to be seen anywhere - the readValue method calls a readValue function which is not defined - you use a while loop and a counter when 'for i in range(10)' would do the same thing more simply - readValue refers to instance variables 'voltageIp' and 'powerIp', neither of which are defined - readValue refers to global variables 'voltageOid' and 'temperatureOid', neither of which are defined - your code runs the reactor but never stops it. Second, the complex stuff: - Nothing in the code explains what you are trying to do, or even specifies the API of the methods that you refer to. - reactor.callLater(5, s.readValue) roughly means "call s.readValue with no arguments in 5 seconds time". You probably don't want to do that. - You probably don't want to call time.sleep() - In fact, you probably want a LoopingCall (see the API docs for details) Third, the AlreadyCalledError: - You create exactly one instance of snmpFactory. This means that there is exactly one Deferred. You fire this Deferred (i.e. run callback on it) in the first pass of the main while loop. You can only fire a Deferred once. The next time the loop goes around, you try to fire it a second time. This will raise an AlreadyCalledError. Fourth, a note on asking for help: Many people on this list are happy to help you learn Twisted. However, anyone who does help you will be doing so in their spare time. This means it is your responsibility to make it as easy for us to help you as possible. Two specific ways spring to mind: 1. If you are posting code, please post a minimal example that other people can run. 2. Read http://www.catb.org/~esr/faqs/smart-questions.html Your example is pretty small -- that's good. You know how to cut out irrelevant code. However, your example is not even close to being runnable. Even after I've taken the time to make it run, it doesn't reproduce the error that you refer to. Good luck with fixing your error. jml
participants (2)
-
Jonathan Lange
-
wang wei