views:

311

answers:

2

Some of you that are more experienced using Twisted will probably judge me about using it together with threads - but I did it :). And now I am in somehow of a trouble - I am having an application server that listens for client requests and each time a new client connects it spawns another thread that I probably forget to properly close, since after a while of heavy usage the server stops processing requests. Well, I have 3 different types of threads and for one of those it happens - the thing is that I am not sure what's the proper way to do it, since Thread.join() seems to not work and doing cat /proc/<pid>/status it always gives me Threads: 43 when the server stopped working.

So I am looking for a way of debugging this and see how can I properly close the threads.

And yeah, I know about this question:

http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python

and probably many others.

+4  A: 

"Twisted way" to do anything outside reactor loop (aka spawning threads) is twisted.internet.threads.deferToThread.

For example:

from twisted.internet import threads

def sthToDoInSeparateThread():
    return 3

d = threads.deferToThread(sthToDoInSeparateThread)

deferToThread will execute sthToDoInSeparateThread in separate thread and fire returned defered d as soon as thread is stopped.

maciejka
`deferToThread` actually uses a thread pool. So the returned Deferred fires when `sthToDoInSeparateThread` returns (or raises an exception), but the thread it ran in doesn't stop, it is kept around to run the next function.
Jean-Paul Calderone
A: 

You probably just want to do mythread.setDaemon(True) so that your threads exit when the main process exits.

Jerub