在twisted中使用我自己的主循环

发布于 2024-09-10 12:49:04 字数 311 浏览 6 评论 0原文

我有一个现有的程序,它有自己的主循环,并根据它收到的输入进行计算 - 比如说来自用户的输入,以使其简单。我现在想远程而不是本地进行计算,因此我决定在 Twisted 中实现 RPC。

理想情况下,我只想更改我的函数之一,例如 doComputation(),以调用 twins 来执行 RPC、获取结果并返回。程序的其余部分应保持不变。但我怎样才能做到这一点呢?当我调用reactor.run()时,Twisted 劫持了主循环。我还读到,您实际上并没有扭曲的线程,所有任务都按顺序运行,所以看来我不能只创建一个 LoopingCall 并以这种方式运行我的主循环。

I have an existing program that has its own main loop, and does computations based on input it receives - let's say from the user, to make it simple. I want to now do the computations remotely instead of locally, and I decided to implement the RPCs in Twisted.

Ideally I just want to change one of my functions, say doComputation(), to make a call to twisted to perform the RPC, get the results, and return. The rest of the program should stay the same. How can I accomplish this, though? Twisted hijacks the main loop when I call reactor.run(). I also read that you don't really have threads in twisted, that all the tasks run in sequence, so it seems I can't just create a LoopingCall and run my main loop that way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

阳光①夏 2024-09-17 12:49:04

您有几个不同的选项,具体取决于您现有程序的主循环类型。

如果它是来自 GUI 库的主循环,Twisted 可能已经支持它< /a>.在这种情况下,您可以继续使用它。

您也可以编写自己的反应器。这方面没有很多很棒的文档,但是您可以查看 qtreactor 实现的方式 Twisted 外部的反应器插件。

您还可以使用threadedselectreactor编写一个最小的反应器。这方面的文档也很少,但是 wxpythonreactor就是使用它实现的。就我个人而言,我不推荐这种方法,因为它很难测试,并且可能会导致混乱的竞争条件,但它确实有一个优点,即让您只需很少的时间就可以利用几乎所有 Twisted 的默认网络代码。包裹层。

如果您确实不希望 doComputation 是异步的,并且希望程序在等待 Twisted 应答时阻塞,请执行以下操作:

  • 在主线程之前在另一个线程中启动 Twisted循环启动,类似 twistedThread = Thread(target=reactor.run); twistedThread.start()
  • 实例化一个对象来在您自己的主循环线程中进行 RPC 通信(比方说,RPCDoer),以便您可以引用它。确保使用 < 实际启动其 Twisted 逻辑code>reactor.callFromThread 因此您不需要包装其所有 Twisted API 调用。
  • 实现 RPCDoer.doRPC 来返回 延迟,仅使用 Twisted API 调用(即不调用现有的应用程序代码,因此您无需担心应用程序对象的线程安全;将 doRPC 全部传递给它需要作为参数的信息)。
  • 您现在可以像这样实现doComputation

    def doComputation(self):
        rpcResult =blockingCallFromThread(reactor, self.myRPCDoer.doRPC)
        返回 self.computeSomethingFrom(rpcResult)
    
  • 记住调用 reactor.callFromThread(reactor.stop); twistedThread.join() 从你的主循环的关闭过程中,否则你可能会在退出时看到一些令人困惑的回溯或日志消息。

最后,您应该真正考虑一个选项,尤其是从长远来看:转储现有的主循环,并找出一种仅使用 Twisted 的方法。根据我的经验,对于提出此类问题的十分之九的人来说,这是正确的答案。我并不是说这总是要走的路 - 在很多情况下您确实需要保留自己的主循环,或者需要付出太多的努力才能摆脱现有循环。但是,维护自己的循环也是工作。请记住,Twisted 循环已经过数百万用户的广泛测试,并在各种各样的环境中使用。如果您的循环也非常成熟,那可能没什么大不了的,但如果您正在编写一个小型的新程序,那么可靠性方面的差异可能会很大。

You have a couple of different options, depending on what sort of main loop your existing program has.

If it's a mainloop from a GUI library, Twisted may already have support for it. In that case, you can just go ahead and use it.

You could also write your own reactor. There isn't a lot of great documentation for this, but you can look at the way that qtreactor implements a reactor plugin externally to Twisted.

You can also write a minimal reactor using threadedselectreactor. The documentation for this is also sparse, but the wxpython reactor is implemented using it. Personally I wouldn't recommend this approach as it is difficult to test and may result in confusing race conditions, but it does have the advantage of letting you leverage almost all of Twisted's default networking code with only a thin layer of wrapping.

If you are really sure that you don't want your doComputation to be asynchronous, and you want your program to block while waiting for Twisted to answer, do the following:

  • start Twisted in another thread before your main loop starts up, with something like twistedThread = Thread(target=reactor.run); twistedThread.start()
  • instantiate an object to do your RPC communication (let's say, RPCDoer) in your own main loop's thread, so that you have a reference to it. Make sure to actually kick off its Twisted logic with reactor.callFromThread so you don't need to wrap all of its Twisted API calls.
  • Implement RPCDoer.doRPC to return a Deferred, using only Twisted API calls (i.e. don't call into your existing application code, so you don't need to worry about thread safety for your application objects; pass doRPC all the information that it needs as arguments).
  • You can now implement doComputation like this:

    def doComputation(self):
        rpcResult = blockingCallFromThread(reactor, self.myRPCDoer.doRPC)
        return self.computeSomethingFrom(rpcResult)
    
  • Remember to call reactor.callFromThread(reactor.stop); twistedThread.join() from your main-loop's shutdown procedure, otherwise you may see some confusing tracebacks or log messages on exit.

Finally, one option that you should really consider, especially in the long term: dump your existing main loop, and figure out a way to just use Twisted's. In my experience this is the right answer for 9 out of 10 askers of questions like this. I'm not saying that this is always the way to go - there are plenty of cases where you really need to keep your own main loop, or where it's just way too much effort to get rid of the existing loop. But, maintaining your own loop is work too. Keep in mind that the Twisted loop has been extensively tested by millions of users and used in a huge variety of environments. If your loop is also extremely mature, that may not be a big deal, but if you're writing a small, new program, the difference in reliability may be significant.

素食主义者 2024-09-17 12:49:04

这里似乎正确且非常简单的答案是 LoopingCall:

http://www.saltycrane.com/blog/2008/10/running-functions-periodically-using-twisteds-loopingcall/

from datetime import datetime
from twisted.internet.task import LoopingCall
from twisted.internet import reactor

def doComputation():
    print "Custom fn run at", datetime.now()

lc = LoopingCall(doComputation)
lc.start(0.1)  # run your own loop 10 times a second

# put your other twisted here

reactor.run()

It seems like the correct and very simple answer here is a LoopingCall:

http://www.saltycrane.com/blog/2008/10/running-functions-periodically-using-twisteds-loopingcall/

from datetime import datetime
from twisted.internet.task import LoopingCall
from twisted.internet import reactor

def doComputation():
    print "Custom fn run at", datetime.now()

lc = LoopingCall(doComputation)
lc.start(0.1)  # run your own loop 10 times a second

# put your other twisted here

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