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

flupke luper.rouch at gmail.com
Thu Feb 19 05:17:07 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 threads.

Just wanted to thank you, I isolated the Twisted part of my  
application using the 'multiprocessing' module, and all the weird  
issues are gone, it seems I was mixing threads somewhere ...

So executing the reactor in a child process was a good solution for my  
problem, no need to use deferToThread everywhere or to do tedious  
research about what is called from where.

Cheers,
Luper




More information about the Twisted-Python mailing list