Пример #1
0
async def worker_pool(request, loop) -> WorkerPool:
    async with WorkerPool(
        PROCESS_NUM,
        initializer=setproctitle,
        initializer_args=(f"[WorkerPool] {request.node.name}",),
        statistic_name=request.node.name,
    ) as pool:
        yield pool
Пример #2
0
async def test_incomplete_task_pool_reuse(request, worker_pool: WorkerPool):
    def get_stats() -> dict:
        stats = {}
        for metric in aiomisc.get_statistics(WorkerPoolStatistic):
            if metric.name == request.node.name:
                stats[metric.metric] = metric.value
        return stats

    stats = get_stats()
    while stats["spawning"] < PROCESS_NUM:
        await asyncio.sleep(0.1)
        stats = get_stats()

    pids_start = set(worker_pool.pids)

    await asyncio.gather(
        *[
            worker_pool.create_task(getpid)
            for _ in range(worker_pool.workers * 4)
        ]
    )

    with pytest.raises(asyncio.TimeoutError):
        await asyncio.wait_for(
            asyncio.gather(
                *[
                    worker_pool.create_task(sleep, 600)
                    for _ in range(worker_pool.workers)
                ]
            ), timeout=1,
        )

    await asyncio.gather(
        *[
            worker_pool.create_task(getpid)
            for _ in range(worker_pool.workers * 4)
        ]
    )

    pids_end = set(worker_pool.pids)

    assert list(pids_start) == list(pids_end)
Пример #3
0
async def test_initializer(worker_pool):
    pool = WorkerPool(
        1, initializer=initializer, initializer_args=("foo",),
        initializer_kwargs={"spam": "egg"},
    )
    async with pool:
        args, kwargs = await pool.create_task(get_initializer_args)

    assert args == ("foo",)
    assert kwargs == {"spam": "egg"}

    async with WorkerPool(1, initializer=initializer) as pool:
        args, kwargs = await pool.create_task(get_initializer_args)

    assert args == ()
    assert kwargs == {}

    async with WorkerPool(1) as pool:
        args, kwargs = await pool.create_task(get_initializer_args)

    assert args is None
    assert kwargs is None
Пример #4
0
async def test_bad_initializer(worker_pool):
    pool = WorkerPool(1, initializer=bad_initializer)

    with pytest.raises(ZeroDivisionError):
        async with pool:
            await pool.create_task(get_initializer_args)
Пример #5
0
async def worker_pool(loop) -> WorkerPool:
    async with WorkerPool(4) as pool:
        yield pool