gpt4 book ai didi

python - 类型错误 : 'instancemethod' object has no attribute '__getitem__' in Celery

转载 作者:太空宇宙 更新时间:2023-11-04 03:38:34 33 4
gpt4 key购买 nike

我正在使用 Django==1.7.1, django-celery==3.1.16.

我已按照 Official Celery Docs 中的指南进行操作设置 Celery 并且也能够运行示例任务。现在,我正在尝试使用 requests.post 给出的响应,但我不知道该怎么做。当我将此响应传递给另一个函数并尝试进行简单打印时,我得到了标题中提到的 TypeError。

在 app/tasks.py 中:

from .helpers.handler import another_function

@shared_task
def gcm_send(ids, msg):
GCM_URL = "https://android.googleapis.com/gcm/send"
API_KEY = "app_api_key"
headers = {'content-type': 'application/json', "authorization": "key=" + API_KEY}
json_data = make_json(ids, msg)
r = requests.post(GCM_URL, data=json_data, headers=headers)
another_function(ids, r.json())

在 views.py 中:

def gcm_dispatcher(request, regid, msg):
gcm_send.delay(regid, msg)
return HttpResponse("Ok")

def another_function(ids, message):
print message, type(message)

日志:

[2014-12-30 06:48:35,090: INFO/MainProcess] Received task: gcm.tasks.gcm_send[cb89f794-dd33-45ef-be17-79a3a7a99ff4]
[2014-12-30 06:48:35,091: WARNING/Worker-1] {"registration_ids": ["APA91bESwG9VhMnOKtWLPi4GB1B9Mxv15t_AIdDpOzb2IDebHQwn4z_nDb-ObqNiKfcTNMNDWXJL7gfI_3kioaT7fe1dHam53aF01C4_vGgm-2XVas3FWq-93sPDCLHUE59YiBP-1VddCUtcvIvGkZSmEkQ0ig9jtQ"], "data": {"message": {"type": "splash", "message": "Message", "persist": true, "data": "http://ipimg.goiphonewallpapers.com/2012/02/19/5be190d8df3b7d2e_320x480.jpg", "title": "Campaign"}}}
[2014-12-30 06:48:35,093: INFO/Worker-1] Starting new HTTPS connection (1): android.googleapis.com
[2014-12-30 06:48:35,846: WARNING/Worker-1] application/json; charset=UTF-8
[2014-12-30 06:48:35,847: WARNING/Worker-1] 200
[2014-12-30 06:48:35,847: WARNING/Worker-1] <bound method Response.json of <Response [200]>>
[2014-12-30 06:48:36,295: ERROR/MainProcess] Task gcm.tasks.gcm_send[cb89f794-dd33-45ef-be17-79a3a7a99ff4] raised unexpected: TypeError("'instancemethod' object has no attribute '__getitem__'",)
Traceback (most recent call last):
File "/home/tilaprimera/.virtualenvs/picovico/lib/python2.7/site-packages/celery/app/trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "/home/tilaprimera/.virtualenvs/picovico/lib/python2.7/site-packages/celery/app/trace.py", line 438, in __protected_call__
return self.run(*args, **kwargs)
File "/home/tilaprimera/PycharmProjects/pvgcmcamp/pvcamp/gcm/tasks.py", line 34, in gcm_send
"APA91bESwG9VhMnOKtWLPi4GB1B9Mxv15t_AIdDpOzb2IDebHQwn4z_nDb-ObqNiKfcTNMNDWXJL7gfI_3kioaT7fe1dHam53aF01C4_vGgm-2XVas3FWq-93sPDCLHUE59YiBP-1VddCUtcvIvGkZSmEkQ0ig9jtQ",\
File "/home/tilaprimera/PycharmProjects/pvgcmcamp/pvcamp/gcm/helpers/gcmhandles.py", line 122, in

map_regid_with_message_reponse
TypeError: 'instancemethod' object has no attribute '__getitem__'

在 gcmhandlers.py 的第 122 行,

121def another_function(regids, msg):
122 print msg, type(msg)

最佳答案

json()requests.Response 实例上的一个方法,您需要调用它:

another_function(ids, r.json())

关于python - 类型错误 : 'instancemethod' object has no attribute '__getitem__' in Celery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27701029/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com