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

- 68,820
- 20
- 127
- 125

- 31,152
- 11
- 87
- 129
-
I am working with pyglet. It's too bad twisted is like this; it seems to make things so much easier, just not what I want to do. You're probably right that I don't need to write my program outside of twisted's reactor.run paradigm, but that doesn't change the fact that it would not be a very elegant solution for me. I'd rather write my own low level comm suite again than deal with that. – shino Nov 14 '10 at 15:19
-
Wait - is there some way to use pyglet reactor so that I am not in an event driven model? I'm not seeing it, but that would be perfect. – shino Nov 14 '10 at 16:40
-
2You can use the Pyglet reactor and then you'll be able to use both Pyglet and Twisted in your program; this way you don't need to do something other than reactor.run(). – Glyph Nov 14 '10 at 17:44
-
I'm guessing that's a no? Why does everyone feel so strongly about enforcing the event-driven paradigm? It's certainly not always best. – shino Nov 14 '10 at 17:47
-
3It *is* always best. There are 2 kinds of programs: programs which are properly event-driven, and programs which are buggy and only event-driven in response to certain kinds of input, and have bugs where they hang and break unexpectedly in certain modes. But, you don't need to agree with me: your question is basically "how can I make my program buggy and slow", and the answer is "call reactor.iterate() periodically". Twisted does not prevent you from writing bad programs :). – Glyph Nov 14 '10 at 17:53
-
Well we'll just have to agree to disagree. When you work on simulations, as I do, then you have user events and simulation events, and also the requirement that the simulation have the most CPU it can get. Twisted prioritizes user events over simulation events, and does this while enforcing its own event loop. It's great for what it does, but it is certainly not as general purpose as is claimed. – shino Nov 14 '10 at 19:23
-
3You may notice that when describing "simulation events", you have to use the word "events", as in "event driven". Twisted does not prioritize user events over simulation events. In fact it does not have any event prioritization mechanism at all. (It would be nice if we did, but it's possible to implement one yourself.) Twisted is perfectly general-purpose. While you may feel free to disagree about a particular implementation technique, please refrain from making factually inaccurate statements about Twisted. – Glyph Nov 14 '10 at 19:51
-
Well we'll just have to agree to disagree. ;) – shino Nov 15 '10 at 01:27
-
I don't think it is a bad idea. Such an interface is from time to time very useful. The OP is asking a very valid question. Many other event loops provide this, for example, qt's `QCoreApplication::processEvents()`, matplotlib's `canvas.flush_events()`, ipython kernel's `kernel.do_one_iteration()`. Also `asyncio` can [do that too](https://stackoverflow.com/questions/29782377/is-it-possible-to-run-only-a-single-step-of-the-asyncio-event-loop). – kawing-chiu Feb 23 '17 at 05:41