From b9c88c78827b848bdfaa5310f7c08b66437124fe Mon Sep 17 00:00:00 2001
From: Sebastian Noack <sebastian.noack@gmail.com>
Date: Fri, 12 Oct 2012 10:00:16 +0200
Subject: [PATCH] Re-connect database and memcached after fork.
---
django/core/cache/backends/memcached.py | 5 +++++
django/db/backends/__init__.py | 10 ++++++++++
2 files changed, 15 insertions(+)
diff --git a/django/core/cache/backends/memcached.py b/django/core/cache/backends/memcached.py
index 9bb47c8..b8e3fa7 100644
a
|
b
|
|
2 | 2 | |
3 | 3 | import time |
4 | 4 | from threading import local |
| 5 | from multiprocessing.util import register_after_fork |
5 | 6 | |
6 | 7 | from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError |
7 | 8 | |
… |
… |
class BaseMemcachedCache(BaseCache):
|
25 | 26 | self._lib = library |
26 | 27 | self._options = params.get('OPTIONS', None) |
27 | 28 | |
| 29 | def _after_fork(cache): |
| 30 | cache._client = None |
| 31 | register_after_fork(self, _after_fork) |
| 32 | |
28 | 33 | @property |
29 | 34 | def _cache(self): |
30 | 35 | """ |
diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
index 02d2a16..e84ca61 100644
a
|
b
|
try:
|
4 | 4 | from django.utils.six.moves import _thread as thread |
5 | 5 | except ImportError: |
6 | 6 | from django.utils.six.moves import _dummy_thread as thread |
| 7 | from multiprocessing.util import register_after_fork |
7 | 8 | from contextlib import contextmanager |
8 | 9 | |
9 | 10 | from django.conf import settings |
… |
… |
class BaseDatabaseWrapper(object):
|
41 | 42 | self._thread_ident = thread.get_ident() |
42 | 43 | self.allow_thread_sharing = allow_thread_sharing |
43 | 44 | |
| 45 | def _after_fork(connection): |
| 46 | connection.close() |
| 47 | |
| 48 | connection.transaction_state = [] |
| 49 | connection.savepoint_state = 0 |
| 50 | connection._dirty = None |
| 51 | connection._thread_ident = thread.get_ident() |
| 52 | register_after_fork(self, _after_fork) |
| 53 | |
44 | 54 | def __eq__(self, other): |
45 | 55 | return self.alias == other.alias |
46 | 56 | |