Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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多请求URL_Python_Python 3.x_Api_Curl_Async Await - Fatal编程技术网

Python多请求URL

Python多请求URL,python,python-3.x,api,curl,async-await,Python,Python 3.x,Api,Curl,Async Await,大家好, 我是python新手(我刚开始不到2周) 所以我需要一些建议和技巧:p 获取1500个api请求的最快和最有效的方法是什么 使用异步函数执行它们并返回以获取 结果如何 将它们划分为300个URL的列表,并将每个URL 在将在异步循环中执行它们的线程中列出 执行与第二个建议相同的操作,但使用流程 而不是线程 目前它对我很有效,但执行1400个api请求需要8秒左右,但如果我尝试一个没有线程的请求,则需要9秒 我做错了什么 获取一个URL(我试图将会话作为param传递,但在到达700个请

大家好, 我是python新手(我刚开始不到2周) 所以我需要一些建议和技巧:p

获取1500个api请求的最快和最有效的方法是什么

  • 使用异步函数执行它们并返回以获取 结果如何
  • 将它们划分为300个URL的列表,并将每个URL 在将在异步循环中执行它们的线程中列出
  • 执行与第二个建议相同的操作,但使用流程 而不是线程
  • 目前它对我很有效,但执行1400个api请求需要8秒左右,但如果我尝试一个没有线程的请求,则需要9秒 我做错了什么

    获取一个URL(我试图将会话作为param传递,但在到达700个请求时出错) 获取异步循环中的URL列表 创建线程并将其放入异步循环中,具体取决于URL/X每个循环的URL 例如,MultiFetch(url[600],200)将创建3个线程,这些线程将以异步方式逐线程调用200个请求
    最后我找到了一个解决方案:)获取1400个URL需要2.2秒

    我使用了3ed建议(进程内的异步循环)

    #获取1个URL

    async def fetch_one(url):
        async with curio_http.ClientSession() as session:
            response = await session.get(url)
            content =  await response.json()
            return  content
    
    #获取X个URL 异步def fetchmultiurl(url\u列表): 任务=[] 答复=[] 对于url_列表中的url: task=wait curio.spawn(获取一个(url)) tasks.append(任务)

    #我试图将lambda替换为此函数,但它不起作用

    def RuningCurio(X):
        return curio.run(fetchMultiURLs(X))
    
    #根据URL创建进程和异步循环/X逐循环URL

    #在我的情况下(我使用VPS),一个进程可以在不到1秒的时间内轻松获取700个链接,因此不要在这个URL数下进行多进程处理(只需使用fetchMultiURLs函数)


    #我用1.1秒获取2100个URL,我希望这个解决方案能帮助你们

    多线程+队列是我所知道的最快的。(一小时内300万个http请求+扫描)非常感谢您的回答:)但这不是最有效的方法,因为在Python中,GIL问题使此类请求的速度过慢:p(我尝试了您的想法,需要11秒)
    def MultiFetch(URLS,X):
        MyThreadsList = []
        MyThreadsResults = []
        N_Threads = (lambda x:  int (x/X) if (x % X == 0) else int(x/X)+1) (len(URLS))
        for i in range( N_Threads  ): # will iterate X = ListSize / X
                  MyThreadsList.append( Thread(  target = curio.run , args = (fetchMultiURLs( (URLS[ i*X:(X*i+X)]) )  ,)    )  )
                  MyThreadsList[i].start()
        for i in range( N_Threads  ):
                  MyThreadsResults.append(MyThreadsList[i].join())
        return MyThreadsResults
    
    async def fetch_one(url):
        async with curio_http.ClientSession() as session:
            response = await session.get(url)
            content =  await response.json()
            return  content
    
            for task in tasks:
                content = await task.join()
                responses.append(content)
            return responses
    
    def RuningCurio(X):
        return curio.run(fetchMultiURLs(X))
    
     def MultiFetch(URLS,X):
        MyListofLists = []
        LengthURLs = len(URLS)
        N_Process = int (LengthURLs / X) if ( LengthURLs % X == 0) else int( LengthURLs / X) + 1
        for i in range( N_Process  ): # Create a list of lists (  [ [1,2,3],[4,5,6],[7,8,9] ] )
            MyListofLists.append(URLS[ i*X:(X*i+X)])
        P = Pool( N_Process) 
        return  P.map( RuningCurio ,MyListofLists)