使用我自己的主循环扭曲 [英] Use my own main loop in twisted

查看:24
本文介绍了使用我自己的主循环扭曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

推荐答案

根据现有程序的主循环类型,您有几个不同的选择.

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

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

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.

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

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.

您还可以使用 threadedselectreactor 编写一个最小的反应器.这方面的文档也很少,但是 wxpython reactor 就是用它来实现的.我个人不推荐这种方法,因为它很难测试并且可能会导致混乱的竞争条件,但它确实具有让您利用几乎所有 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.

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

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:

  • 在主循环启动之前在另一个线程中启动 Twisted,类似于 twistedThread = Thread(target=reactor.run);TwistedThread.start()
  • 在您自己的主循环线程中实例化一个对象以进行 RPC 通信(例如,RPCDoer),以便您可以引用它.确保使用 <真正启动它的 Twisted 逻辑code>reactor.callFromThread 这样你就不需要包装它的所有 Twisted API 调用.
  • 实施 RPCDoer.doRPC 以返回 Deferred,仅使用 Twisted API 调用(即不调用您现有的应用程序代码,因此您无需担心应用程序对象的线程安全;通过 doRPC 需要作为参数的所有信息).
  • 您现在可以像这样实现 doComputation:

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

  • 记得调用reactor.callFromThread(reactor.stop);twinedThread.join() 来自主循环的关闭过程,否则您可能会在退出时看到一些令人困惑的回溯或日志消息.
  • 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.
  • 最后,您应该真正考虑一个选项,尤其是从长远来看:转储现有的主循环,并找出一种仅使用 Twisted 的方法.根据我的经验,对于 10 位提问者中的 9 位这样的问题,这是正确的答案.我并不是说这总是要走的路 - 在很多情况下,您确实需要保留自己的主循环,或者只是为了摆脱主循环而付出了太多努力现有循环.但是,维护自己的循环也是可行的.请记住,Twisted loop 已经过数百万用户的广泛测试,并在各种环境中使用.如果您的循环也非常成熟,那可能没什么大不了的,但如果您正在编写一个小型的新程序,可靠性方面的差异可能会很大.

    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.

    这篇关于使用我自己的主循环扭曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆