async def test_call_raising(self): loop = asyncio.get_event_loop() queue = MethodQueue(num_workers=2, loop=loop) all_done = asyncio.Event(loop=loop) calls = 0 async with queue: async def myfun(x, y): nonlocal calls calls += 1 if calls >= 9: all_done.set() raise KeyError(x) futures = [] for i in range(10): fut = loop.create_future() if i == 3: fut.cancel() await queue.call(fut, myfun, i, i) futures.append(fut) await all_done.wait() for future in futures: if not future.cancelled(): with pytest.raises(KeyError): future.result()
async def test_flush(self): queue = MethodQueue(num_workers=2) calls = 0 all_done = asyncio.Event() async def myfun(x, y): nonlocal calls calls += 1 if calls >= 9: all_done.set() for i in range(10): await queue.cast(myfun, i, i) await queue.flush() await all_done.wait()
async def test_call(self, loop): queue = MethodQueue(num_workers=2, loop=loop) async with queue: async def myfun(x, y): return x * y futures = [] for i in range(10): fut = loop.create_future() await queue.call(fut, myfun, i, i) futures.append(fut) assert await asyncio.gather(*futures) == [ i * i for i, _ in enumerate(futures) ]
async def test_cast(self, loop): queue = MethodQueue(num_workers=2, loop=loop) calls = 0 all_done = asyncio.Event(loop=loop) async with queue: async def myfun(x, y): nonlocal calls calls += 1 if calls >= 9: all_done.set() for i in range(10): await queue.cast(myfun, i, i) await all_done.wait()
def __init__(self, *args: Any, **kwargs: Any) -> None: super().__init__(*args, **kwargs) self._method_queue = MethodQueue(loop=self.loop, beacon=self.beacon) self.add_dependency(self._method_queue) self._thread = self._new_consumer_thread() self.add_dependency(self._thread)