How to speed up API requests?

后端 未结 5 1567
没有蜡笔的小新
没有蜡笔的小新 2020-12-16 02:31

I\'ve constructed the following little program for getting phone numbers using google\'s place api but it\'s pretty slow. When I\'m testing with 6 items it takes anywhere fr

5条回答
  •  鱼传尺愫
    2020-12-16 02:49

    Most of the time isn't spent computing your request. The time is spent in communication with the server. That is a thing you cannot control.

    However, you may be able to speed it along using parallelization. Create a separate thread for each request as a start.

    from threading import Thread
    
    def request_search_terms(*args):
        #your logic for a request goes here
        pass
    
    #...
    
    threads = []
    for st in searchTerms:
        threads.append (Thread (target=request_search_terms, args=(st,)))
        threads[-1].start()
    
    for t in threads:
        t.join();
    

    Then use a thread pool as the number of request grows, this will avoid the overhead of repeated thread creation.

提交回复
热议问题