{"id":502,"date":"2018-02-09T00:15:18","date_gmt":"2018-02-08T16:15:18","guid":{"rendered":"https:\/\/vinta.ws\/code\/?p=502"},"modified":"2026-02-18T01:20:35","modified_gmt":"2026-02-17T17:20:35","slug":"run-a-celery-task-at-a-specific-time","status":"publish","type":"post","link":"https:\/\/vinta.ws\/code\/run-a-celery-task-at-a-specific-time.html","title":{"rendered":"Run a Celery task at a specific time"},"content":{"rendered":"<h2>Schedule Tasks<\/h2>\n<p>You are able to run any Celery task at a specific time through <code>eta<\/code> (means &quot;Estimated Time of Arrival&quot;) parameter.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-py\">import datetime\n\nimport celery\n\n@celery.shared_task(bind=True)\ndef add_tag(task, user_id, tag):\n    User.objects.filter(id=user_id, tags__ne=tag).update(push__tags=tag)\n    return True\n\nuser_id = '582ee32a5b9c861c87dc297e'\ntag = 'new_tag'\nstarted_at = datetime.datetime(2018, 3, 12, tzinfo=datetime.timezone.utc)\nadd_tag.apply_async((user_id, tag), eta=started_at)<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"http:\/\/docs.celeryproject.org\/en\/master\/userguide\/calling.html#eta-and-countdown\">http:\/\/docs.celeryproject.org\/en\/master\/userguide\/calling.html#eta-and-countdown<\/a><\/p>\n<h2>Revoke Tasks<\/h2>\n<p>Revoked tasks will be discarded until their eta.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-py\">from celery.result import AsyncResult\n\nAsyncResult(task_id).revoke()<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"http:\/\/docs.celeryproject.org\/en\/latest\/reference\/celery.result.html#celery.result.AsyncResult.revoke\">http:\/\/docs.celeryproject.org\/en\/latest\/reference\/celery.result.html#celery.result.AsyncResult.revoke<\/a><\/p>\n<p>Revoking tasks works by sending a broadcast message to all the workers, the workers then keep a list of revoked tasks in memory. When a worker starts up it will synchronize revoked tasks with other workers in the cluster.<\/p>\n<p>The list of revoked tasks is in-memory so if all workers restart the list of revoked ids will also vanish. If you want to preserve this list between restarts you need to specify a file for these to be stored in by using the <code>\u2013statedb<\/code> argument to <code>celery worker<\/code>.<\/p>\n<p>ref:<br \/>\n<a href=\"http:\/\/docs.celeryproject.org\/en\/latest\/userguide\/workers.html#worker-persistent-revokes\">http:\/\/docs.celeryproject.org\/en\/latest\/userguide\/workers.html#worker-persistent-revokes<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are able to run any Celery task at a specific time through eta (means \"Estimated Time of Arrival\") parameter.<\/p>\n","protected":false},"author":1,"featured_media":503,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,116],"tags":[37,2],"class_list":["post-502","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-about-python","category-about-web-development","tag-celery","tag-python"],"_links":{"self":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/comments?post=502"}],"version-history":[{"count":0,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/502\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media\/503"}],"wp:attachment":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media?parent=502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/categories?post=502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/tags?post=502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}