TypeError:' instancemethod'对象没有属性' __ getitem __'在芹菜

时间:2014-12-30 07:04:07

标签: python django celery python-requests

我正在使用 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)

1 个答案:

答案 0 :(得分:2)

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

another_function(ids, r.json())