[Twisted-Python] twisted and threading

Jp Calderone exarkun at divmod.com
Tue Aug 17 23:18:59 EDT 2004


twisted-10152003 at atlas.lcs.mit.edu wrote:
> Christopher Armstrong [radeex at gmail.com] wrote:
> 
>>On Tue, 17 Aug 2004 20:34:44 -0400, twisted-10152003 at atlas.lcs.mit.edu
>><twisted-10152003 at atlas.lcs.mit.edu> wrote:
>>
>>>I wrapped a c library using swig and was hoping to make use of it in
>>>a twisted server I am writing.  The issue is that the c library
>>>fundamentally blocks waiting for input that may never come.  Is
>>>there a way from twisted to deal with this cleanly within the
>>>framework?
>>>
>>>I.e. some combination of adding a reactor.addSystemEventTrigger event
>>>and calling reactor.threadpool.stop seem like it should work but
>>>I haven't had any luck yet.
>>
>>
>>Try twisted.internet.threads.deferToThread(myBlockingCall) -->
>>Deferred.
> 
> 
> Ah, but that doesn't work.  As I stated the c library (not mine) calls
> potentially block forever (or at least longer than I want to wait to
> shutdown the twisted server).  The thread created by deferTothread may
> never complete which makes shutting down the server gracefully
> impossible (or so I thought?).
> 

   There is no portable way to terminate a thread without its 
assistance.  Python supports no API for this - save one, the 
ridiculously named "setDaemon" Thread method.  Twisted doesn't expose 
this, nor call it internally, as it can lead to segfaults.

   Perhaps this should be parameterizable (defaulting to the current 
behavior, of course), so that poorly behaved libraries can be dealt with?

   Alternatively, since daemonized threads might lead to this anyway, 
perhaps you should just add a shutdown event os.kill(os.getpid(), 9) 
<wwinkink>.

   Jp




More information about the Twisted-Python mailing list