[Twisted-web] using threads in twisted

Glyph Lefkowitz glyph at twistedmatrix.com
Tue Apr 19 14:01:10 EDT 2011

On Apr 19, 2011, at 4:43 AM, Phil Mayers wrote:

> On 04/18/2011 11:36 AM, Ivan wrote:
>> Thanks, but how to kill workers by timeout in this example ?
> You can't kill threads reliably in python AIUI.

You can't kill threads in _any_ language reliably.  There are certain special cases where you can sort of make it work in some specific C programs, but for more information read this explanation of why it's not possible to implement a generally correct 'stop this thread' API, see here: <http://download.oracle.com/javase/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html>.

> If you want to reliably kill a task, spawn a process.

Yes, that's how you do it.

> Alternatively you might see if you can break the task up into small 
> pieces of work, and schedule it in the mainloop using something like 
> task.cooperator.

If the task is doing something CPU intensive, a subprocess will let you take advantage of multiple cores as well, though.  If the task is small and cheap though, and just needs to be performed periodically cooperator lets you avoid the overhead of both processes and threads.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20110419/4b551f48/attachment.htm 

More information about the Twisted-web mailing list