質問編集履歴

1

追記

2019/01/07 08:18

投稿

trafalbad
trafalbad

スコア303

test CHANGED
@@ -1 +1 @@
1
- celeryでジョブキューを投げたあと結果を取得きない問題について
1
+ celeryのチュートリアルインデント
test CHANGED
@@ -1,92 +1,110 @@
1
- redisとceleryでceleryにジョブキューを送ろうとしまし
1
+ celeryでworkerを作るため[celeryのチュートリアル](http://flask.pocoo.org/docs/0.12/patterns/celery/)やっのですが、
2
2
 
3
+ ```python
4
+
5
+ celery -A add worker -l info
6
+
7
+ ```
8
+
9
+
10
+
11
+ でceleryを動かすと下記のコードでインデントがおかしいとエラーが出ました。
12
+
13
+
14
+
15
+ コード部分のクラス内の関数がおかしいのですが、ちょっと処理がいまいちわからないためご教授お願いします。
16
+
17
+
18
+
19
+ ```python
20
+
3
- だいたいこの[サイト](https://ensekitt.hatenablog.com/entry/2017/05/07/140857)通りにやりました。Mac上でターミナルをredis用とcelery用に2つ開いて、以下手順実行しました。
21
+ File "/anaconda3/lib/python3.7/importlib/__init__.py", line 127, in import_module
22
+
23
+ return _bootstrap._gcd_import(name[level:], package, level)
24
+
25
+ File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
26
+
27
+ File "<frozen importlib._bootstrap>", line 983, in _find_and_load
28
+
29
+ File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
30
+
31
+ File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
32
+
33
+ File "<frozen importlib._bootstrap_external>", line 724, in exec_module
34
+
35
+ File "<frozen importlib._bootstrap_external>", line 860, in get_code
36
+
37
+ File "<frozen importlib._bootstrap_external>", line 791, in source_to_code
38
+
39
+ File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
40
+
41
+ File "/Users/tatsuyahagiwara/Desktop/add.py", line 12
42
+
43
+ return TaskBase.__call__(self, *args, **kwargs)
44
+
45
+ ^
46
+
47
+ IndentationError: expected an indented block
48
+
49
+ ```
50
+
51
+ ```python
52
+
53
+ # add.py
54
+
55
+ from celery import Celery
56
+
57
+
58
+
59
+ def make_celery(app):
60
+
61
+ celery = Celery('tasks', backend=app.config['CELERY_RESULT_BACKEND'],
62
+
63
+ broker=app.config['CELERY_BROKER_URL'])
64
+
65
+ celery.conf.update(app.config)
66
+
67
+ TaskBase = celery.Task
68
+
69
+ class ContextTask(TaskBase):
70
+
71
+ abstract = True
72
+
73
+ def __call__(self, *args, **kwargs):
74
+
75
+ with app.app_context():
76
+
77
+ return TaskBase.__call__(self, *args, **kwargs)
78
+
79
+ celery.Task = ContextTask
80
+
81
+ return celery
82
+
83
+
84
+
85
+ from flask import Flask
86
+
87
+
88
+
89
+ flask_app = Flask(__name__)
90
+
91
+ flask_app.config.update(
92
+
93
+ CELERY_BROKER_URL='redis://localhost:6379',
94
+
95
+ CELERY_RESULT_BACKEND='redis://localhost:6379')
96
+
97
+ celery = make_celery(flask_app)
4
98
 
5
99
 
6
100
 
7
101
 
8
102
 
9
- エラーは手順4にある通りです。celeryはちゃんと結果を受け取ってるのに、なぜredis側で結果を取得できないのでしょうか?
103
+ @celery.task()
10
104
 
11
- ご教授お願いします
105
+ def add_together(a, b):
12
106
 
13
-
14
-
15
- 手順1.Macのターミナル上でredisを起動
16
-
17
- ```
18
-
19
- $ redis-server /usr/local/etc/redis.conf
20
-
21
- ```
22
-
23
-
24
-
25
- 手順2.mac上で2つ目のターミナルを開いてceleryを起動
26
-
27
- ```
28
-
29
- $ celery -A taskss worker -l info
30
-
31
- ```
32
-
33
-
34
-
35
- 手順3.1で開いたredisでスクリプトを実行
36
-
37
- ```
38
-
39
- # redis側
40
-
41
- $Python3
42
-
43
- $from taskss import tasks
44
-
45
- $p=tasks.delay("arrived!")
46
-
47
- $p
48
-
49
- >>> <AsyncResult: 969b31ee-f07e-4e96-9cf7-99a25d5671ed>
50
-
51
- ```
52
-
53
- ```
54
-
55
- # celery側
56
-
57
- Task taskss.tasks[969b31ee-f07e-4e96-9cf7-99a25d5671ed] succeeded in 10.002289248000011s: 'Hello arrived!'
58
-
59
- ```
60
-
61
-
62
-
63
- 手順4.結果を取得する
64
-
65
- ```
66
-
67
- # redis側
68
-
69
- $ p.result()
107
+ return a + b
70
-
71
- # エラー
72
-
73
- Traceback (most recent call last):
74
-
75
- File "<stdin>", line 1, in <module>
76
-
77
- File "/anaconda3/lib/python3.7/site-packages/celery/result.py", line 434, in result
78
-
79
- return self._get_task_meta()['result']
80
-
81
- File "/anaconda3/lib/python3.7/site-packages/celery/result.py", line 410, in _get_task_meta
82
-
83
- return self._maybe_set_cache(self.backend.get_task_meta(self.id))
84
-
85
- File "/anaconda3/lib/python3.7/site-packages/celery/backends/base.py", line 365, in get_task_meta
86
-
87
- meta = self._get_task_meta_for(task_id)
88
-
89
- AttributeError: 'DisabledBackend' object has no attribute '_get_task_meta_for'
90
108
 
91
109
 
92
110