【问题标题】:Workflow cannot be stoped automatically after running pytest command on github action when using nest_asyncio.apply() in fastapi在 fastapi 中使用 nest_asyncio.apply() 时,在 github 操作上运行 pytest 命令后无法自动停止工作流
【发布时间】:2022-08-04 01:10:27
【问题描述】:
# main.py
from fastapi import FastAPI
import nest_asyncio

nest_asyncio.apply()
app = FastAPI()


@app.get(\'/hello\')
def hello():
    return {\'msg\': \'hello\'}
# test_main.py
from .main import app

client = TestClient(app)


def test_hello():
    res = client.get(\'/hello\')
    assert res.status_code == 200
# python-app.yml workflow file
name: Python application

on:
  push:
    branches: [ \"main\" ]
  pull_request:
    branches: [ \"main\" ]

permissions:
  contents: read

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Set up Python 3.10
      uses: actions/setup-python@v3
      with:
        python-version: \"3.10\"
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install flake8 pytest
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

    - name: Test with pytest
      run: |
        pytest
# requirements.txt
fastapi == 0.78.0
pytest == 7.1.2
nest_asyncio == 1.5.5

我有上面的代码和工作流文件,在我的本地机器上执行pytest时是成功的,但是如果用GitHub操作运行它,工作流不能自动停止,换句话说,步骤\'Test with pytest\'仍然是进行中。

如果我删除代码 \'nest_asyncio.apply()\',工作流程运行良好,但我知道我需要此代码,因为我将使用 asyncio.get_event_loop() 来做其他事情。所以必须将它保存在我的代码中。

有人对这个问题有任何想法吗?我应该怎么做才能使工作流程正常工作?提前致谢。

标签: python pytest github-actions fastapi nest-asyncio


【解决方案1】:

事件循环修补似乎搞砸了,但这是:

@app.get('/hello')
async def hello():
    return {'msg': 'hello'}

为我工作。注意添加的异步。

【讨论】:

    猜你喜欢
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 2021-08-29
    • 2022-06-14
    • 2021-06-08
    • 2020-11-20
    • 1970-01-01
    相关资源
    最近更新 更多