问题描述
我想使用twisted(和StarPy,它是asterisk ami 的协议实现)连接到asterisk 服务器.应用程序在那里启动外发传真.我发现了一些关于我的问题的提示,但我不知道如何正确处理这个问题.
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.
第一份传真正确发送.
问题是,如果我调用twisted for the第二次,应用程序保持挂在主循环中.
我知道我可能不会像这里那样做:
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"
我简化了代码 - 它除了再次登录 + 注销之外什么都不做.它永远不会从第二次 reactor.run() 调用返回.
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.
推荐答案
感谢您的回答,我现在还没有实施解决方案,但我现在知道如何实现...
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:
- 我不了解twisted 的异步基础知识.我在 gui-frameworks 中使用了类似的东西,但很长时间没有看到它的好处.
- 其次,我尝试多次考虑事件循环的同步调用.这在我看来是必要的,因为我一次可能不会使用多个外发传真线路.因为 Twisted 的事件循环不可重启,所以没有选择.正如我在文档中读到的那样,deferToThread"可以在这里帮助我,但我认为这不是最好的解决方案.
在我的概念中,我解决了这些问题:
In my concept I solved these problems:
- 使用 defer.DeferredSemaphore() 允许我将并发限制为 1.我在这里找到了一个例子:http://oubiwann.blogspot.com/2008/06/async-batching-with-twisted-walkthrough.html
- 随时调用扭曲并让 Defered 完成工作(发送传真)
- 将传真状态写入数据库并发送回执邮件是从延迟调用的
- Use of defer.DeferredSemaphore() which allows me to limit concurrency to 1.I found an example here: http://oubiwann.blogspot.com/2008/06/async-batching-with-twisted-walkthrough.html
- calling twisted anynchronously and let the Defereds do the work (send a fax)
- Writing the fax status to the database and sending the receipt mail is called from the defereds
我需要重新思考很多,但一旦你明白了,它看起来真的很容易.
I needed a lot of re-thinking but as soon as you get it it looks really easy.
感谢 iny 和 Jean-Paul Calderone 的帮助.
Thanks to iny and Jean-Paul Calderone for your help.
这篇关于用扭曲连接两次 - 如何正确地做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!