Celeryのchainの使い方メモ
monkeyjack人工植物草Imitation CeleryリーフグリーンFoliage Home Decor 0fc86a448d9ee0e32f6ad4b8bd12d311
- 出版社/メーカー: MonkeyJack
- メディア: ホーム&キッチン
- この商品を含むブログを見る
コメント
Celery(セロリ)のメモ。
実装例
並列処理する対象の関数、関数その1、その2があるとして、関数その1が一度終わったら、そのまま引数を引き継いで関数その2に受け渡す例 (具体例:メールを送信する関数1、成功・失敗をプリントする関数2)
app/tasks.py/
#app/tasks.py/ from celery import shared_task @shared_task def add(x, y): return 2 * x + y @shared_task def printer(z): string="some string" print("pre print==>",string)#redisのログに吐き出される return z
app/views.py/
#app/views.py/ from .tasks import * from celery import chain #urls.pyと連動したtest def test(request): result = add.delay(3, 8) #通常のcelery関数ひとつ while not result.ready(): print('spam') print(result.get()) #プリントデバッグはこれ(これ以外はredisのログを見る) result = chain(add.s(4, 4), printer.s()).apply_async() #↑関数addに引数4,4を投入、 #addの返り値"2 * x + y"を #関数printerの第一引数になる。 #add.sのsはシグネイチャーという機能の略らしい print(result.get()) #プリントデバッグは同じ ・・・以下は通常のDjango.viewsと同じ