Async Mode Examples
Contents
Client.get() Examples
The following example loops at line 4 while the response has not yet arrived:
1@client.session
2async def main():
3 resp = client.get('quote_type', symbol='msft')
4 while resp.pending():
5 # do some other stuff
The following example blocks at line 4 until the response has arrived:
1@client.session
2async def main():
3 resp = client.get('quote_type', symbol='msft')
4 await resp.wait()
5 # do some other stuff
Client.batch_get() Examples
The following example waits until all of the responses have arrived before running the async for loop:
1@client.session
2async def main():
3 queries = [
4 dict(endpoint='quote_type', symbol='msft'),
5 dict(endpoint='price_overview', symbol='aapl'),
6 dict(endpoint='key_statistics', symbol='tsla')
7 ]
8 results = client.batch_get(queries)
9 async for resp in results.gather():
10 # do some stuff with the resp
The following example starts yielding the responses into the async for loop as soon as they become available:
1@client.session
2async def main():
3 queries = [
4 dict(endpoint='quote_type', symbol='msft'),
5 dict(endpoint='price_overview', symbol='aapl'),
6 dict(endpoint='key_statistics', symbol='tsla')
7 ]
8 results = client.batch_get(queries)
9 async for resp in results.as_completed():
10 # do some stuff with the resp
Client.get_all() Examples
The following example loops while all the available data about a symbol is being retrieved:
1@client.session
2async def main():
3 results = client.get_all(symbol='msft')
4 while results.pending():
5 # do some other stuff
The following example blocks while all the available data about a symbol is being retrieved:
1@client.session
2async def main():
3 results = client.get_all(symbol='aapl')
4 await results.wait()
5 # do some other stuff
WARNING: A single call to get_all() creates 32 simultaneous network requests and
can return up to 1.5 megabytes of data, so uncontrolled usage of this method
may deplete the memory of your system and may get your IP blacklisted by Yahoo.