tools to help writing async python codes

async wrap

async_wrap[source]

async_wrap(func)

%%time
@async_wrap
def aSlowFunc(input_:str):
  time.sleep(2)
  return input_

## async func execute
import nest_asyncio, time
nest_asyncio.apply()
import time


async def runASlowFunc(input_):
  return await aSlowFunc(input_)
async def runLoop():
  rtup = (runASlowFunc(i) for i in range (10))
  r = await asyncio.gather(*rtup)
  return r
asyncio.run(runLoop())
CPU times: user 2.78 ms, sys: 3.69 ms, total: 6.47 ms
Wall time: 4.01 s
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
async def main():
    await asyncio.sleep(1)
    print('hello')

asyncio.run(main())
hello

thread mapping

asyncMap[source]

asyncMap(f:Callable, data:Iterable[Any], threads:int=5)

%%time
import time
asyncMap(lambda x: (x+1, time.sleep(1))[0] , range(100), threads = 100)[:10]
CPU times: user 12.3 ms, sys: 8.86 ms, total: 21.1 ms
Wall time: 1.03 s
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def aSlowFunc(x):
  time.sleep(1)
  return x
%%time
asyncMap(aSlowFunc, range(100))[:10]
CPU times: user 7.04 ms, sys: 268 µs, total: 7.31 ms
Wall time: 20 s
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
input_ = list(zip(range(10), range(1,11)))
print(input_)
asyncMap(lambda x: (lambda x,y: x+y )(x[0],x[1]), input_)
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (9, 10)]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

asyncAwaitMap

asyncAwaitMap[source]

asyncAwaitMap(f:Callable, data:Iterable[Any])

%%time
import nest_asyncio
nest_asyncio.apply()
asyncAwaitMap(aSlowFunc, range(100))[:10]
CPU times: user 21 ms, sys: 4.2 ms, total: 25.2 ms
Wall time: 17 s
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
input_ = list(zip(range(10), range(1,11)))
print(input_)
asyncAwaitMap(lambda x: (lambda x,y: x+y )(x[0],x[1]), input_)
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (9, 10)]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

AsyncThread

asyncThreadMap[source]

asyncThreadMap(f, data, threads=10)

%%time
def aSlowFunc(x):
  time.sleep(1)
  return x

list(asyncThreadMap(aSlowFunc, range(100)))[:10]
CPU times: user 9.18 ms, sys: 1.04 ms, total: 10.2 ms
Wall time: 10 s
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

AsyncProcess map

asyncProcessMap[source]

asyncProcessMap(f, data, threads=10)

%%time
def aSlowFunc(x):
  time.sleep(1)
  return x

list(asyncProcessMap(aSlowFunc, range(100)))[:10]
CPU times: user 24.6 ms, sys: 50.9 ms, total: 75.6 ms
Wall time: 10.1 s
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]