{"id":18438681,"url":"https://github.com/seapagan/capture-uvicorn-output","last_synced_at":"2026-03-07T03:32:09.804Z","repository":{"id":227285917,"uuid":"770985552","full_name":"seapagan/capture-uvicorn-output","owner":"seapagan","description":"Example how to capture the output from a process in python","archived":false,"fork":false,"pushed_at":"2024-06-21T11:34:54.000Z","size":493,"stargazers_count":3,"open_issues_count":9,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-31T05:39:38.226Z","etag":null,"topics":["capture","non-blocking","python","stdout","subprocess","uvicorn"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/seapagan.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-12T13:44:30.000Z","updated_at":"2025-05-10T14:04:49.000Z","dependencies_parsed_at":"2024-03-12T15:28:36.946Z","dependency_job_id":"21b94670-b844-4702-9aa6-a7594e29066d","html_url":"https://github.com/seapagan/capture-uvicorn-output","commit_stats":null,"previous_names":["seapagan/capture-uvicorn-output"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/seapagan/capture-uvicorn-output","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seapagan%2Fcapture-uvicorn-output","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seapagan%2Fcapture-uvicorn-output/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seapagan%2Fcapture-uvicorn-output/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seapagan%2Fcapture-uvicorn-output/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seapagan","download_url":"https://codeload.github.com/seapagan/capture-uvicorn-output/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seapagan%2Fcapture-uvicorn-output/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30206573,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-07T03:24:23.086Z","status":"ssl_error","status_checked_at":"2026-03-07T03:23:11.444Z","response_time":53,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["capture","non-blocking","python","stdout","subprocess","uvicorn"],"created_at":"2024-11-06T06:21:07.159Z","updated_at":"2026-03-07T03:32:09.761Z","avatar_url":"https://github.com/seapagan.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Capture Uvicorn Output\n\nThis is just an example of how to capture the `stdout` and `stderr` output of a\nprocess and use it in a Python application.\n\nIt uses the [Threading](https://docs.python.org/3/library/threading.html) and\n[Queue](https://docs.python.org/3/library/queue.html) modules to capture the\noutput of a [Uvicorn](https://www.uvicorn.org/) server and then use it in the\nmain application.\n\nIt also shows how to cleanly catch the ctrl-c keystroke and pass that on to the\n`uvicorn` server to allow it and the application to shut down cleanly.\n\nAs a uvicorn server, it includes a simple API server based on\n[FastAPI](https://fastapi.tiangolo.com/), though the same technique could be\nused with most other types of processes.\n\n## Setup\n\nInstall the dependencies using Poetry:\n\n```console\n$ poetry install\n```\n\nThen, activate the virtual environment:\n\n```console\n$ poetry shell\n```\n\nNow you can run the application:\n\n```console\n$ python main.py\n```\n\n## Code\n\nThe important code is in the `main.py` file, in the `__call__` method of the\nApp class. This method does the following:\n\n- Set up the `uvicorn` server and start it.\n- Sets up a `Queue` to capture the output.\n- Start a separate thread to fill that queue from the `uvicorn` server's output.\n\nThe main thread then reads the output. from the queue and simply prints it for\nnow.\n\nThe `server.py` file just creates a very simple API server using `FastAPI` with\none route that returns a simple JSON response on the root\n('\u003chttp://localhost:8000/\u003e')\n\nI will write a documentation page to explain this in more detail soon.\n\n## Example Run\n\nBelow is an example of the output from running the application, showing the\n`ctrl-c` being pressed and the application shutting down cleanly.\n\n```console\n$ python main.py\nINFO:     Will watch for changes in these directories: ['/home/seapagan/code/capture-uvicorn-output']\nINFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)\nINFO:     Started reloader process [167389] using WatchFiles\nINFO:     Started server process [167396]\nINFO:     Waiting for application startup.\nINFO:     Application startup complete.\nINFO:     127.0.0.1:56914 - \"GET / HTTP/1.1\" 200 OK\nINFO:     127.0.0.1:46182 - \"GET / HTTP/1.1\" 200 OK\nINFO:     Ctrl-C pressed, Shutting Down...\nINFO:     Shutting down\nINFO:     Waiting for application shutdown.\nINFO:     Application shutdown complete.\nINFO:     Finished server process [167396]\nINFO:     Stopping reloader process [167389]\n\n```\n\n## License\n\nThis project is released under the terms of the MIT license.\n\n## Credits\n\nThe original Python boilerplate for this package was created using\n[Pymaker](https://github.com/seapagan/py-maker) by [Grant\nRamsay](https://github.com/seapagan) (me :smile:)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseapagan%2Fcapture-uvicorn-output","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseapagan%2Fcapture-uvicorn-output","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseapagan%2Fcapture-uvicorn-output/lists"}