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 ser开发者_如何学Pythonver 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.
"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.
You probably just want to do mythread.setDaemon(True)
so that your threads exit when the main process exits.
精彩评论