[Twisted-Python] reactor.run() do not return after reactor.stop()
Hi everyone, I am having a problem with current twisted trunk (rev 27375). I am trying to implement simple ssh client with conch.ssh but right now my implementation works wrong way. I've made a wrapper class for my client factory, to separate host/creds information and command I want to execute. So in execute method of the wrapper I am calling reactor.run() - (that blocks execute method) and in one of callbacks for success/error calling reactor.stop(). So the problem is that the first call to execute returns from reactor.run() right after reactor.stop(), but second call to same method of same instance do not return ever. I believe I am doing something wrong, so please help with find out what I've missed. Problem code attached.
Stanislav Yudin wrote:
Hi everyone, I am having a problem with current twisted trunk (rev 27375). ... So the problem is that the first call to execute returns from reactor.run() right after reactor.stop(), but second call to same method of same instance do not return ever. I believe I am doing something wrong, so please help with find out what I've missed. Problem code attached. http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#WhycanttheTwiste...
But since you can do everything without restarting the reactor, that's no real limitation. hth, Johann
Thanks, but how can I pass control back to calling code after starting the reactor? Or the idea is to run it in separate thread? I understand that main idea on twisted is async processing, but this time I need to block the calling code until response is received. On Sat, Oct 10, 2009 at 6:10 PM, Johann Borck <johann.borck@densedata.com>wrote:
Stanislav Yudin wrote:
Hi everyone, I am having a problem with current twisted trunk (rev 27375). ... So the problem is that the first call to execute returns from reactor.run() right after reactor.stop(), but second call to same method of same instance do not return ever. I believe I am doing something wrong, so please help with find out what I've missed. Problem code attached.
http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#WhycanttheTwiste...
But since you can do everything without restarting the reactor, that's no real limitation.
hth, Johann
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Stanislav Yudin wrote:
Thanks, but how can I pass control back to calling code after starting the reactor? Or the idea is to run it in separate thread? I understand that main idea on twisted is async processing, but this time I need to block the calling code until response is received.
You already have everything set up, make your SimpleClient._on_done callback aware of what has to happen next, and if you don't know beforehand, maybe because the next thing to do depends on the result of the current, you can pass a callable to the code in charge of that decision. The only difference is that you have to break the currently blocking code into several callables. hth, Johann
On Sat, Oct 10, 2009 at 12:50 PM, Stanislav Yudin <decvar@gmail.com> wrote:
Thanks, but how can I pass control back to calling code after starting the reactor? Or the idea is to run it in separate thread? I understand that main idea on twisted is async processing, but this time I need to block the calling code until response is received.
The way that you want to do this is to run all of your Twisted code in a thread, and make calls into it from a different thread using one of the utility APIs for that purpose. See, for example, < http://twistedmatrix.com/documents/8.2.0/api/twisted.internet.threads.html#b...>. Remember not to touch any Twisted APIs directly from the non-Twisted thread. Leave the Twisted I/O thread running for the entire lifetime of your process; don't start and stop it repeatedly. For what it's worth, you should really look into restructuring your application so that this kind of hack is not necessary. It will almost certainly have hard-to-debug unforseen consequences. There's nothing fundamentally broken here, but it is much harder to keep track of and get right than you think it will be, just based on the fact that many many people who try this strategy end up giving up on it.
On 04:50 pm, decvar@gmail.com wrote:
Thanks, but how can I pass control back to calling code after starting the reactor? Or the idea is to run it in separate thread? I understand that main idea on twisted is async processing, but this time I need to block the calling code until response is received.
No, you don't need to block the calling code until the response is received. The main idea on Twisted is async processing. :) Jean-Paul
participants (4)
-
exarkun@twistedmatrix.com
-
Glyph Lefkowitz
-
Johann Borck
-
Stanislav Yudin