V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Harlaus
V2EX  ›  Python

可以将 asyncio 与 celery 一起使用吗?

  •  
  •   Harlaus · Dec 9, 2019 · 6949 views
    This topic created in 2331 days ago, the information mentioned may be changed or developed.

    一个是 asyncio run_forever (我希望它一直运行,维护一个队列)的协程函数, 用 celery 装饰成 task,这样做有效吗,代码像这样的:

    import asyncio
    improt celery
    
    async def do_some_work(x):
        print("Waiting " + str(x))
        await asyncio.sleep(x)
    
    def worker():
       loop = asyncio.get_event_loop()
       for _ in range(_concurrency):
           loop.create_task(do_some_work(x))
       loop.run_forever()
    
    @app.task
    def run(msg):
        worker()
    
    
    Supplement 1  ·  Dec 9, 2019
    也不完全是偷懒的原因,我用 asyncio 和 concurrent.futures 写了一个 work with 队列的代码 ( https://gitee.com/Harlaus/pipenode/blob/master/pipenode/server.py
    见笑了非常粗糙,回到问题,我是想用它集成我写的一些任务,同时我又不想放弃 celery,就酱
    12 replies    2020-04-23 17:19:03 +08:00
    dingyaguang117
        1
    dingyaguang117  
       Dec 9, 2019
    - - 这样做的意义是啥?
    Harlaus
        2
    Harlaus  
    OP
       Dec 9, 2019
    @dingyaguang117 asyncio 的代码写好了,不想改
    superrichman
        3
    superrichman  
       Dec 9, 2019
    没有试过 celery, 我用 apscheduler 的 AsyncIOScheduler 可以和 asyncio 一起使用
    hustlibraco
        4
    hustlibraco  
       Dec 9, 2019
    run_forever 肯定不行,run_until_complete 应该可以,但是这样做很不好,还是不要偷懒了。python 代码改起来也不是很麻烦。
    rogwan
        5
    rogwan  
       Dec 9, 2019 via iPhone
    程序是拿来解决问题的,不是拿来玩死自己的。
    forrestshuang
        6
    forrestshuang  
       Dec 9, 2019
    celery 本身就是异步的
    ClericPy
        7
    ClericPy  
       Dec 9, 2019
    协程可以挂在任何线程上跑, 非主线程上跑事件循环没什么经验的话有一定可能出问题, 还是那句, 何必呢

    把多个任务用 gather 合起来, run_forever 换成 run_until_complete 放里面跑吧, 有时候 running 的 Loop 还不让, 就得 new 一个 Loop
    Harlaus
        8
    Harlaus  
    OP
       Dec 9, 2019
    @hustlibraco 也不完全是偷懒的原因,我用 asyncio 和 concurrent.futures 写了一个 work with 队列的代码( https://gitee.com/Harlaus/pipenode/blob/master/pipenode/server.py)见笑了非常粗糙,我想用它集成我写的一些任务,同时我又不想放弃 celery,就酱
    hustlibraco
        9
    hustlibraco  
       Dec 9, 2019
    @Harlaus 你可以把队列的部分去掉,替换成 celery,loop 只能有一个,然后去调度 celery 的 task。
    Harlaus
        10
    Harlaus  
    OP
       Dec 9, 2019
    @ClericPy
    ‘把多个任务用 gather 合起来, run_forever 换成 run_until_complete 放里面跑吧, 有时候 running 的 Loop 还不让, 就得 new 一个 Loop'
    现在已经是了
    lolizeppelin
        11
    lolizeppelin  
       Dec 9, 2019
    至少之前我看 kombu 代码的时候还不支持 asyncio

    asyncio 就在大量库支持之前根本没法用,还不如老老实实 eventlet
    18620610600
        12
    18620610600  
       Apr 23, 2020
    celery 官方要 5.0 才支持 asyncio
    我的是这么实现在 celery 中跑 async def 的
    ```
    import asyncio

    def run_async(coro):
    return asyncio.run(coro)

    @app.task
    def celery_task(*args, **kwargs):
    return run_async(async_func(*args, **kwargs))

    async def async_func(*args, **kwargs):
    rv = await sub_func()
    # do sth
    return rv


    async def sub_func():
    return 1


    # Usage:
    def view(request):
    task = celery_task.delay(request)
    return Response({'task_id': task.id})
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1033 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 64ms · UTC 22:12 · PVG 06:12 · LAX 15:12 · JFK 18:12
    ♥ Do have faith in what you're doing.