2
votes

I am using Django==1.7.1, django-celery==3.1.16.

I have followed the guide in Official Celery Docs to set up Celery and have been able to run a sample task too. Now, I am trying to use the response given by requests.post but I do not know how to do this. I get the TypeError as mentioned in the title, when I pass this response to another function and try to do a simple print.

Inside 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())

In 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)

Log:

[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__'

In line 122 in gcmhandlers.py,

121def another_function(regids, msg):
122    print msg, type(msg)
1
If you delete line 122 in gcmhandlers.py, does this error still exist? - Stephen Lin

1 Answers

2
votes

json() is a method on a requests.Response instance, you need to call it:

another_function(ids, r.json())