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

Awaitable task result #6603

Open
4 tasks done
bblanchon opened this issue Jan 18, 2021 · 6 comments
Open
4 tasks done

Awaitable task result #6603

bblanchon opened this issue Jan 18, 2021 · 6 comments

Comments

@bblanchon
Copy link

bblanchon commented Jan 18, 2021

Checklist

  • I have checked the issues list
    for similar or identical feature requests.
  • I have checked the pull requests list
    for existing proposed implementations of this feature.
  • I have checked the commit log
    to find out if the if the same feature was already implemented in the
    master branch.
  • I have included all related issues and possible duplicate issues
    in this issue (If there are none, check this box anyway).

Related Issues and Possible Duplicates

Related Issues

Possible Duplicates

Also related:

Brief Summary

Being able to await an AsyncResult

The goal is to offload CPU intensive tasks to dedicated workers and return a result to the client once the task is completed.

Design

Architectural Considerations

I don't know how to implement this feature, but it seems that this project does:
https://github.com/kai3341/celery-pool-asyncio
However, it's not compatible with Celery 5 (see kai3341/celery-pool-asyncio#29)

Proposed Behavior

Proposed UI/UX

In a Django 3.1 async view:

async def my_view(request):
    result = await my_cpu_intensive_task.delay()
    return HttpResponse(f"Result = {result}")

Diagrams

N/A

Alternatives

I've considered polling AsyncResult.state every second, but it's a very inelegant solution.

@auvipy
Copy link
Member

auvipy commented Jan 20, 2021

we can try this in django-celery-results along with celery core as a POC first. but it is a part of a bigger design decision which we are planning for celery 6.0. additionally we can try to use an aio-libs in one of our result back end and see how things need to be done as experiments.

@philmday
Copy link

philmday commented Feb 2, 2021

this would be a very useful feature.

@bblanchon
Copy link
Author

As I wrote on stackoverflow, I currently use the following helper as a workaround:

import asyncio
from asgiref.sync import sync_to_async

# Converts a Celery tasks to an async function
def task_to_async(task):
    async def wrapper(*args, **kwargs):
        delay = 0.1
        async_result = await sync_to_async(task.delay)(*args, **kwargs)
        while not async_result.ready():
            await asyncio.sleep(delay)
            delay = min(delay * 1.5, 2)  # exponential backoff, max 2 seconds
        return async_result.get()
    return wrapper

Like sync_to_async, it can be used as a direct wrapper:

@shared_task
def get_answer():
    sleep(10) # simulate long computation
    return 42    

result = await task_to_async(get_answer)()

...and as a decorator:

@task_to_async
@shared_task
def get_answer():
    sleep(10) # simulate long computation
    return 42    

result = await get_answer()

Of course, this is not a perfect solution since it relies on polling, but it is a good workaround for calling tasks from Django async views until Celery officially provides a better solution.

@thedrow
Copy link
Member

thedrow commented Apr 11, 2021

We're working on it.
Anyone is welcome to join the Jumpstarter task force.
https://github.com/celery/jumpstarter

@amacfie
Copy link

amacfie commented Apr 12, 2021

We're working on it.
Anyone is welcome to join the Jumpstarter task force.
https://github.com/celery/jumpstarter

@thedrow can you explain more about what Jumpstarter is?

@auvipy auvipy added this to the 6.0.0 milestone Dec 20, 2021
@NoraGithub
Copy link

we can try this in django-celery-results along with celery core as a POC first. but it is a part of a bigger design decision which we are planning for celery 6.0. additionally we can try to use an aio-libs in one of our result back end and see how things need to be done as experiments.

https://github.com/celery/django-celery-results may be this one

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

No branches or pull requests

6 participants