task-cookbook.rst 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. .. _cookbook-tasks:
  2. ================
  3. Task Cookbook
  4. ================
  5. .. contents::
  6. :local:
  7. .. _cookbook-task-serial:
  8. Ensuring a task is only executed one at a time
  9. ==============================================
  10. You can accomplish this by using a lock.
  11. In this example we'll be using the cache framework to set a lock that is
  12. accessible for all workers.
  13. It's part of an imaginary RSS feed importer called `djangofeeds`.
  14. The task takes a feed URL as a single argument, and imports that feed into
  15. a Django model called `Feed`. We ensure that it's not possible for two or
  16. more workers to import the same feed at the same time by setting a cache key
  17. consisting of the MD5 checksum of the feed URL.
  18. The cache key expires after some time in case something unexpected happens
  19. (you never know, right?)
  20. .. code-block:: python
  21. from celery.task import Task
  22. from django.core.cache import cache
  23. from django.utils.hashcompat import md5_constructor as md5
  24. from djangofeeds.models import Feed
  25. LOCK_EXPIRE = 60 * 5 # Lock expires in 5 minutes
  26. class FeedImporter(Task):
  27. name = "feed.import"
  28. def run(self, feed_url, **kwargs):
  29. logger = self.get_logger(**kwargs)
  30. # The cache key consists of the task name and the MD5 digest
  31. # of the feed URL.
  32. feed_url_digest = md5(feed_url).hexdigest()
  33. lock_id = "%s-lock-%s" % (self.name, feed_url_hexdigest)
  34. # cache.add fails if if the key already exists
  35. acquire_lock = lambda: cache.add(lock_id, "true", LOCK_EXPIRE)
  36. # memcache delete is very slow, but we have to use it to take
  37. # advantage of using add() for atomic locking
  38. release_lock = lambda: cache.delete(lock_id)
  39. logger.debug("Importing feed: %s" % feed_url)
  40. if acquire_lock():
  41. try:
  42. feed = Feed.objects.import_feed(feed_url)
  43. finally:
  44. release_lock()
  45. return feed.url
  46. logger.debug(
  47. "Feed %s is already being imported by another worker" % (
  48. feed_url))
  49. return