Open yidea opened 3 years ago
Why don't you just use ThreadPoolExecutor
? Async really isn't much of a solve for what you could already do with existing concurrency features in Python. Just do something like this:
import concurrent.futures
from concurrent.futures import ThreadPoolExecutor
def get_price(symbol):
pass # TODO: implement API call to fetch stock data
with ThreadPoolExecutor(max_workers=4) as executor:
futures = [executor.submit(get_price, symbol) for symbol in symbols]
done, not_done = concurrent.futures.wait(futures)
data = [future.result() for future in done]
The data
var is a list of the return values generated by get_price
. I don't recommend using more than just a few max_workers
because Robinhood could easily flag your account for abuse of their API if you send too many requests too quickly.
Currently all API here r seq, in some case e.g. scanning a bunch of stock price will take longer time, any plan we have the async support down the road?