用扭曲连接两次 - 如何正确执行?

发布于 2024-08-16 05:13:29 字数 1185 浏览 3 评论 0原文

我想使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千里故人稀 2024-08-23 05:13:29

正如 iny 所说,您只需调用一次reactor.run 和reactor.stop 即可完成所有操作。

如果我们考虑您发布的示例代码,我们会看到它需要执行以下步骤:

  1. 启动反应器
  2. 连接,发送传真,断开连接
  3. 停止反应器
  4. 启动反应器
  5. 连接,发送传真,断开连接
  6. 停止反应器

如果我们只删除步骤 3 和4、那么程序实际上会做一件相当合理的事情。

以下是您实现步骤 3 的方式:

def onLogoff( result ):
    print "Logoff erfolgt"
    reactor.stop()

这导致对 reactor.run 的第一次调用返回,为您实现步骤 4 扫清了道路:

reactor.callWhenRunning( main )
reactor.run(installSignalHandlers=0)

因此,这里的总体思路是直接跳到步骤 5,而不是执行步骤 3 和 4。考虑一下,如果您像这样重新定义 onLogoff

def onLogoff( result ):
    print "Logoff erfolgt"
    main()

并删除示例的最后三行,可能会发生什么情况。这实际上会给您带来无限循环,因为相同的 onLogoff 在第二次断开连接后运行并启动第三次连接。但是,您可以使用 main 函数的参数来控制重新启动行为来解决此问题。

一旦这有意义,您可能需要考虑将重试注销从 main 函数移到 __main__ 块中定义的回调中。这是 Deferreds 强大功能的一个重要部分:它使您可以在事件源(在本例中为传真发送函数)的实现和处理结果事件的代码(发送第二个传真,或退出,在这种情况下)。

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:

  1. Start the reactor
  2. Connect, send a fax, disconnect
  3. Stop the reactor
  4. Start the reactor
  5. Connect, send a fax, disconnect
  6. Stop the reactor

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).

心的位置 2024-08-23 05:13:29

感谢您的回答,我现在还没有实施解决方案,但我知道现在该怎么做......

这里是我学到的东西的简短总结。

首先,简而言之 - 我在使用twisted时遇到的问题:

  1. 我不了解twisted的异步基础知识。我在图形用户界面框架中使用了类似的东西,但很长一段时间没有看到好处。
  2. 其次,我尝试考虑多次同步调用事件循环。在我看来,这是必要的,因为我一次不能使用多于一根传出传真线路。因为twisted的事件循环是不可重启的,所以这是没有选择的。正如我在文档中读到的那样,“deferToThread”可以在这里帮助我,但我认为这不是最好的解决方案。

在我的概念中,我解决了这些问题:

我需要大量的重新思考,但一旦你得到它,它看起来就很容易了。

感谢 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:

  1. I didn't understand the asynchronous basics of twisted. I used something like that in gui-frameworks but didn't see the benefit for a long time.
  2. Second, I tried to think of an synchronous call of the event loop multiple times. This was necessary in my mind because I may not use more than one outgoing fax line at a time. Because the event loop of twisted is not restartable this is no option. As I could read in the docs "deferToThread" could help me here, but I think it's not the best solution.

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.

我ぃ本無心為│何有愛 2024-08-23 05:13:29

你无法重新启动反应堆。换句话说,您只能调用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.

七颜 2024-08-23 05:13:29

如果您仍在寻找解决方案...我也遇到了同样的问题。我有一个使用 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文