import asyncio from aioknife.synclike import Group import logging import os logging.basicConfig(level=getattr(logging, os.environ.get("LOGLEVEL", "INFO"))) async def run(i): print("before", i) await asyncio.sleep(0.5) print("after", i) return i g = Group() for i in range(10): g.go(run, i) print(g.wait())
import logging import time from aioknife.synclike import Group import fakes logging.basicConfig(level=logging.DEBUG) st = time.time() g = Group(limit=5) for i in range(10): g.go(fakes.req, i) g.go(fakes.req_await, i) print(g.wait()) print(time.time() - st)
import logging import os logging.basicConfig(level=getattr(logging, os.environ.get("LOGLEVEL", "INFO"))) async def run(i): print("before", i) await asyncio.sleep(0.5) if i == 2: raise Exception("hmm") print("after", i) return i def callback(fut): if fut.exception() is not None: print("E", fut.exception()) else: print("R", fut.result()) g = Group(concurrency=2) for i in range(10): fut = g.go(run, i) fut.add_done_callback(callback) try: print(g.wait()) except Exception as e: print("!!!", e)
import asyncio from aioknife.synclike import Group import logging import os logging.basicConfig(level=getattr(logging, os.environ.get("LOGLEVEL", "INFO"))) async def run(i): print("before", i) await asyncio.sleep(0.5) if i == 8: raise Exception("hmm") print("after", i) return i g = Group() for i in range(10): g.go(run, i) print(g.wait(return_exceptions=True))
import asyncio from aioknife.synclike import Group # https://qiita.com/castaneai/items/9cc33817419896667f34 async def func(name, n): for i in range(n): print(f"func{name}") await asyncio.sleep(1) return n g = Group() g.go(func, "1", 3) g.go(func, "2", 3) print(g.wait())
from aioknife.synclike import Group import asyncio # https://qiita.com/takechanman1228/items/4cc4728abc8ffbb506fe # 時間がかかる処理を含む関数 async def compute(x, y): print("Compute %s + %s ..." % (x, y)) # 時間がかかる処理にはawaitつける await asyncio.sleep(1.0) return x + y g = Group() g.go(compute, 1, 2) print(f"1 + 2 = {g.wait()}")
import random import logging import asyncio from aioknife.synclike import Group # https://qiita.com/icoxfog417/items/07cbf5110ca82629aca0 async def sleeping(order, seconds, hook=None): await asyncio.sleep(seconds) if hook: hook(order) return order logging.basicConfig(level=logging.DEBUG) g = Group(limit=3) for i in range(9): num = random.randint(0, 3) g.go(sleeping, str(num), num) for d in g.wait(): print(f"limited parallel: {d}")
async def main(): g = Group(loop=loop) for i in range(10): g.go(run, i) print("R", await g.execute())
import asyncio from aioknife.synclike import Group # https://qiita.com/icoxfog417/items/07cbf5110ca82629aca0 Seconds = [("first", 5), ("second", 0), ("third", 3)] async def sleeping(order, seconds, hook=None): await asyncio.sleep(seconds) if hook: hook(order) return order def notify(order): print(f"{order} has just finished.") g = Group() for s in Seconds: g.go(sleeping, *s, hook=notify) for r in g.wait(): print(f"asyncio.gather result: {r}")
def _makeOne(self, *args, **kwargs): from aioknife.synclike import Group return Group(*args, **kwargs)
import asyncio from aioknife.synclike import Group import logging import os logging.basicConfig(level=getattr(logging, os.environ.get("LOGLEVEL", "INFO"))) async def run(i): print("before", i) await asyncio.sleep(0.5) print("after", i) return i g = Group(concurrency=5) for i in range(10): g.go(run, i) print(g.wait())
import asyncio from aioknife.synclike import Group # from https://qiita.com/takechanman1228/items/4cc4728abc8ffbb506fe # 時間がかかる処理を含む関数 async def factorial(name, number): f = 1 for i in range(2, number + 1): print("Task %s: Compute factorial(%s)..." % (name, i)) await asyncio.sleep(1) f *= i print("Task %s: factorial(%s) = %s" % (name, number, f)) g = Group() g.go(factorial, "A", 2) g.go(factorial, "B", 3) g.go(factorial, "C", 4) g.wait()