How does cherrypy handle user threads?

I am working on django right and I am using cherrypy as a server. Cherrypy creates a new stream for each page view. I would like to have access to all of these threads (the threads responsible for communicating with django) from any of them. More specifically, I would like to have access to thread_data for each of these threads from any of them. Is it possible? If so, how to do it?

+4
source share
2 answers

CherryPy wsgiserver does not create a new thread for each request - it uses a pool. Each of these worker threads is a subclass of threading.Thread, so they should all be accessible through threading.enumerate ().

However, if you are talking specifically about cherrypy.thread_data, this is something else: threading.local. If you use the latest version of Python, then everything that is encoded in C and you (probably rightfully) do not have end-to-end access to it with Python. If you really need it and really know what you are doing, the best way is usually to stick to additional links to such things in the global container at the same time when they are inserted into the thread_data structure, I recommend dicts with weakrefs as keys for these global containers - there are enough Python ORMs that use them for connection pools (like my own Geniusql) so you can learn how to implement them quite easily.

+6
source

My first answer to this question is not to tell you how to do this, but to emphasize what you really need to review before moving forward. I usually shy away from streaming web servers in favor of multiprocessor or asynchronous solutions. Adding explicit cross-threading to the mix only increases these fears.

When asked such a question, there is a deeper goal. I suspect that what, in your opinion, can be solved by inter-threaded communication, may be resolved in some other, more secure way.

+4
source

Source: https://habr.com/ru/post/1277199/


All Articles