I am trying to make parallel calls to external apis from django using requests or any other library that allows me to do this.
I have already tried using grequests to make this calls, sometimes it works but most times I get ‘NoneType’ object has no attribute ‘json’ error on the client side. Here are my codes
views.py
def get_fixtures(request, league_id): league_id = league_id urls = [ "https://api-football-v1.p.rapidapi.com/v2/fixtures/league/%d" % league_id, "https://api-football-v1.p.rapidapi.com/v2/leagues/league/%d" % league_id ] headers = {'X-RapidAPI-Host': "api-football-v1.p.rapidapi.com", 'X-RapidAPI-Key': X_RapidAPI_Key} resp = (grequests.get(u, headers=headers) for u in urls) responses = grequests.map(resp) a = responses[0].json() b = responses[1].json() fix_1 = a['api']['fixtures'] api_2 = b['api']['leagues'] context = { 'fix_1': fix_1, 'api_2': api_2, } return render(request, "pages/fixtures.html", context)
On the server side i get this error:
File "srcgevent_greenlet_primitives.py", line 60, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch File "srcgevent_greenlet_primitives.py", line 64, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch File "srcgevent__greenlet_primitives.pxd", line 35, in gevent.__greenlet_primitives._greenlet_switch greenlet.error: cannot switch to a different thread.
Can I use requests or any other library to perform the calls without getting these errors? if yes how do i implement it in my work?
Advertisement
Answer
Try placing this:
resp = list(grequests.get(u, headers=headers) for u in urls)