On 18 October 2013 13:13, Rasjid Wilcox <[email protected]> wrote:
> The insert into get_data_guard should be completely atomic - it will only
> ever succeed for one caller, and so it should eliminate the race condition.
>
Hmmm. Wonder how this will work with transactions...
Just tried running the following test code:
--- cut ---
#!/usr/bin/env python
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'kgadmin.conf.settings'
import datetime
import time
import django.db.transaction
from karaage.machines.models import Machine
from karaage.cache.models import MachineCache
@django.db.transaction.commit_on_success
def meow():
start=datetime.date(year=2003,month=12,day=25)
end=datetime.date(year=2003,month=12,day=25)
print "meowing"
machine = Machine.objects.get(pk=1)
print "barking"
MachineCache.objects.create(machine=machine, start=start, end=end,
cpu_hours=10, no_jobs=10)
print "sleeping"
time.sleep(10)
print "eating"
raise RuntimeError("oops")
meow()
--- cut ---
If I run one copy, it works fine:
meowing
barking
sleeping
eating
Traceback (most recent call last):
File "./test", line 24, in <module>
meow()
File "/usr/lib/python2.7/dist-packages/django/db/transaction.py", line
223, in inner
return func(*args, **kwargs)
File "./test", line 22, in meow
raise RuntimeError("oops")
RuntimeError: oops
If I run another copy while the first one is sleeping, the 2nd copy gets
locked out until the first one finishes:
meowing
barking
[ hangs here ]
sleeping
eating
Traceback (most recent call last):
File "./test", line 24, in <module>
meow()
File "/usr/lib/python2.7/dist-packages/django/db/transaction.py", line
223, in inner
return func(*args, **kwargs)
File "./test", line 22, in meow
raise RuntimeError("oops")
RuntimeError: oops
I wasn't aware a transaction could lock out other processes like this.
Something to be careful of when doing http requests that should return
quickly.
Would be interested to know just what is happening here, and what the scope
is of this "lock".
(also note this is Django 1.5, apparently transactions support is different
in 1.6; I don't really understand what has changed however).
Will continue looking at this tomorrow.
--
Brian May <[email protected]>
_______________________________________________
melbourne-pug mailing list
[email protected]
https://mail.python.org/mailman/listinfo/melbourne-pug