Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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的多线程GET请求:错误401_Python_Multithreading_Python Multithreading_Http Status Code 401 - Fatal编程技术网

使用python的多线程GET请求:错误401

使用python的多线程GET请求:错误401,python,multithreading,python-multithreading,http-status-code-401,Python,Multithreading,Python Multithreading,Http Status Code 401,我正在尝试查询一些搜索结果的API。我首先获取搜索返回的结果数,然后生成与我的结果页面数相等的线程数。然而,当页面数量增加时,我开始从urllib2获得不一致的HTTP错误401,即使我对生成的所有URL使用相同的API键。每次错误都发生在不同的URL上。首先,这是查询API以获取跨越多个页面(超过一千个页面)的信息的最佳方法吗。第二,为什么我会有bug def worker(pageNum): pageDetails = urllib2.urlopen(generateUrl(page

我正在尝试查询一些搜索结果的API。我首先获取搜索返回的结果数,然后生成与我的结果页面数相等的线程数。然而,当页面数量增加时,我开始从urllib2获得不一致的HTTP错误401,即使我对生成的所有URL使用相同的API键。每次错误都发生在不同的URL上。首先,这是查询API以获取跨越多个页面(超过一千个页面)的信息的最佳方法吗。第二,为什么我会有bug

def worker(pageNum):
    pageDetails = urllib2.urlopen(generateUrl(pageNum), timeout=1000).read()
    pageDetails = json.loads(pageDetails)
    #print pageDetails
    print str(pageNum) + "\n"
    return

def parallelRun(totalPages):
    pageList = range(totalPages)
    threads = []
    for pageNum in pageList:
        t = threading.Thread(target=worker, args=(pageNum,))
        threads.append(t)

    for thread in threads:
        thread.start()

    for thread in threads:
        thread.join()
    return

parallelRun(numPages)

如果您将工作人员更改为以下内容:

def worker(pageNum):
    try:
       pageDetails = urllib2.urlopen(generateUrl(pageNum), timeout=1000).read()
       pageDetails = json.loads(pageDetails)
       #print pageDetails
       print str(pageNum) + "\n"
       return
    except urlib2.HTTPError as err:
       print err.reason
       print err.read()
       raise
您将获得有关出错原因的更详细信息