flask集成celery说明一

1.首先是项目的目录预览


项目原地址:人人快速开发平台

    1.financialData.py

@tushareDown.route("/allocationPlan", methods=["GET", "POST"])

#@login_required  登录才能访问

def allocationPlan():

    print("tushareDowning allocationPlan data start year:"+ request.data)

    data =request.data

    json_data =  json.loads(data)

    yearParam = str(json_data['BODY']['year'])

    allocationPlanTask.apply_async(args=[yearParam], kwargs='yearParam')# 发送异步任务,指定队列

    #allocationPlanTask.delay(yearParam);    

    return '';

2.financialTask.py

from celery import Celery

celeryTask=Celery("app",broker='redis://127.0.0.1:6379/1',backend="redis://127.0.0.1:6379/0")

@celeryTask.task(name='allocationPlanTask')

def allocationPlanTask(yearParam):

    print("tushareDowning allocationPlanTask data start year:",yearParam)

    df = ts.profit_data(year = int(yearParam),top=3000)

    for index,row in df.iterrows():

        pdRow = pd.DataFrame(row)

        if os.path.exists(allocationPlanPath+"code\\"+str(row["code"])+fileName):

            oldData = pd.read_csv(allocationPlanPath+"code\\"+str(row["code"])+fileName,dtype=str,index_col =0);

            codeData =pd.concat([oldData,pdRow.T],join="inner");

            codeData=codeData.sort_values(by = ["report_date"],axis=0,ascending = [True])

            codeData.drop_duplicates("report_date",inplace=True);

            codeData.to_csv(allocationPlanPath+"code\\"+str(row["code"])+fileName,encoding='utf-8');

        else:

            pdRow.T.to_csv(allocationPlanPath+'code\\'+str(row["code"])+fileName,encoding='utf-8');

    df.to_csv(allocationPlanPath+"date\\"+yearParam+fileName,encoding=fileEncoding);

1.python manage.py runserver -h 127.0.0.1

2.启动redis

3.

项目启动命令 celery -A app.tushareDown.tasks.financialTask worker --loglevel=info


问题:celery 启动未实现flask的管理,只在financialTask.py才能实现,后续优化为全局变量

你可能感兴趣的:(flask集成celery说明一)