Avoiding django QuerySet caching in a @staticmethod [duplicate]
The following few lines of code illustrate a distributed worker model that I use to crunch data. Jobs are being created in a database, their data goes onto the big drives, and once all information is available, the job status is set to 'WAITING'. From here, multiple active workers come into play: from time to time each of them issues a query, in which it attempts to "claim" a job. In order to synchronize the claims, the queries are encapsulated into a transaction that immediately changes the job state if the query returns a candidate. So far so good.
The problem is that the call to claim only works the first time. Reading up on QuerySets and their caching behavior, it seems to me that combining static methods and QuerySet caching always falls back on the cache... see for yourselves:
I have a class derived from django.db.models.Model:
class Job(models.Model):
[...]
in which I define the following static function.
@staticmethod
@transaction.commit_on_success
def claim():
# select the oldest, top priority job and
# update its record
jobs = Job.objects.filter(state__exact = 'WAITING').order_by('-priority', 'create_timestamp')
if jobs.count() > 0:
j = jobs[0]
j.state = 'CLAIMED'
j.save()
logger.info('Job::claim: claimed %s' % j.name)
return j
return None
Is there any obvio开发者_如何学JAVAus thing that I am doing wrong? What would be a better way of dealing with this? How can I make sure that the QuerySet does not cache its results across different invocations of the static method? Or am I missing something and chasing a phantom? Any help would be greatly appreciated... Thanks!
Why not just have a plain module-level function claim_jobs()
that would run the query?
def claim_jobs():
jobs = Job.objects.filter(...)
... etc.
精彩评论