on_stop_thread_list thread_data with thread call

Anonymous avatarAnonymous created an issue

I have problem with mysqldb with autoreloader. After long time it says "Too much connections". I connect one connection per thread. I try to close connection in stop_thread, but it doesn't work, because thread_data doesn't have any attributes. Am I doing something wrong? Test:

def threadStart(threadIndex):
    print "Setting loaded for:", threadIndex
    cherrypy.thread_data.loaded = 1

def threadStop(threadIndex):
    print "Printing loaded for:", threadIndex
    print cherrypy.thread_data.loaded

Reported by kolda@lss.fd.cvut.cz

Comments (6)

  1. Robert Brewer

    The on_stop_thread_list callbacks are called by the main thread (or whatever thread calls engine.stop), not by each thread. This is in fact why the thread index is passed to each on_stop callable; so your callbacks can store and look up the necessary resources by thread id. In your example, you should keep, for example, a dict of conns = {threadIndex: conn} pairs, and call conns[threadIndex].close() inside threadStop.

    This is necessary because Python provides no mechanism to call finalization functions when a thread stops; instead, the on_stop_thread functions are all called when engine.stop is called. :/

  2. Anonymous

    Thank you for your explanation. And is it possible to change that in future versions? I'm mainly C++ programmer and also use just beginthread function. But if you wrap it in class like:

    class CherrypyThread(threading.thread):
        def __init__(self, startlist, stoplist):
            self.startlist = startlist
            self.stoplist = stoplist
        def run(self):
            for f in startlist:
                f(self) #or something like in cherrypy
            for f in stoplist:
                f(self) #or something like in cherrypy
        def runInner(self):
            # Main rutine with request processing until stop of engine

    It is just idea written in browser so sorry for syntax errors and nonsense... But I think you see what I mean. I didn't see internals of cherrypy (I tried, but I'm beginner in cherrypy API), but I thing there is something similar to conditional variable that wait for request and for end. After that it returns and process stopthread functions.

    That you for your time.


  3. Robert Brewer

    That's an extension you're welcome to use on your own, but it won't be a part of core CherryPy, since CP is designed to be used with a wide variety of HTTP servers doing the thread creation. This includes, for example, allowing Apache to create lighter-weight threads than Python does. Since one of the intended strengths of CherryPy is portability among HTTP servers, having an on_stop_thread mechanism that only fulfilled its contract when using a server in the CP distro would be counter-productive and confusing; a better, truly portable solution is to put your thread-sensitive resources behind a Queue and send a stop message to your DB conn across the Queue.

  4. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.