簡體   English   中英

Python Celery 異步任務未執行

[英]Python Celery asynchronous tasks are not executed

我正在學習使用 Celery 在 python 中創建異步任務

這是我的代碼

from celery import Celery
import time

app = Celery("hello", backend="rpc://", broker="amqp://guest@localhost//")


@app.task
def hello():
    time.sleep(5)
    with open("./test.txt", "w+", encoding="utf-8") as fp:
        fp.write("ok")

    return "hello world"

Celery啟動信息

> celery -A main worker -l INFO
 
 -------------- celery@ajanuw v5.2.1 (dawn-chorus)    
--- ***** -----
-- ******* ---- Windows-10-10.0.19043-SP0 2021-12-07 08:56:00
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app:         hello:0x1c616583040   
- ** ---------- .> transport:   amqp://guest:**@localhost:5672//
- ** ---------- .> results:     rpc://
- *** --- * --- .> concurrency: 12 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery


[tasks]
  . main.hello

[2021-12-07 08:56:00,970: INFO/MainProcess] Connected 
to amqp://guest:**@127.0.0.1:5672//
[2021-12-07 08:56:01,013: INFO/MainProcess] mingle: searching for neighbors
[2021-12-07 08:56:01,370: INFO/SpawnPoolWorker-4] child process 2000 calling self.run()



[2021-12-07 08:56:14,018: INFO/MainProcess] Task main.hello[010042fd-4839-
received

打個招呼 function

>>> from main import *

>>> hello.delay()
<AsyncResult: 010042fd-4839-46c0-8602-cf192f6b50b9>

我不知道出了什么問題,文件還沒有創建

版本信息

> python -V
Python 3.10.0

celery==5.2.1

rabbitmq:3.9.11-management

在此處輸入圖像描述

不管在windows上怎么測試都不會成功,於是我在wsl ubuntu上安裝了Python 3.8.10 測試發現可以正確執行異步任務我不知道為什么它在 windows 上不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM