Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How would one use this progress bar when a task is wrapped in a Django transaction.on_commit? #84

Open
pmi123 opened this issue May 30, 2021 · 4 comments

Comments

@pmi123
Copy link

pmi123 commented May 30, 2021

Your usage notes for the view depends on the task returning a task_id:

def progress_view(request):
    result = my_task.delay(10)
    return render(request, 'display_progress.html', context={'task_id': result.task_id})

but a transaction.on_commit does not return a task_id, since the task may not have started yet. Any suggestions on how to use your progress bar with transactions?

Thanks!
Mark

@OmarWKH
Copy link
Collaborator

OmarWKH commented May 30, 2021

To overcome this, I gave the task a custom id.

@pmi123
Copy link
Author

pmi123 commented May 30, 2021

Could you elaborate a little more on how you got it to work?

Thanks!

Mark

@OmarWKH
Copy link
Collaborator

OmarWKH commented May 31, 2021

I didn't run this code. But it gives you the general idea.

# pre-generate a random id
custom_id = celery.utils.gen_unique_id()

# set task id to custom id
task_function = lambda: my_task.apply_async(args, task_id=custom_id)

# instead of passing my_task.apply_async directly, it's wrapped in a lambda function so we can give it arguments
transaction.on_commit(task_function)

@pmi123
Copy link
Author

pmi123 commented May 31, 2021

Thanks!

Giving a task a custom task_id is a new concept for me. While we were talking, I implemented my progress bar using my Django site's MySQL db and uuid.uuid4().hex to generate a unique ID outside of the celery task, but I will take a look at using this approach when I have some free time. Yours will certainly be more efficient than periodically hitting the db to get the progress amount in my view. But, it does work!

Mark

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants