I believe I'm (at least partly) responsible for this change, and the short
answer is basically "historically memcache client libs have been awful with
This may have improved, but in order to avoid a whole slew of potential
problems (with memcache and other backbends) I copied the pattern used by
That said, I like your idea of sharing the connection pool between threads. It
would mean at worst we'd wind up where we are.
On 8 March 2018 06:53:57 GMT+11:00, Nicolas Le Manchet <nico...@lemanchet.fr>
>Thanks to commit f02dbbe1ae, the Memcached cache backend using
>pylibmc can now keep connections open between requests. Establishing a
>new TCP connection is rather expensive and each saved round trips to
>the cache server allows to shave a few ms of response time.
>It appears that in a multithreaded environment we could improve the
>situation even more by letting threads share the same `PyLibMCCache`
>Currently in Django, each thread asking for a cache backend gets its
>own personal Backend object, thus each thread also get its own
>connection pool to memcached. After a few requests the process ends up
>opening as many connections to memcached as there are threads.
>If instead we allowed the connection pool to be shared between threads,
>connections would only be opened when necessary (other threads using
>all the pooled connections).
>Now the important questions. Why do we have thread locals in the first
>place? Can we share Backend instances between threads?
>After looking at the code of all cache backends I feel that nothing
>prevents dropping threadlocal altogether. Did I miss something?
>Nicolas Le Manchet
>You received this message because you are subscribed to the Google
>Groups "Django developers (Contributions to Django itself)" group.
>To unsubscribe from this group and stop receiving emails from it, send
>an email to django-developers+unsubscr...@googlegroups.com.
>To post to this group, send email to
>Visit this group at https://groups.google.com/group/django-developers.
>To view this discussion on the web visit
>For more options, visit https://groups.google.com/d/optout.
Sent from my Android device with K-9 Mail. Please excuse my brevity.
You received this message because you are subscribed to the Google Groups
"Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email
To post to this group, send email to firstname.lastname@example.org.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit
For more options, visit https://groups.google.com/d/optout.