I have to keep my application compatible with Python 2 and 3 at the same time.
I have some code like the following that I want to be able to asynchronously call a function and then wait for all my futures to be resolved.
Code:
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 seems only compatible with Python 3.
What is the best way to be able to run functions async thats compatible for both python 2/3?
For Python 2 there is Twisted or Tornado.
But maybe in your use case threads (threading, concurrent.futures) would be the easiest solution.
Also keep in mind that Python 2 is going to be not maintained anymore at the end of 2019.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With