方法采用 1 个位置参数,但给出了 2 个
Method takes 1 positional argument but 2 were given
我是运行一个芹菜任务,它需要两个参数,像这样:
@task_bp.route('/analysis/<user_id>', methods=['GET', 'POST'])
def save_user_with_analysis(user_id):
post_data = request.get_json()
response_object = {
'status': 'fail',
'message': 'User does not exist'
}
spotify_token = ''
if post_data:
spotify_token = post_data.get('spotify_token')
try:
user = User.query.filter_by(id=user_id).first()
if not user:
return jsonify(response_object), 404
else:
# pass 2 args, ID and token
task = save_user_tracks_with_analysis.apply_async(args=[user_id, spotify_token],kwargs={})
response_object = {
'status': 'success',
'message': 'Background task for saving user analysis to database initialized.',
'data': {
'task_id': task.id,
}
}
return jsonify(response_object), 202
except (exc.IntegrityError, ValueError):
db.session.rollback()
return jsonify(response_object), 404
和:
@celery.task(queue='analysis',default_retry_delay=30,max_retries=3,soft_time_limit=1000)
def save_user_tracks_with_analysis(user_id, spotify_token):
track_and_features = sp_utilities.query_tracks(spotify_token)
try:
while True:
result = Upload_Track_With_Audio_Analysis(dataset=next(track_and_features),
user_id=user_id)
except StopIteration:
pass
finally:
del track_and_features
return {'Status': 'Tracks uploaded with analysis!'}
但我得到这个错误:
TypeError: save_user_tracks_with_analysis() takes 1 positional argument but 2 were given
完整追溯:
[2019-10-23 00:38:58,444: INFO/MainProcess] Received task: project.api.routes.background.save_user_tracks_with_analysis[d0db40ee-a1bb-4dbb-b066-7db9b5f5900d]
[2019-10-23 00:38:58,450: DEBUG/MainProcess] TaskPool: Apply <function _fast_trace_task at 0x7f5d8a9448c8> (args:('project.api.routes.background.save_user_tracks_with_analysis', 'd0db40ee-a1bb-4dbb-b066-7db9b5f5900d', {'lang': 'py', 'task': 'brandio.api.routes.background.save_user_tracks_with_analysis', 'id': 'd0db40ee-a1bb-4dbb-b066-7db9b5f5900d', 'shadow': None, 'eta': None, 'expires': None, 'group': None, 'retries': 0, 'timelimit': [None, 1000], 'root_id': 'd0db40ee-a1bb-4dbb-b066-7db9b5f5900d', 'parent_id': None, 'argsrepr': "['1', '']", 'kwargsrepr': '{}', 'origin': 'gen14@9065a884289a', 'reply_to': '4ed9cf27-826d-3d3d-9282-83750953189b', 'correlation_id': 'd0db40ee-a1bb-4dbb-b066-7db9b5f5900d', 'delivery_info': {'exchange': '', 'routing_key': 'analysis', 'priority': 0, 'redelivered': None}}, b'[["1", ""], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]', 'application/json', 'utf-8') kwargs:{})
[2019-10-23 00:38:58,457: DEBUG/MainProcess] Task accepted: project.api.routes.background.save_user_tracks_with_analysis[d0db40ee-a1bb-4dbb-b066-7db9b5f5900d] pid:12
[2019-10-23 00:38:58,456: ERROR/ForkPoolWorker-3] Task project.api.routes.background.save_user_tracks_with_analysis[d0db40ee-a1bb-4dbb-b066-7db9b5f5900d] raised unexpected: TypeError('save_user_tracks_with_analysis() takes 1 positional argument but 2 were given',)
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/celery/app/trace.py", line 382, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/celery/app/trace.py", line 641, in __protected_call__
return self.run(*args, **kwargs)
TypeError: save_user_tracks_with_analysis() takes 1 positional argument but 2 were given
[2019-10-23 00:40:10,528: INFO/MainProcess] Received task: project.api.routes.background.save_user_tracks_with_analysis[a049f85f-bb5a-4cf3-81f8-f334210cb7e4]
[2019-10-23 00:40:10,532: DEBUG/MainProcess] TaskPool: Apply <function _fast_trace_task at 0x7f5d8a9448c8> (args:('project.api.routes.background.save_user_tracks_with_analysis', 'a049f85f-bb5a-4cf3-81f8-f334210cb7e4', {'lang': 'py', 'task': 'project.api.routes.background.save_user_tracks_with_analysis', 'id': 'a049f85f-bb5a-4cf3-81f8-f334210cb7e4', 'shadow': None, 'eta': None, 'expires': None, 'group': None, 'retries': 0, 'timelimit': [None, 1000], 'root_id': 'a049f85f-bb5a-4cf3-81f8-f334210cb7e4', 'parent_id': None, 'argsrepr': "['1', 'BQCFoIPB1BS3_Fe4nrHuTJMJ2bDDqOJBlEMH2-TwzrAeLk8GN30O3DQm3hRW2DRFVbaDb-0msctmjN6phKAmad4F6hs9AilCyyO_Ky5JoT1cThsRNoL6GptRh1SPcYL29toTkUQitXcRSaDRQY0BsJB9haNtCRD-KNhNPQSC0dbCB6M-tWBd-4l9hl74tRR7csamu5qBol653QBfoNhcKzvJeSaewPMpyhMArvA_DW4Pcr8KLFsFmEV8jiDb2d0f']", 'kwargsrepr': '{}', 'origin': 'gen14@9065a884289a', 'reply_to': '4ed9cf27-826d-3d3d-9282-83750953189b', 'correlation_id': 'a049f85f-bb5a-4cf3-81f8-f334210cb7e4', 'delivery_info': {'exchange': '', 'routing_key': 'analysis', 'priority': 0, 'redelivered': None}}, b'[["1",... kwargs:{})
[2019-10-23 00:40:10,536: DEBUG/MainProcess] Task accepted: project.api.routes.background.save_user_tracks_with_analysis[a049f85f-bb5a-4cf3-81f8-f334210cb7e4] pid:13
[2019-10-23 00:40:10,537: ERROR/ForkPoolWorker-4] Task project.api.routes.background.save_user_tracks_with_analysis[a049f85f-bb5a-4cf3-81f8-f334210cb7e4] raised unexpected: TypeError('save_user_tracks_with_analysis() takes 1 positional argument but 2 were given',)
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/celery/app/trace.py", line 382, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/celery/app/trace.py", line 641, in __protected_call__
return self.run(*args, **kwargs)
TypeError: save_user_tracks_with_analysis() takes 1 positional argument but 2 were given
[2019-10-23 01:45:54,209: INFO/MainProcess] Received task: project.api.routes.background.save_user_tracks_with_analysis[7391e3c7-ddf9-4f76-a046-36506b3c8289]
[2019-10-23 01:45:54,212: DEBUG/MainProcess] TaskPool: Apply <function _fast_trace_task at 0x7f5d8a9448c8> (args:('project.api.routes.background.save_user_tracks_with_analysis', '7391e3c7-ddf9-4f76-a046-36506b3c8289', {'lang': 'py', 'task': 'project.api.routes.background.save_user_tracks_with_analysis', 'id': '7391e3c7-ddf9-4f76-a046-36506b3c8289', 'shadow': None, 'eta': None, 'expires': None, 'group': None, 'retries': 0, 'timelimit': [None, 1000], 'root_id': '7391e3c7-ddf9-4f76-a046-36506b3c8289', 'parent_id': None, 'argsrepr': "('1', 'BQABvl5STugFWzHizIipkw0B7-_g7MmP-lFAokVh4_XMJFHGl6XYNmEvi8AieQduMxpqHCujjkOJC4sypY4w-7_77NSsspXUpVn65bW77VeyihG5JLmJpIC86pqzzijZZTZfu-RTt1_yBCkK7UXsEb1K7biWcL8gpVsfZu1vAsFsI1V51IP3sxKoXf3ohPxTLddH1Wfi_npfOv1wkgaeLdbwxCaO2gEqowqRappzIiSNfXqCSAIdNNaGPROqpR5r')", 'kwargsrepr': '{}', 'origin': 'gen30@9065a884289a', 'reply_to': '5b96d61f-e2f9-3237-a7a1-afab83d41e12', 'correlation_id': '7391e3c7-ddf9-4f76-a046-36506b3c8289', 'delivery_info': {'exchange': '', 'routing_key': 'analysis', 'priority': 0, 'redelivered': None}}, b'[["1",... kwargs:{})
[2019-10-23 01:45:54,214: DEBUG/MainProcess] Task accepted: project.api.routes.background.save_user_tracks_with_analysis[7391e3c7-ddf9-4f76-a046-36506b3c8289] pid:12
[2019-10-23 01:45:54,225: ERROR/ForkPoolWorker-3] Task project.api.routes.background.save_user_tracks_with_analysis[7391e3c7-ddf9-4f76-a046-36506b3c8289] raised unexpected: TypeError('save_user_tracks_with_analysis() takes 1 positional argument but 2 were given',)
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/celery/app/trace.py", line 382, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/celery/app/trace.py", line 641, in __protected_call__
return self.run(*args, **kwargs)
TypeError: save_user_tracks_with_analysis() takes 1 positional argument but 2 were given
就我而言,apply_sync()
需要将参数作为 list
传递。
我错过了什么?
我不明白为什么,但是当我重新启动 Docker 应用程序和所有 celery worker 时,上面的代码开始工作了。
我是运行一个芹菜任务,它需要两个参数,像这样:
@task_bp.route('/analysis/<user_id>', methods=['GET', 'POST'])
def save_user_with_analysis(user_id):
post_data = request.get_json()
response_object = {
'status': 'fail',
'message': 'User does not exist'
}
spotify_token = ''
if post_data:
spotify_token = post_data.get('spotify_token')
try:
user = User.query.filter_by(id=user_id).first()
if not user:
return jsonify(response_object), 404
else:
# pass 2 args, ID and token
task = save_user_tracks_with_analysis.apply_async(args=[user_id, spotify_token],kwargs={})
response_object = {
'status': 'success',
'message': 'Background task for saving user analysis to database initialized.',
'data': {
'task_id': task.id,
}
}
return jsonify(response_object), 202
except (exc.IntegrityError, ValueError):
db.session.rollback()
return jsonify(response_object), 404
和:
@celery.task(queue='analysis',default_retry_delay=30,max_retries=3,soft_time_limit=1000)
def save_user_tracks_with_analysis(user_id, spotify_token):
track_and_features = sp_utilities.query_tracks(spotify_token)
try:
while True:
result = Upload_Track_With_Audio_Analysis(dataset=next(track_and_features),
user_id=user_id)
except StopIteration:
pass
finally:
del track_and_features
return {'Status': 'Tracks uploaded with analysis!'}
但我得到这个错误:
TypeError: save_user_tracks_with_analysis() takes 1 positional argument but 2 were given
完整追溯:
[2019-10-23 00:38:58,444: INFO/MainProcess] Received task: project.api.routes.background.save_user_tracks_with_analysis[d0db40ee-a1bb-4dbb-b066-7db9b5f5900d]
[2019-10-23 00:38:58,450: DEBUG/MainProcess] TaskPool: Apply <function _fast_trace_task at 0x7f5d8a9448c8> (args:('project.api.routes.background.save_user_tracks_with_analysis', 'd0db40ee-a1bb-4dbb-b066-7db9b5f5900d', {'lang': 'py', 'task': 'brandio.api.routes.background.save_user_tracks_with_analysis', 'id': 'd0db40ee-a1bb-4dbb-b066-7db9b5f5900d', 'shadow': None, 'eta': None, 'expires': None, 'group': None, 'retries': 0, 'timelimit': [None, 1000], 'root_id': 'd0db40ee-a1bb-4dbb-b066-7db9b5f5900d', 'parent_id': None, 'argsrepr': "['1', '']", 'kwargsrepr': '{}', 'origin': 'gen14@9065a884289a', 'reply_to': '4ed9cf27-826d-3d3d-9282-83750953189b', 'correlation_id': 'd0db40ee-a1bb-4dbb-b066-7db9b5f5900d', 'delivery_info': {'exchange': '', 'routing_key': 'analysis', 'priority': 0, 'redelivered': None}}, b'[["1", ""], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]', 'application/json', 'utf-8') kwargs:{})
[2019-10-23 00:38:58,457: DEBUG/MainProcess] Task accepted: project.api.routes.background.save_user_tracks_with_analysis[d0db40ee-a1bb-4dbb-b066-7db9b5f5900d] pid:12
[2019-10-23 00:38:58,456: ERROR/ForkPoolWorker-3] Task project.api.routes.background.save_user_tracks_with_analysis[d0db40ee-a1bb-4dbb-b066-7db9b5f5900d] raised unexpected: TypeError('save_user_tracks_with_analysis() takes 1 positional argument but 2 were given',)
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/celery/app/trace.py", line 382, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/celery/app/trace.py", line 641, in __protected_call__
return self.run(*args, **kwargs)
TypeError: save_user_tracks_with_analysis() takes 1 positional argument but 2 were given
[2019-10-23 00:40:10,528: INFO/MainProcess] Received task: project.api.routes.background.save_user_tracks_with_analysis[a049f85f-bb5a-4cf3-81f8-f334210cb7e4]
[2019-10-23 00:40:10,532: DEBUG/MainProcess] TaskPool: Apply <function _fast_trace_task at 0x7f5d8a9448c8> (args:('project.api.routes.background.save_user_tracks_with_analysis', 'a049f85f-bb5a-4cf3-81f8-f334210cb7e4', {'lang': 'py', 'task': 'project.api.routes.background.save_user_tracks_with_analysis', 'id': 'a049f85f-bb5a-4cf3-81f8-f334210cb7e4', 'shadow': None, 'eta': None, 'expires': None, 'group': None, 'retries': 0, 'timelimit': [None, 1000], 'root_id': 'a049f85f-bb5a-4cf3-81f8-f334210cb7e4', 'parent_id': None, 'argsrepr': "['1', 'BQCFoIPB1BS3_Fe4nrHuTJMJ2bDDqOJBlEMH2-TwzrAeLk8GN30O3DQm3hRW2DRFVbaDb-0msctmjN6phKAmad4F6hs9AilCyyO_Ky5JoT1cThsRNoL6GptRh1SPcYL29toTkUQitXcRSaDRQY0BsJB9haNtCRD-KNhNPQSC0dbCB6M-tWBd-4l9hl74tRR7csamu5qBol653QBfoNhcKzvJeSaewPMpyhMArvA_DW4Pcr8KLFsFmEV8jiDb2d0f']", 'kwargsrepr': '{}', 'origin': 'gen14@9065a884289a', 'reply_to': '4ed9cf27-826d-3d3d-9282-83750953189b', 'correlation_id': 'a049f85f-bb5a-4cf3-81f8-f334210cb7e4', 'delivery_info': {'exchange': '', 'routing_key': 'analysis', 'priority': 0, 'redelivered': None}}, b'[["1",... kwargs:{})
[2019-10-23 00:40:10,536: DEBUG/MainProcess] Task accepted: project.api.routes.background.save_user_tracks_with_analysis[a049f85f-bb5a-4cf3-81f8-f334210cb7e4] pid:13
[2019-10-23 00:40:10,537: ERROR/ForkPoolWorker-4] Task project.api.routes.background.save_user_tracks_with_analysis[a049f85f-bb5a-4cf3-81f8-f334210cb7e4] raised unexpected: TypeError('save_user_tracks_with_analysis() takes 1 positional argument but 2 were given',)
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/celery/app/trace.py", line 382, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/celery/app/trace.py", line 641, in __protected_call__
return self.run(*args, **kwargs)
TypeError: save_user_tracks_with_analysis() takes 1 positional argument but 2 were given
[2019-10-23 01:45:54,209: INFO/MainProcess] Received task: project.api.routes.background.save_user_tracks_with_analysis[7391e3c7-ddf9-4f76-a046-36506b3c8289]
[2019-10-23 01:45:54,212: DEBUG/MainProcess] TaskPool: Apply <function _fast_trace_task at 0x7f5d8a9448c8> (args:('project.api.routes.background.save_user_tracks_with_analysis', '7391e3c7-ddf9-4f76-a046-36506b3c8289', {'lang': 'py', 'task': 'project.api.routes.background.save_user_tracks_with_analysis', 'id': '7391e3c7-ddf9-4f76-a046-36506b3c8289', 'shadow': None, 'eta': None, 'expires': None, 'group': None, 'retries': 0, 'timelimit': [None, 1000], 'root_id': '7391e3c7-ddf9-4f76-a046-36506b3c8289', 'parent_id': None, 'argsrepr': "('1', 'BQABvl5STugFWzHizIipkw0B7-_g7MmP-lFAokVh4_XMJFHGl6XYNmEvi8AieQduMxpqHCujjkOJC4sypY4w-7_77NSsspXUpVn65bW77VeyihG5JLmJpIC86pqzzijZZTZfu-RTt1_yBCkK7UXsEb1K7biWcL8gpVsfZu1vAsFsI1V51IP3sxKoXf3ohPxTLddH1Wfi_npfOv1wkgaeLdbwxCaO2gEqowqRappzIiSNfXqCSAIdNNaGPROqpR5r')", 'kwargsrepr': '{}', 'origin': 'gen30@9065a884289a', 'reply_to': '5b96d61f-e2f9-3237-a7a1-afab83d41e12', 'correlation_id': '7391e3c7-ddf9-4f76-a046-36506b3c8289', 'delivery_info': {'exchange': '', 'routing_key': 'analysis', 'priority': 0, 'redelivered': None}}, b'[["1",... kwargs:{})
[2019-10-23 01:45:54,214: DEBUG/MainProcess] Task accepted: project.api.routes.background.save_user_tracks_with_analysis[7391e3c7-ddf9-4f76-a046-36506b3c8289] pid:12
[2019-10-23 01:45:54,225: ERROR/ForkPoolWorker-3] Task project.api.routes.background.save_user_tracks_with_analysis[7391e3c7-ddf9-4f76-a046-36506b3c8289] raised unexpected: TypeError('save_user_tracks_with_analysis() takes 1 positional argument but 2 were given',)
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/celery/app/trace.py", line 382, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/celery/app/trace.py", line 641, in __protected_call__
return self.run(*args, **kwargs)
TypeError: save_user_tracks_with_analysis() takes 1 positional argument but 2 were given
就我而言,apply_sync()
需要将参数作为 list
传递。
我错过了什么?
我不明白为什么,但是当我重新启动 Docker 应用程序和所有 celery worker 时,上面的代码开始工作了。