Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.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线程是否会因请求而相互绊倒?_Python_Multithreading_Python Requests - Fatal编程技术网

我的Python线程是否会因请求而相互绊倒?

我的Python线程是否会因请求而相互绊倒?,python,multithreading,python-requests,Python,Multithreading,Python Requests,我有一个程序需要从API访问数据。我需要从中获取一个列表,然后针对该列表中的每一项,从API请求更多数据。当我得到一个列表时,当列表上有大约600个项目时,我会将它们分成50个批次。我认为我可以使用请求和线程来实现这一点。下面是它的外观: 我基本上有一个助手方法来调用API: call_api_method(method, token, params={}): params_to_send = params.copy() params_to_send['auth'] = to

我有一个程序需要从API访问数据。我需要从中获取一个列表,然后针对该列表中的每一项,从API请求更多数据。当我得到一个列表时,当列表上有大约600个项目时,我会将它们分成50个批次。我认为我可以使用请求和线程来实现这一点。下面是它的外观:

我基本上有一个助手方法来调用API:

call_api_method(method, token, params={}):
     params_to_send = params.copy()
     params_to_send['auth'] = token
     response = requests.get('{0}/rest{1}'.format(DOMAIN, method), params = params_to_send)
     return response.json()
然后我有一个递归线程函数来获取所有信息。我想我可以使用线程继续并请求下一批信息,同时让线程请求每个项目的信息:

def import_item_info(auth_token, start = None):
     if start is None:
          start = 0
     threads = []
     result = call_api_method('get_list', auth_token, {'start': start})
     #the call returns next which is the index of the next batch
     if result['next']:
          thread = threading.Thread(target=import_item_info, args=(auth_token, result['next'])
          thread.start()
          threads.append(thread)
     for list_item in result['result']:
          thread = threading.Thread(target=get_item_info, args=(auth_token, item['ID'])
          thread.start()
          threads.append(thread)
     for thread in threads:
          thread.join()
这是get_item_info,它使用项的id调用api以获取有关项的特定详细信息:

 def get_item_info(auth_token, item_id):
     item = call_api_method('get_item', auth_token, {'id': item_id})
     print(item['key'])
我已经抽象了很多信息,但本质上是这样的:有时requests.get返回的内容有点混乱,我得到了一个JSONDecodeError:期望值:第1行第1列(char 0)


我高度怀疑这是一个线程问题,因为第一个请求通过得很好。我似乎找不到我做错了什么。

好的。很抱歉我想我检查过了,但显然我达到了查询限制,这就是为什么它开始做奇怪的事情。

什么是真正的url?您是否尝试过在没有线程的情况下获取项目信息?你也有同样的问题吗?您是否打印了
response.text
response.content
以查看您得到了什么?也许你们会得到一些有用的信息——也许API会给你们一些警告,或者网络出现了不同的问题。您是否打印完整的url以直接在web浏览器中进行测试?也许您创建的url在web浏览器中也不起作用。get\u item\u info在没有线程的情况下工作得非常好。没问题。我把物品和钥匙都印好了。完整的url也可以正常工作。至于真正的url是什么:它是我们公司的云CRM。