0

The purpose is to implement a pool like database connection pool in my web application. My application is write by Django.

The problem is that every time a http request come, my code will be loaded and run through. So if I write some code to initiate a pool. These code will be run per http request. And the pool will be initiated per request. So it is meaningless.

So how should I write this?

Mingwei Li
  • 11
  • 2
  • How are you deploying your Django project? uWSGI? Gunicorn? Something else? – Blender Nov 08 '17 at 06:50
  • does it matter? It will be deployed by uWSGI and nginx – Mingwei Li Nov 08 '17 at 06:59
  • uWSGI spins up an interpreter for each worker. You can create per-worker connection pools by just keeping them outside of the request pipeline, for example as a module-level global. – Blender Nov 08 '17 at 07:09

1 Answers1

0

Your understanding of how things work is wrong, unfortunately. The way Django runs is very much dependent on the way you are deploying it, but in almost all circumstances it does not load code or initiate globals on every request. Certainly, uWSGI does not behave that way; it runs a set of long-lived workers that persist across many requests.

In effect, uWSGI is already a connection pool. In other words, you are trying to solve a problem that does not exist.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895