class LimitRateDriverMixin(object): requests_per_period = 3 period = 1 #seconds def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._queue = TaskQueue(self.requests_per_period, self.period) @wait_free_slot async def json(self, *args, **kwargs): return await super().json(*args, **kwargs) @wait_free_slot async def get_text(self, *args, **kwargs): return await super().get_text(*args, **kwargs) @wait_free_slot async def get_bin(self, *args, **kwargs): return await super().get_bin(*args, **kwargs) @wait_free_slot async def post_text(self, *args, **kwargs): return await super().post_text(*args, **kwargs) def close(self): super().close() self._queue.canel()
class LimitRateDriverMixin: requests_per_period = 3 period = 1 # seconds def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._queue = TaskQueue(self.requests_per_period, self.period) @wait_free_slot async def json(self, *args, **kwargs): return await super().json(*args, **kwargs) @wait_free_slot async def get_text(self, *args, **kwargs): return await super().get_text(*args, **kwargs) @wait_free_slot async def get_bin(self, *args, **kwargs): return await super().get_bin(*args, **kwargs) @wait_free_slot async def post_text(self, *args, **kwargs): return await super().post_text(*args, **kwargs) async def close(self): await super().close() self._queue.canel()
async def test_simple_usage(self): @wait_free_slot async def foo(a): pass size = 1 period = 1 q = TaskQueue(size, period) obj = Mock() obj._queue = q await foo(obj) self.assertEqual(q.qsize(), size - 1) q.canel()
def test_get_elem_and_pause(self): size = 1 period = 1 q = TaskQueue(size, period) self.assertEqual(q.qsize(), size) yield from q.get() self.assertEqual(q.qsize(), size - 1) yield from asyncio.sleep(period + 1) self.assertEqual(q.qsize(), size) q.canel()
async def test_with_pause(self): @wait_free_slot async def foo(a): pass size = 1 period = 1 q = TaskQueue(size, period) obj = Mock() obj._queue = q await q.get() start = time.time() await foo(obj) stop = time.time() self.assertEqual(math.floor(stop - start), period) self.assertEqual(q.qsize(), 0) q.canel()
def test_get_all_plus_one_elem(self): size = 1 period = 1 q = TaskQueue(size, period) self.assertEqual(q.qsize(), size) yield from q.get() start = time.time() yield from q.get() stop = time.time() self.assertEqual(math.floor(stop - start), period) self.assertEqual(q.qsize(), 0) q.canel()
async def test_long_init(self): size = 1 period = 1 q = TaskQueue(size, period) self.assertEqual(q.qsize(), size) await asyncio.sleep(period + 1) self.assertEqual(q.qsize(), size) q.canel()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._queue = TaskQueue(self.requests_per_period, self.period)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._queue = TaskQueue(self.requests_per_period, self.period)