[Twisted-Python] Correct pattern to do SSH forwarding in a GUI

glyph at divmod.com glyph at divmod.com
Tue Feb 17 10:13:49 EST 2009

On 04:19 am, luper.rouch at gmail.com wrote:
>My first attempt was to run the reactor in the main thread, but the 
>application was "freezing" when e.g. doing a SELECT with 
>SQLAlchemy/psycopg2 (I guess because psycopg2 just waits for data and 
>blocks the twisted reactor). So I put reactor.run() in a thread and it 
>works, but I'm getting strange issues on some platforms (for example I 
>can't connect at all on *some* OSX computers, getting a "reactor 
>stopping" log from twisted without any further error), and I feel I'm 
>doing the whole thing terribly wrong.

These errors sound like you are making Twisted API calls from threads 
other than the reactor thread by accident.  There's no obvious fix 
except for "don't do that" :).

My suggestion would be to move the SQLAlchemy/psycopg2 into threads, 
with e.g. deferToThread, rather than putting the reactor into a thread. 
Then the reactor won't freeze while the blocking SQL stuff happens.
>So my question is, how to do proper SSH port forwarding with conch in 
>an application that also uses the tunnels not the asynchronous way ? If 
>running the reactor in a thread is OK, are there particular things to 
>watch out for ?

There's no reason that running the reactor in a thread shouldn't work; 
it's just generally tricky (especially if you're working with an 
existing codebase) to completely avoid calling Twisted APIs from other 

More information about the Twisted-Python mailing list