Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python请求进行多个api调用_Python_Django_Api_Python Requests_Sendasynchronousrequest - Fatal编程技术网

如何使用python请求进行多个api调用

如何使用python请求进行多个api调用,python,django,api,python-requests,sendasynchronousrequest,Python,Django,Api,Python Requests,Sendasynchronousrequest,我试图使用请求或任何其他允许我这样做的库从django对外部API进行并行调用 我已经尝试过使用grequests来进行此调用,有时可以,但大多数时候我得到的'NoneType'对象在客户端没有属性'json'错误。 这是我的密码 views.py def get_fixtures(request, league_id): league_id = league_id urls = [ "https://api-football-v1.p.rapidapi.com/v2/fixtures

我试图使用请求或任何其他允许我这样做的库从django对外部API进行并行调用

我已经尝试过使用grequests来进行此调用,有时可以,但大多数时候我得到的'NoneType'对象在客户端没有属性'json'错误。 这是我的密码

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)
在服务器端,我遇到以下错误:

File "src\gevent\_greenlet_primitives.py", line 60, in 
gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src\gevent\_greenlet_primitives.py", line 64, in 
gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src\gevent\__greenlet_primitives.pxd", line 35, in 
gevent.__greenlet_primitives._greenlet_switch
greenlet.error: cannot switch to a different thread.
我是否可以使用请求或任何其他库来执行调用而不出现这些错误?如果是,我如何在我的工作中实施它?

试着将其放在以下位置:

resp = list(grequests.get(u, headers=headers) for u in urls)
尝试放置以下内容:

resp = list(grequests.get(u, headers=headers) for u in urls)

这很有帮助。看起来效果更好。但我仍然得到:文件“src\gevent\uu greenlet\u primitives.pxd”,第35行,在gevent.\uu greenlet\u primitives.\u greenlet\u switch greenlet.error:无法切换到其他线程。在我的本地server@Bruce你解决过这个问题吗?我也有同样的问题,这很有帮助。看起来效果更好。但我仍然得到:文件“src\gevent\uu greenlet\u primitives.pxd”,第35行,在gevent.\uu greenlet\u primitives.\u greenlet\u switch greenlet.error:无法切换到其他线程。在我的本地server@Bruce你解决过这个问题吗?我也有同样的问题