我有多個測試文件,每個文件都有一個異步夾具,如下所示:@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 進(jìn)行并行化。另外我有這個裝飾器:@toolz.currydef 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我有一個函數(shù)使用它:@throttle(10)def f(): ...現(xiàn)在f正在從多個測試文件調(diào)用,并且我收到一個異常,告訴我無法使用不同事件循環(huán)中的信號量。我嘗試轉(zhuǎn)向會話級事件循環(huán)裝置:@pytest.fixture(scope="session", autouse=True)def event_loop(request): loop = asyncio.get_event_loop_policy().new_event_loop() yield loop loop.close()但這只給了我:ScopeMismatch:您嘗試使用“模塊”范圍請求對象訪問“函數(shù)”范圍固定裝置“event_loop”,涉及工廠是否有可能讓 xdist + 異步裝置 + 信號量一起工作?
pytest 與會話范圍的固定裝置和 asyncio 相關(guān)的問題
呼喚遠(yuǎn)方
2023-07-11 16:39:36