代码之家  ›  专栏  ›  技术社区  ›  ClickThisNick

2019年python 2/3异步

  •  1
  • ClickThisNick  · 技术社区  · 6 年前

    我必须同时保持我的应用程序与python 2和3兼容。

    我有一些类似下面的代码,我希望能够异步调用一个函数,然后等待我的所有未来得到解决。

    代码:

    import requests
    
    # This would return a future
    def get_xhr(url):
        return requests.get('https://www.{}.com'.format(url))
    
    # This would return an array of futures
    def get_search_engines():
        urls = ['google', 'yahoo', 'bing']
        return [get_xhr(url) for url in urls]
    
    
    # Here I want to wait for all the futures to be resolved
    get_search_engines()
    
    print('All requests are done')
    

    asyncio async/await似乎只与python 3兼容。

    什么是能够运行与两个Python2/3兼容的异步函数的最佳方法?

    1 回复  |  直到 6 年前
        1
  •  4
  •   Messa    6 年前

    对于python 2有 Twisted Tornado .

    但也许在你的用例线程中( threading , concurrent.futures )将是最简单的解决方案。

    还要记住 到2019年底,python 2将不再被维护。 .