Global objects on python wsgi server

I am creating a python application that uses a web server to serve the interface, and after accepting the data as a web request, it redirects the task to the job queue, which, together with the main processing function, uses the thread.

But the problem is that the python web server is trying to import itself to start threads, and in this process, a new job queue is created for almost every request, which breaks the logic. Is there a way in which a global variable can be configured on python web servers since the python queue is thread safe, I assume that it will not hurt.

(I tried two web servers Flask and web.py)

+4
source share
1 answer

Shared data is best stored in a database.

If you need a global variable for a user session, you want to save it with the session data: here are the details for web.py and for the flask .

If you have a standing process that processes the queue, you can look at starting your server using FastCGI, where your python server can be run in a separate instance from your web server. See how to configure FastCGI for web.py - see if this works, perhaps. The web server will communicate with the python server through its own port, so your python server can continue to work and support any global data.

[edit]

Since you need to exchange variables - you can check flask.g for a flask or web.ctx for web.py. I never used them, so I don’t know if there are any evil consequences or performance issues. I saw an example here that suggested in web.py to do something like:

import web def add_global_hook(): g = web.storage({"counter": 0}) def _wrapper(handler): web.ctx.globals = g return handler() return _wrapper class Hello: def GET(self): web.ctx.globals.counter += 1 return "<h1>Counter: %d</h1>" % web.ctx.globals.counter if __name__ == '__main__': urls = ("/", "Hello") app = web.application(urls, globals()) app.add_processor(add_global_hook()) app.run() 
-2
source

All Articles