Celery: Eagerモード

settings:

1
2
CELERY_TASK_ALWAYS_EAGER = True
CELERY_TASK_STORE_EAGER_RESULT = True

unittest

Eagerモードの確認

Hello, this is Bing. Celery is a distributed task queue for Python. Eager mode is a setting that makes Celery run tasks synchronously in the same process as the caller, instead of sending them to a worker¹². This can be useful for testing, but it may not reflect the actual behavior of the tasks in a worker¹². To check if a running Celery task is in eager mode, you can use the is_eager attribute of the task request object³. For example:

1
2
3
4
5
6
7
@app.task
def add(x, y):
    if add.request.is_eager:
        print("Running in eager mode")
    else:
        print("Running in worker mode")
    return x + y

Is this what you were looking for?

ソース: Bing との会話 2023/4/12