[Twisted-Python] what's wrong in my code?

Jonathan Lange jml at mumak.net
Tue Jul 4 23:16:49 EDT 2006

On 05/07/06, wang wei <wgwigw at gmail.com> wrote:
> 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
- 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.


More information about the Twisted-Python mailing list