I have a very simple script on python 3.7 and don't understand why it executes consistently, not simultaneously. Where is mistake? This is very important to stay script structure as it is, meaning don't remove class and semaphore, and I need to compute math operations in op()
. I tried to play with self.op()
, removing async
, but the same results.
import time
import asyncio
class Trader:
def __init__(self, ticker):
self.ticker = ticker
async def _init(self):
# asyncio.run(self.op())
await self.op()
print('{} ended'.format(self.ticker))
async def op(self):
a = range(1000000)
b = []
for i in a:
b.append(i*2)
async def fetch_tickers():
return await asyncio.gather(*(asyncio.ensure_future(safe_trader(ticker)) for ticker in ['A', 'B', 'C', 'D', 'E', 'F', 'G']))
async def safe_trader(ticker):
async with sem:
t = Trader(ticker)
return await t._init()
if __name__ == '__main__':
start_time = time.time()
sem = asyncio.Semaphore(10)
loop = asyncio.get_event_loop()
results = loop.run_until_complete(fetch_tickers())
print("%.2f execution seconds" % (time.time() - start_time))
Result:
A ended
B ended
C ended
D ended
E ended
F ended
G ended
0.82 execution seconds
If I leave only 1 ticker A
, then we have results:
A ended
0.12 execution seconds
Your code is working perfectly in async as it's supposed to be. Since each task requires the same amount of time and computational power to complete and in op it is not waiting for any task to complete it so executes consistently.
try with this
async def op(self):
await asyncio.sleep(2)
This time it will execute simultaneously as there is no computation required and system is free to give resources.