What is the best way to control Twisted's reactor so that it is nonblocking?

柔情痞子 提交于 2019-12-13 13:04:37

问题


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:


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



来源:https://stackoverflow.com/questions/4176405/what-is-the-best-way-to-control-twisteds-reactor-so-that-it-is-nonblocking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!