Django                
            非同期タスク(セロリ)
        
        
            
    サーチ…
備考
Celeryは、バックグラウンドジョブやスケジューリングされたジョブを実行し、Djangoと非常によく統合できるタスクキューです。セロリには、 メッセージブローカーと呼ばれる何かが、呼び出しからワーカーにメッセージを渡す必要があります。このメッセージブローカーはredis、rabbitmq、またはDjango ORM / dbでもかまいませんが、これは推奨される方法ではありません。
サンプルを始める前に、セロリを設定する必要があります。セロリを設定するには、メインアプリでsettings.pyファイルと並行してcelery_config.pyファイルを作成します。 
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
# broker url 
BROKER_URL = 'redis://localhost:6379/0'
# Indicate Celery to use the default Django settings module
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
app = Celery('config')
app.config_from_object('django.conf:settings')
# if you do not need to keep track of results, this can be turned off
app.conf.update(
    CELERY_RESULT_BACKEND=BROKER_URL,
)
# This line will tell Celery to autodiscover all your tasks.py that are in your app folders
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
メインアプリの__init__.pyファイルでセロリのアプリケーションをインポートします。このような
# -*- coding: utf-8 -*- 
# Not required for Python 3. 
from __future__ import absolute_import
from .celery_config import app as celery_app  # noqa
celery workerを実行するには、manage.pyがあるレベルでこのコマンドを使用します。
# pros is your django project, 
celery -A proj worker -l info
2つの数字を追加する簡単な例
開始するには:
- セロリpip install celeryセロリをpip install celery
- セロリを構成する(備考欄に向かう)
from __future__ import absolute_import, unicode_literals
from celery.decorators import task
@task
def add_number(x, y):
    return x + y
 .delay()メソッドを使用すると、これを非同期に実行できます。 
 add_number.delay(5, 10)と10は、関数add_number引数です) 
非同期関数が操作を終了したかどうかを調べるには、 delayメソッドによって返された非同期オブジェクトに対して.ready()関数を使用できます。 
計算の結果をフェッチするには、非同期オブジェクトの.result属性を使用します。 
例
async_result_object = add_number.delay(5, 10)
if async_result_object.ready():
    print(async_result_object.result)
Modified text is an extract of the original Stack Overflow Documentation
        ライセンスを受けた CC BY-SA 3.0
        所属していない Stack Overflow