[Twisted-Python] Re: twisted and threading

Patrick Mézard patrick.mezard at ifrance.com
Fri Nov 26 02:49:15 EST 2004


> Sorry this is from long ago, possibly the OP has found a solution by now, but I 
> thought I'd share my approach in case it was of more general interest...
> 
> I have hit the same problem recently (C library calls that might hang, and I 
> want to give up and quit regardless in the end). What I came up with was a 
> function deferToDaemonThread, which put the call in a daemon thread (one that 
> the Python main thread will not wait for). It steals a lot of code from 
> deferToThread, but creates a dedicated daemon thread for the call rather than 
> using the thread pool.

Have you no way to terminate the blocking call from the C library itself 
? For instance, if your working python thread is waiting on a mutex in 
the C library, destroying the mutex (or something else depending on the 
threading API) may release it.

In this case, you can add a cleanup function to the library cancelling 
all blocking calls. And call it during the reactor shutdown sequence (I 
do not know where exactly, I am not an expert in this area yet, but I 
remember there are tons of handlers everywhere to perform cleanup. I 
hope there are some called before threads shutdown attempts). It means 
that you actually need a thread-pool but can terminate the blocking 
operations safely.

Or am I missing something ?

Patrick Mézard





More information about the Twisted-Python mailing list