# 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

I have the above codes and workflow file, it is successful when executing pytest on my local machine, but if run it with GitHub actions, the workflow cannot be stopped automatically, for other say, the step 'Test with pytest' is still in-progress.

If I remove the code 'nest_asyncio.apply()', the workflow works well, but I know I need this code because I will use asyncio.get_event_loop() for doing other things. So must keep it in my code.

Does anyone have any ideas about this issue? What should I do could make the workflow works normally? Thanks in advance.

1

There are 1 answers

0
beckcl On

The event loop patching seems to mess something up, but this:

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

works for me. Note the added async.