0

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:

Is there any way to kill a Thread in Python?

and probably many others.

Community
  • 1
  • 1
hyperboreean
  • 8,273
  • 12
  • 61
  • 97

2 Answers2

4

"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
  • 306
  • 2
  • 6
  • `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 Nov 06 '09 at 22:09
0

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

Jerub
  • 41,746
  • 15
  • 73
  • 90