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 to use AsyncResult.then in async/await #4353

Closed
xulei890817 opened this Issue Oct 30, 2017 · 1 comment

Comments

Projects
None yet
3 participants
@xulei890817
Copy link

xulei890817 commented Oct 30, 2017

from gevent import monkey

monkey.patch_all()
import asyncio

import time
from celery import Celery
from celery.result import AsyncResult

app = Celery(broker='amqp://xxx', backend='redis://:xxx')

app.config_from_object('config')


@app.task(name='tasks.add')
def add(x, y):
    return x + y


def on_result_ready(result: AsyncResult):
    print('Received result for id %r: %r' % (result.id, result.result,))
    result.forget()
    print("forget end")


if __name__ == "__main__":
    for _ in range(10):
        async def test(my_nym):
            res: AsyncResult = add.apply_async(args=[my_nym, my_nym])
            res.then(on_result_ready)


        asyncio.ensure_future(test(1))
        asyncio.ensure_future(test(2))
        asyncio.ensure_future(test(3))
        asyncio.ensure_future(test(4))

    import asyncio

    asyncio.get_event_loop().run_forever()

I have tried this code in my code.It works fine.
But,it seems it's only working after i import gevent.
I'm coding with python3.6 async/await is very nice for me ,The gevent patch change the queue to non-block make many error,
So what can i do.
change gevent patch?

__all__ = [
    'patch_all',
    'patch_builtins',
    'patch_dns',
    'patch_os',
    'patch_select',
    'patch_signal',
    'patch_socket',
    'patch_ssl',
    'patch_subprocess',
    'patch_sys',
    'patch_thread',
    'patch_time',
    # query functions
    'get_original',
    'is_module_patched',
    'is_object_patched',
    # module functions
    'main',
]

or what can i do
change my code to queue.get_nowait may is not suitable for me.

from gevent import monkey
monkey.patch_all()
@thedrow

This comment has been minimized.

Copy link
Member

thedrow commented Nov 13, 2017

Currently Celery doesn't support the asyncio library.
We plan to change that in Celery 5.0 but we haven't found the time to implement it.

@thedrow thedrow closed this Nov 13, 2017

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