question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Development using gunicorn + uvicorn

See original GitHub issue

I’m developing an API using API Star, gunicorn, and uvicorn. I would like to make my debugging life easier.

I’m able to get tracebacks about uncalled co-routines via environment variable PYTHONASYNCIODEBUG=1 or inside the application with:

asyncio.get_event_loop().set_debug(True)

So far so good. The asyncio docs suggest to additionally enable ResourceWarnings by running python -Wdefault [...]. Since I’m starting the application with ["gunicorn", "-c", "gunicorn.py", "app:app"] (inside Docker), I’m unsure how to do that. I have set

warnings.simplefilter("always", ResourceWarning)

inside my application but I don’t see any warnings. Maybe I’m failing to trigger the warning even though one of my routes awaits the following co-routine:

async def transport() -> str:
    LOGGER.debug("I transport.")
    trans = asyncio.Transport()
    return "Rapid"

According to the asyncio docs, not closing the transport should trigger the warning. I do see the logging output 'I transport.'.

Lastly, even with extensive configuration, I’m not seeing any uvicorn log statements. I’ve seen this mentioned around in other issues so maybe I’m just confused whether that’s solved or not. I use:

logging.dictConfig({
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'simple': {
                'format': "[%(name)s] [%(levelname)s] %(message)s",
            },
        },
        'handlers': {
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'simple'
            },
        },
        'loggers': {
            'uvicorn': {
                'level': 'DEBUG',
                'handlers': ['console']
            }
        },
        'root': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': True,
        },
})

Adding the handler on the uvicorn logger and separately changing the level should not be necessary due to the changes on the root logger but I was kinda desperate to try something.

Any advice or help is greatly appreciated. I’m happy to share Dockerfile and minimal app if required.

The versions I’m using are:

Python 3.6
apistar==0.5.41
gunicorn==19.9.0
uvicorn==0.3.2

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
tomchristiecommented, Sep 6, 2018

I guess my only remaining problem is that I’m raising an exception (ValueError) in one of my routes and I had expected to see mention of that in the logs in debug mode.

Indeed. That’s resolved just now - version 0.3.4. Previously the debug middleware was swallowing exceptions when it returned the 500 traceback. Now we respond with the 500 traceback, but still raise the exception so that it can be caught and logged.

https://github.com/encode/uvicorn/pull/190

0reactions
Midnightercommented, Sep 5, 2018

I guess my only remaining problem is that I’m raising an exception (ValueError) in one of my routes and I had expected to see mention of that in the logs in debug mode. However, I only get a server error and no mention of the exception. I can, of course, catch it and log it myself but I was curious if this is expected behaviour.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Server Workers - Gunicorn with Uvicorn - FastAPI
You can use Gunicorn (or also Uvicorn) as a process manager with Uvicorn workers to take advantage of multi-core CPUs, to run multiple...
Read more >
Deployment - Uvicorn
Gunicorn is probably the simplest way to run and manage Uvicorn in a production setting. Uvicorn includes a gunicorn worker class that means...
Read more >
Fastapi recommends use gunicorn with uvicorn workers why?
Currently i use uvicorn only in production but when i check the docs it says you should run uvicorn workers inside gunicorn, ...
Read more >
How To Set Up an ASGI Django App with Postgres, Nginx ...
You will configure the Gunicorn application server paired with Uvicorn, an ASGI implementation, to interface with your applications ...
Read more >
How to use Django with Uvicorn
Gunicorn is a robust web server that implements process monitoring and automatic restarts. This can be useful when running Uvicorn in a production ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found