我正面临与Celery not running chord callback中类似的问题,我尝试了链接问题中的答案,但无法解决问题。
这是我的任务。py
import uuid
import os
import json
from celery import Celery, chord
import pandas as pd
from db import DataBaseEngine
from os import listdir, remove
from config import config
# Setting the Default Django settings module
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'algo360.settings')
# import datetime
app = Celery('tasks',
broker='pyamqp://' + config.RabbitMQ.USERNAME + ":"
+ config.RabbitMQ.PASSWORD + "@"
+ config.RabbitMQ.INSTANCE_ID
+ "/" + config.RabbitMQ.HOST,
backend='redis://localhost')
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@app.task(queue="filewriter")
def writeJsonFiles(jsonObj):
""" something """
@app.task(queue="filereader")
def readJsonFiles(fileName):
F = open(os.path.join('json_temp', fileName), 'r')
jObj = json.loads(F.read())
print(jObj)
F.close()
remove(os.path.join('json_temp', fileName))
return jObj
@app.task(queue="filereader")
def writeToSql(jsonObjects):
print("Called")
print('write to json')
df = pd.DataFrame(jsonObjects, index=[0])
df['date'] = pd.to_datetime(df['date'])
conn = DataBaseEngine(config.RedShift.ENGINE, config.RedShift.HOST,
config.RedShift.USER, config.RedShift.PASSWORD,
config.RedShift.DATA_BASE).createEngine()
df.to_sql("main_accessattempts", conn, index=False, if_exists='append')
test.py
from tasks import writeJsonFiles, readJsonFiles, writeToSql
import json
from os import listdir
import os
from db import DataBaseEngine
from celery import Celery, chord
from config import config
print(config.RedShift.ENGINE)
try:
read_write = [readJsonFiles.s(i) for i in listdir('json')]
chord(read_write, writeToSql.s())
print('test')
except Exception as e:
print('error', e)
该和弦未在test.py中运行。甚至chord_unlock也被调用。
这是日志文件输出
(venv) C:\Users\Neil Haria\Documents\djangoapi>celery worker -A tasks -l info
-------------- celery@Neil v3.1.25 (Cipater)
---- **** -----
--- * *** * -- Windows-10-10.0.17134-SP0
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: tasks:0x4ca0eb0
- ** ---------- .> transport: ***************
- ** ---------- .> results: redis://localhost/
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ----
--- ***** ----- [queues]
-------------- .> celery exchange=celery(direct) key=celery
[tasks]
. tasks.readJsonFiles
. tasks.writeJsonFiles
. tasks.writeToSql
[2018-08-21 15:09:00,143: INFO/MainProcess] Connected to ***************
[2018-08-21 15:09:00,500: INFO/MainProcess] mingle: searching for
neighbors
[2018-08-21 15:09:01,811: INFO/MainProcess] mingle: all alone
[2018-08-21 15:09:02,406: WARNING/MainProcess] c:\users\neil haria\documents\djangoapi\venv\lib\site packages\celery\fixups\django.py:265: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
warnings.warn('Using settings.DEBUG leads to a memory leak, never '
[2018-08-21 15:09:02,424: WARNING/MainProcess] celery@Neil ready.
[2018-08-21 15:09:03,464: INFO/MainProcess] Events of group {task} enabled by remote
该和弦在python manage.py shell中也不起作用