Den 10/02/2015 kl. 21.27 skrev Erik Cederstrand <erik+li...@cederstrand.dk>: > > Running this in parallel in two processes on the same machine returns this > after a while: > > Process A: > [...] > > Process B: > [...] > Traceback (most recent call last): > File "tmp.py", line 30, in <module> > assert cache.add(key='foo', value='bar') > AssertionError > > > I don't see how this is possible when I'm using transaction.atomic().
Phew. This situation is actually possible since "Read Committed" is the default isolation level in PostgreSQL, which means that non-repeatable reads are possible within a transaction. My code relies on isolation level "Serializable" for a cache.get() followed by cache.add() to be reliable, but Django uses the default from PostgreSQL. The isolation level is supposed to be configurable with the 'isolation_level' setting in OPTIONS (https://docs.djangoproject.com/en/1.7/ref/databases/#isolation-level). Except it doesn't work because django/db/backends/postgresql_psycopg2/base.py::_set_isolation_level() is never called anywhere, AFAICS. I tried disabling autocommit (the docs are wrong BTW, https://docs.djangoproject.com/en/1.7/ref/databases/#autocommit-mode says to put it in OPTIONS, but django/db/backends/__init__.py (line 123) looks at settings_dict['AUTOCOMMIT'], not in OPTIONS) and hacking postgresql_psycopg2/base.py::_set_autocommit() to call _set_isolation_level(), but now process A blocks process B entirely, even when process A is outside the atomic() context manager. Has anyone got isolation_level "Serializable" to work in Django 1.7? Thanks, Erik -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/12300B76-606A-4977-A402-990D562C7F77%40cederstrand.dk. For more options, visit https://groups.google.com/d/optout.