当前位置:网站首页>Cell asynchronously invokes method change records

Cell asynchronously invokes method change records

2022-06-10 14:13:00 tnan2522

Celery It's a task queue , You can handle... Asynchronously
stay 4.0 After version python Use celery There will be mistakes , At this point you need to install eventlet modular

pip install eventlet

The default is

celery -A task worker --loglevel=info

start-up celery

4.0 Post start celery:

celery -A task worker --loglevel=info -P eventlet

stay work The decorator also needs to be changed

The default is :

import time

from celery import Celery

app = Celery(
    broker='amqp://[email protected]//',  #  Message queue url
    backend='redis://localhost:6379/9'  #  Store the result of the call in Redis in 

)


@app.task
def add(x, y):
    print("celery  The data received is : ", x, y)
    print("x+y=", x+y)
    # time.sleep(100)
    return x + y

Change it to :

import time

from celery import Celery

app = Celery(
    broker='amqp://[email protected]//',  #  Message queue url
    backend='redis://localhost:6379/9'  #  Store the result of the call in Redis in 

)


@app.task(name="aaabbb")
def add(x, y):
    print("celery  The data received is : ", x, y)
    print("x+y=", x+y)
    # time.sleep(100)
    return x + y

	

This is because if you do not set a name for the task queue , The queue will be named according to the current module name and method name , Such as
aaabbb

Importing and using in other modules is , Such as

from celery_aabb.task import add


for i in range(50):
    add.delay(i, i + 10)

add.delay When sending a message to a task queue, the queue sent to is celery_aabb.task.add, In this way, the corresponding task queue processing cannot be found , There will be an exception

If the queue name is set , that , In the import add I will use it when I use it celery Decorate the named name as the name of the task queue sent , Thus, the task processing queue is found

When it's called remotely ,

from celery import Celery


app = Celery(
    broker='amqp://[email protected]//',  #  Message queue url
)

app.send_task("aabbcc", [10, 20])



Just write the name of the task queue to find celery Task queue started in

Save execution results :

backend='redis://localhost:6379/8'

Store the result of the call in Redis in , Storage format yes json Serialized string

stay add In the processing function, the return The return value is stored in as the value to be saved redis in , If there is no return value ,celery It also stores data , However, it stores the restful by null

celery Send E-mail

stay task Import other modules into the work module , Because it was started in command line mode , So it can only find the files and folders in the current directory , Can't find the files and folders on the upper level ,
You need to put the mail sending module into task Under the directory of work modules

原网站

版权声明
本文为[tnan2522]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101339432801.html