Python 会话范围固定装置和异步IO的pytest问题

Python 会话范围固定装置和异步IO的pytest问题,python,pytest,python-asyncio,semaphore,pytest-xdist,Python,Pytest,Python Asyncio,Semaphore,Pytest Xdist,我有多个测试文件,每个文件都有一个异步装置,如下所示: @pytest.fixture(scope="module") def event_loop(request): loop = asyncio.get_event_loop_policy().new_event_loop() yield loop loop.close() @pytest.fixture(scope="module") async def some_fix

我有多个测试文件,每个文件都有一个异步装置,如下所示:


@pytest.fixture(scope="module")
def event_loop(request):
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close()


@pytest.fixture(scope="module")
async def some_fixture():
    return await make_fixture()
我正在使用xdist进行并行化。此外,我还有一个装饰师:

@toolz.curry
def throttle(limit, f):
    semaphore = asyncio.Semaphore(limit)

    @functools.wraps(f)
    async def wrapped(*args, **kwargs):
        async with semaphore:
            return await f(*args, **kwargs)

    return wrapped
我有一个函数使用它:

@throttle(10)
def f():
    ...
现在从多个测试文件调用
f
,我得到一个异常,告诉我不能使用来自不同事件循环的信号量

我尝试移动到会话级事件循环装置:



@pytest.fixture(scope="session", autouse=True)
def event_loop(request):
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close()

但这只给了我:

作用域不匹配:您试图使用“模块”作用域请求对象访问“函数”作用域fixture“事件\循环”,涉及工厂


甚至可以让xdist+异步fixture+信号量一起工作吗?

最终使用以下
conftest.py
使其工作:

导入异步IO
导入pytest
@pytest.fixture(scope=“session”)
def事件_循环():
返回asyncio.get\u event\u loop()

这对我来说很有效,但pytest asyncio似乎有一个bug