flask-sqlalchemy DB不在芹菜和码头内更新

时间:2018-06-15 18:35:44

标签: flask celery flask-sqlalchemy

我在码头工具里面有一个烧瓶应用和芹菜任务。我可以看到芹菜表现不错但唯一的问题是结果没有更新到芹菜功能内的DB。但是,当我关闭docker并再次重新启动时,我可以看到DB正在更新。
该函数在第16行调用芹菜任务。

@app.route('/upload', methods=['GET', 'POST'])
@is_logged_in
def upload():
    file = request.files['files']
    file_copy = copy.deepcopy(file.read())
    file_copy1 = copy.deepcopy(file_copy)
    username = session['username']
    try:
        df = pd.read_csv(BytesIO(file_copy))
    except:
        return redirect(url_for('leaderboard'))
    columns = df.columns.values.tolist()
    if 'ImageID' not in columns or 'Masks' not in columns:
        return redirect(url_for('leaderboard'))

    evaluate_metric_at_backend.delay(df, username)

    newFile = FileContents(name=file.filename,
                           username=username,
                           data=file_copy1,
                           segmentation=-1,
                           mean_avg_precision=-1)

    update_scores(username=username,
                  prev_segmentation=-1,
                  prev_mean_avg_precision=-1)


    filestorage_db.session.add(newFile)
    filestorage_db.session.commit()
    app.logger.info('File saved')


    return redirect(url_for('leaderboard'))

这个函数是芹菜任务,我试图在每个端点更新数据库,这是无效的。

@celery.task(name='celery.calculate_metric', serializer='pickle')
def evaluate_metric_at_backend(df, username):

    to_upload = {}
    result = evaluate(df)
    segmentation_score, mean_avg_precision = result
    print(segmentation_score, mean_avg_precision)
    prev_segmentation_score, prev_mean_avg_precision = get_scores_for(username)
    print('qqqqqqqq', prev_segmentation_score, prev_mean_avg_precision)

    to_upload['segmentation_score'] = 0
    to_upload['mean_avg_precision'] = 0
    if segmentation_score >= prev_segmentation_score:
        to_upload['segmentation_score'] = 1
    if mean_avg_precision >= prev_mean_avg_precision:
        to_upload['mean_avg_precision'] = 1
    filters = []
    for each_score in ['segmentation_score', 'mean_avg_precision']:
        if to_upload[each_score] == 0:
            filters.append(each_score)
    Alarm_msg = 'The values of {0} are not greater than your previous {0} values'.format(','.join(filters))
    if len(filters) == 0:
        prev_segmentation_score = segmentation_score
        prev_mean_avg_precision = mean_avg_precision


    with app.app_context():
        print('ffffff', prev_segmentation_score, prev_mean_avg_precision)
        print(FileContents.query.filter_by(username=username).all())

        update_scores(username=username,
                      prev_segmentation=prev_segmentation_score,
                      prev_mean_avg_precision=prev_mean_avg_precision)
        print(FileContents)
        print(type(username))
        print(FileContents.query.filter_by(username=username).all())
        print(PrevScoreContensts.query.filter_by(username=username).all())

        rows = FileContents.query.filter_by(username=username, segmentation=-1, mean_avg_precision=-1).first()
        rows.segmentation = segmentation_score
        rows.mean_avg_precision = mean_avg_precision
        print(rows.segmentation, rows.mean_avg_precision)
        filestorage_db.session.commit()

update_scores的功能

def update_scores(username, prev_segmentation, prev_mean_avg_precision):
    count = PrevScoreContensts.query.filter_by(username=username).first()
    print('ggggggg', prev_segmentation, prev_mean_avg_precision)
    if count is None:
        prev_file = PrevScoreContensts(username=username,
                                       prev_segmentation=prev_segmentation,
                                       prev_mean_avg_precision=prev_mean_avg_precision)
        filestorage_db.session.add(prev_file)
        filestorage_db.session.commit()
    else:
        count.prev_segmentation = prev_segmentation
        count.prev_mean_avg_precision = prev_mean_avg_precision
        filestorage_db.session.commit()

知道如何更新芹菜任务中的数据库。 ?

0 个答案:

没有答案