Skip to content

Commit

Permalink
Add example on_commit_task to django example code.
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Mar 24, 2023
1 parent 4093cdb commit 6e7cd48
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions examples/django_ex/djangoex/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
},
}

DATABASES = {'default': {
'NAME': ':memory:',
'ENGINE': 'django.db.backends.sqlite3'}}

SECRET_KEY = 'foo'
17 changes: 16 additions & 1 deletion examples/django_ex/djangoex/test_app/tasks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time

from huey import crontab
from huey.contrib.djhuey import task, periodic_task, db_task
from huey.contrib.djhuey import task, periodic_task, db_task, on_commit_task


def tprint(s, c=32):
Expand Down Expand Up @@ -49,3 +49,18 @@ def every_other_minute():
@periodic_task(crontab(minute='*/5'))
def every_five_mins():
tprint('This task runs every 5 minutes.', 34)


# When this task is called, it will not be enqueued until the active
# transaction commits. If no transaction is active it will enqueue immediately.
# Example:
# with transaction.atomic():
# rh = after_commit('hello!')
# time.sleep(5) # Still not enqueued....
#
# # Now the task is enqueued.
# print(rh.get(True)) # prints "6".
@on_commit_task()
def after_commit(msg):
tprint(msg, 33)
return len(msg)

0 comments on commit 6e7cd48

Please sign in to comment.