hyperboreean
hyperboreean

Reputation: 8343

Problem with Twisted and threads

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.

Upvotes: 0

Views: 1325

Answers (2)

Jerub
Jerub

Reputation: 42648

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

Upvotes: 0

maciejka
maciejka

Reputation: 306

"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.

Upvotes: 4

Related Questions