用扭曲连接两次 - 如何正确执行?
我想使用twisted(和StarPy,它是asterisk ami的协议实现)连接到asterisk服务器。应用程序在那里启动传出传真。我发现了一些关于我的问题的提示,但我不知道如何正确处理这个问题。
第一份传真已正确发送。
问题是,如果我调用twisted 第二次,应用程序保留 挂在主循环中。
我知道我可能不会像这里这样做:
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如 iny 所说,您只需调用一次reactor.run 和reactor.stop 即可完成所有操作。
如果我们考虑您发布的示例代码,我们会看到它需要执行以下步骤:
如果我们只删除步骤 3 和4、那么程序实际上会做一件相当合理的事情。
以下是您实现步骤 3 的方式:
这导致对
reactor.run
的第一次调用返回,为您实现步骤 4 扫清了道路:因此,这里的总体思路是直接跳到步骤 5,而不是执行步骤 3 和 4。考虑一下,如果您像这样重新定义
onLogoff
并删除示例的最后三行,可能会发生什么情况。这实际上会给您带来无限循环,因为相同的
onLogoff
在第二次断开连接后运行并启动第三次连接。但是,您可以使用main
函数的参数来控制重新启动行为来解决此问题。一旦这有意义,您可能需要考虑将重试注销从
main
函数移到__main__
块中定义的回调中。这是 Deferreds 强大功能的一个重要部分:它使您可以在事件源(在本例中为传真发送函数)的实现和处理结果事件的代码(发送第二个传真,或退出,在这种情况下)。As iny said, you need to do everything with just one call to
reactor.run
andreactor.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:
This caused the first call to
reactor.run
to return, clearing the way for your implementation of step 4: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: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 themain
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).感谢您的回答,我现在还没有实施解决方案,但我知道现在该怎么做......
这里是我学到的东西的简短总结。
首先,简而言之 - 我在使用twisted时遇到的问题:
在我的概念中,我解决了这些问题:
我在这里找到了一个示例:http://oubiwann。 blogspot.com/2008/06/async-batching-with-twisted-walkthrough.html
我需要大量的重新思考,但一旦你得到它,它看起来就很容易了。
感谢 iny 和 Jean-Paul Calderone 的帮助。
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 found an example here: http://oubiwann.blogspot.com/2008/06/async-batching-with-twisted-walkthrough.html
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.
你无法重新启动反应堆。换句话说,您只能调用reactor.run()一次。
相反,可以在一个反应堆运行中完成您需要的一切。
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.
如果您仍在寻找解决方案...我也遇到了同样的问题。我有一个使用 Twisted 在远程服务器上执行程序的脚本。我需要一种从 django 应用程序内同步运行该脚本的方法。我最终做的是让我的 Twisted 脚本调用远程服务器并打印到标准输出。然后,在我的 Django 应用程序中,我通过 subprocess.Popen 执行该脚本并设置 stdout=PIPE,这样我就可以捕获 Twisted 脚本的输出并在我的 Django 应用程序中使用它。
这并不是很理想,而且几乎违背了 Twisted 的目的,但这已经超越了“无法再次调用reactor.run(),因为 Twisted 脚本每次都在它自己的进程中运行。
这确实做到了最终对我来说效果很好,听起来与您所处的情况非常相似。我希望这会有所帮助(如果您认为有帮助,我可以发布一些代码示例,请告诉我)。
If you're still looking for a solution... I had this same issue. I have a script that uses Twisted to execute a program on a remote server. I needed a way to run that script synchronously from within a django application. What I ended up doing was making my Twisted script call the remote server and just print to stdout. Then from within my Django app I executed that script via subprocess.Popen and set stdout=PIPE so I could capture the output from my Twisted script and use it in my Django app.
This isn't really ideal, and pretty much defeats the purpose of Twisted, but this gets past the "not being able to call reactor.run() a second time, since the Twisted script runs in it's own process each time.
This did end up working great for me, and it sounds very similar to the same situation that you're in. I hope this helps. Good luck. (I can post some code samples if you think it would help, just let me know).