I want to use twisted (and StarPy which is a protocol implementation for asterisk ami) to connect to an asterisk server. The application initiates a outgoing fax there. I found some hints on my problem, but I cannot find out how to handle this correctly.
The first fax is sent out correctly.
Problem is, if I call twisted for the second time, the application keeps hanging in main loop.
I know I may NOT do this like here:
from starpy import manager
from twisted.internet import reactor
def main():
f = manager.AMIFactory(cUser, cPass)
print "Login"
df = f.login(cServer, cPort)
def onLogin(protocol):
print "Logoff again"
df = protocol.logoff()
def onLogoff( result ):
print "Logoff erfolgt"
reactor.stop()
return df.addCallbacks( onLogoff, onLogoff )
def onFailure( reason ):
print "Login failed"
print reason.getTraceback()
df.addCallbacks( onLogin, onFailure )
return df
if __name__ == "__main__":
reactor.callWhenRunning( main )
reactor.run(installSignalHandlers=0)
print "runned the first time"
reactor.callWhenRunning( main )
reactor.run(installSignalHandlers=0)
print "will never reach this point"
I simplified the code - it does nothing than login + logoff again. It will never return from the second reactor.run() call.
How is this done correctly? I'm stuck here - thanks in advance.
Best Regards, Florian.
As iny said, you need to do everything with just one call to reactor.run
and reactor.stop
.
If we consider the example code you posted, we see that it takes these steps:
If we only delete steps 3 and 4, then the program will actually be doing a pretty reasonable thing.
Here's how you implemented step 3:
def onLogoff( result ):
print "Logoff erfolgt"
reactor.stop()
This caused the first call to reactor.run
to return, clearing the way for your implementation of step 4:
reactor.callWhenRunning( main )
reactor.run(installSignalHandlers=0)
So, the general idea here is going to be to jump right to step 5 instead of doing step 3 and 4. Consider what might happen if you redefine onLogoff
like this:
def onLogoff( result ):
print "Logoff erfolgt"
main()
and deleting the last three lines of your example. This will actually give you an infinite loop, since the same onLogoff
runs after the 2nd disconnect and starts a 3rd connection. However, you might remedy this with a parameter to the main
function to control the restart behavior.
Once this makes sense, you may want to think about moving the retry logout out of the main
function and into a callback defined in the __main__
block. This is a big part of the power of Deferreds: it lets you keep proper separation between the implementation of an event source (in this case, your fax sending function) and the code for dealing with the resulting events (sending a second fax, or exiting, in this case).
Thanks for your answers, I haven't implemented a solution right now but I know how I could do that now...
Here a short summary of things I learned.
First, in short - the problems I had with twisted:
In my concept I solved these problems:
I needed a lot of re-thinking but as soon as you get it it looks really easy.
Thanks to iny and Jean-Paul Calderone for your help.
You can't restart the reactor. In other words, you can call reactor.run() only once.
Instead can do everything you need in one reactor run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With