How to handle local long-living objects in WSGI env-Collection of common programming errors

WSGI is merely a standardized interface that makes it possible to build the various components of a web-server architecture so that they can talk to each other.

Pyramid is a framework whose components are glued with each other through WSGI.

Pyramid, like other WSGI frameworks, makes it possible to choose the actual server part of the stack, like gunicorn, Apache, or others. That choice is for you to make, and there lies the ultimate answer to your question.

What you need to know is whether your server is multi-threaded or multi-process. In the latter case, it’s not enough to check whether a global variable has been instantiated in order to initialize costly resources, because subsequent requests might end up in separate processes, that don’t share state.

If your model is multi-threaded, then you might indeed rely on global state, but be aware of the fact that you are introducing a strong dependency in your code. Maybe a singleton pattern coupled with dependency-injection can help to keep your code cleaner and more open to change.