Flask + Celery:任务ID为整数顺序

时间:2018-08-09 08:12:56

标签: python flask celery celery-task celerybeat

我正在尝试建立一个定期的芹菜任务,每分钟从API提取数据。每次执行任务时,我都希望自定义任务ID,以便第一个任务从1开始,然后每次执行任务时都递增1。

以下内容将任务ID作为唯一键返回:例如123-456-789。

方法1:

@periodic_task(run_every=crontab(minute="*"), bind=True)
def periodic_run_get_manifest(self):
    with app.app_context():
        taskId = self.request.id.__str__
        df = fetch_external_API_data('results', params['schedule'][taskId]['season'], params['schedule'][taskId]['roundID'])

这是必需的,因为json文件是这样创建的。

{
    "schedule": {
        "1": {
            "season": 2018,
            "roundID": 1
        },
        "2": {
            "season": 2018,
            "roundID": 2
        },
        "3": {
            "season": 2018,
            "roundID": 3
        },
         "4": {
            "season": 2018,
            "roundID": 4
        }
    }
}

我想使用这个:

方法2:

id_list = []
@periodic_task(run_every=crontab(minute="*"), bind=True)
def periodic_run_get_manifest(self):
    with app.app_context():
        taskId = self.request.id
        id_list.append(taskId)
        seasons = [params['schedule'][str(len(id_list))]['season']]
        print(id_list, seasons ,races)
        races = [params['schedule'][str(len(id_list))]['roundID']]
        df = fetch_external_API_data('results', seasons, races)

但是打印出来: 似乎没有存储先前完成的任务的taskId,因为每次任务运行都会打印不同的ID。另外,我没有任何解释,为什么即使我在函数外部初始化了一个空的id_list数组,似乎每次执行任务都会重新初始化它?

[2018-08-09 09:02:00,039: WARNING/ForkPoolWorker-4] ([u'29617c68-5048-4c46-b131-676022d73ce2'], [2018], [1])
[2018-08-09 09:03:00,052: WARNING/ForkPoolWorker-7] ([u'9115b9c6-bb11-4ab8-a78d-2d4d5730f1d3'], [2018], [1])
[2018-08-09 09:04:00,042: WARNING/ForkPoolWorker-5] ([u'df8aa5c4-9aa7-4a92-a461-bca29699ca4e'], [2018], [1])
[2018-08-09 09:05:00,060: WARNING/ForkPoolWorker-1] ([u'15ee2959-03e4-4a52-9181-089fa79ca821'], [2018], [1])
[2018-08-09 09:06:00,053: WARNING/ForkPoolWorker-4] ([u'29617c68-5048-4c46-b131-676022d73ce2', u'08138521-281d-4468-af05-258140c04a59'], [2018], [2])
[2018-08-09 09:07:00,053: WARNING/ForkPoolWorker-7] ([u'9115b9c6-bb11-4ab8-a78d-2d4d5730f1d3', u'0d851ec6-0ecf-432f-a862-0c25f3351903'], [2018], [2])
[2018-08-09 09:08:00,052: WARNING/ForkPoolWorker-5] ([u'df8aa5c4-9aa7-4a92-a461-bca29699ca4e', u'587bd5c1-689e-4922-9572-f0570b3871d1'], [2018], [2])
[2018-08-09 09:09:00,060: WARNING/ForkPoolWorker-1] ([u'15ee2959-03e4-4a52-9181-089fa79ca821', u'0ca49386-3ea9-4eb5-8f76-27b1650761e4'], [2018], [2])

0 个答案:

没有答案