What is the best way to control Twisted's reac

2019-06-18 23:20发布

Instead of running reactor.run(), I'd like to call something else (I dunno, like reactor.runOnce() or something) occasionally while maintaining my own main loop. Is there a best-practice for this with twisted?

1条回答
该账号已被封号
2楼-- · 2019-06-18 23:45

Yes. The best practice is that this is a bad idea, and that you never really need to do it. It doesn't work with all reactors, and you certainly can't have two different libraries which want to do this.

Why do you need to maintain your own main loop? Chances are, it's something like "I want to work with PyGame" or "I am writing a GUI program and I want to use GTK's mainloop" or "I'm using Twisted from within Blender and it has its own event-handling". If this is the case, you should ask that specific question, because each one of those has its own answer.

If you absolutely need to do this (and, again: you don't) the way to do it is to call reactor.iterate() periodically. This will be slow, break signal handling, and have wonky semantics with respect to reactor.stop(). It will introduce lots of bugs into your program that wouldn't otherwise be there, and when you need help diagnosing them, if you ask someone on the Twisted dev team, the first thing they will tell you is "stop doing that, you don't need to do it".

查看更多
登录 后发表回答