Comments (4)
This issue shows up repeatedly (like #1160 ), and I hope it gets an attention.
Here's a workaround to silence this verbose error messages at exit.
Hope it helps!
# Monkey patching to silence verbose error at uvicorn exit
# Put this code at the end of main.py
if is_dev_environment:
get = asyncio.Queue.get
async def new_get(self):
try:
return await get(self)
except asyncio.exceptions.CancelledError:
pass
asyncio.Queue.get = new_get
from uvicorn.
import sys
import threading
from fastapi import FastAPI
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from pystray import MenuItem as item
import pystray
from PIL import Image, ImageTk
import asyncio
import tkinter as tk
from asyncio import CancelledError
from contextlib import suppress
app = FastAPI()
# Allow all origins
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # You can replace this with specific origins if needed
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def start(lang, root=None, async_loop=None):
global mainwindow, canvas
# root.resizable(width=True, height=True)
root.iconbitmap("assets/icon.ico")
root.title('tkinter asyncio demo')
root.update_idletasks()
async def wait():
try:
tasks = asyncio.all_tasks(loop)
print(f'========{tasks}')
for task in tasks:
try:
# await asyncio.sleep(3600)
await task.cancel()
except asyncio.exceptions.CancelledError:
print("done")
except RuntimeError as err:
print('SIGINT or SIGTSTP raised')
print("cleaning and exiting")
sys.exit(1)
def quit_window(icon, item):
global loop,fastapi_thread
print('shutdown icon')
icon.stop()
print('shutdown server')
server.should_exit = True
server.force_exit = True
asyncio.run(server.shutdown())
print(f'0:{asyncio.all_tasks(loop)}')
try:
if loop.is_running():
# _logger.debug("Asked running asyncio loop to stop.")
loop.call_soon_threadsafe(loop.stop)
tasks = asyncio.all_tasks(loop)
print(tasks,'========')
for task in tasks:
try:
# await asyncio.sleep(3600)
task.cancel()
with suppress(CancelledError):
loop.run_until_complete(task)
except asyncio.exceptions.CancelledError:
print("done")
except RuntimeError as err:
print('SIGINT or SIGTSTP raised')
print("cleaning and exiting")
sys.exit(1)
print('shutdown root')
root.destroy()
asyncio.run(wait())
print('close loop')
print(f'1:{asyncio.all_tasks(loop)}')
# loop.close()
print('stop loop')
loop.stop()
# loop.run_until_complete(asyncio.gather(*[shutdown()]))
# import os
# os._exit(0)
def show_window(icon, item):
icon.stop()
root.after(0, root.deiconify)
def withdraw_window():
root.withdraw()
image = Image.open("assets/icon.ico")
menu = (item("Quit", quit_window), item("Show", show_window))
icon = pystray.Icon("name", image, "title", menu)
# icon.run_detached()
icon.run()
def start_fastapi_server(loop):
import uvicorn
global server
config = uvicorn.Config(app, loop=loop, host="0.0.0.0", port=8000)
server = uvicorn.Server(config)
try:
loop.run_until_complete(server.serve())
except KeyboardInterrupt:
print("Received Ctrl+C. Stopping gracefully...")
# Cancel all running tasks
for task in asyncio.Task.all_tasks():
task.cancel()
# Optionally: Close any open resources (sockets, files, etc.)
# Cleanup code here
finally:
loop.close()
def start_tkinter_app(async_loop):
global root, settings, db, canvas, locale
root = tk.Tk()
locale = 'en'
start(locale, root, async_loop)
root.protocol('WM_DELETE_WINDOW', withdraw_window)
root.mainloop()
if __name__ == "__main__":
global loop,fastapi_thread
loop=None
if sys.platform == 'win32':
asyncio.get_event_loop().close()
# On Windows, the default event loop is SelectorEventLoop, which does
# not support subprocesses. ProactorEventLoop should be used instead.
# Source: https://docs.python.org/3/library/asyncio-subprocess.html
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
else:
loop = asyncio.get_event_loop()
# Start FastAPI server in a separate thread
fastapi_thread = threading.Thread(target=start_fastapi_server,args=(
loop,)).start()
start_tkinter_app(loop)
loop.run_forever()
loop.close()
from uvicorn.
I have same effect then trying to close server from inside
Example code
from fastapi import FastAPI
import uvicorn
class ServerRunner:
def __init__(self):
self.app = FastAPI()
@self.app.get("/close")
async def close():
self.server.force_exit = True
self.server.should_exit = True
await self.server.shutdown()
config = uvicorn.Config(self.app, host='0.0.0.0', port=5432, forwarded_allow_ips='*')
self.server = uvicorn.Server(config)
def run(self):
self.server.run()
if __name__ == '__main__':
runner = ServerRunner()
runner.run()
Output
INFO: Started server process [3840]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:5432 (Press CTRL+C to quit)
INFO: 127.0.0.1:58584 - "GET /close HTTP/1.1" 200 OK
INFO: Shutting down
INFO: Finished server process [3840]
ERROR: Traceback (most recent call last):
File "D:\Projects\some_project\venv310\lib\site-packages\starlette\routing.py", line 686, in lifespan
await receive()
File "D:\Projects\some_project\venv310\lib\site-packages\uvicorn\lifespan\on.py", line 137, in receive
return await self.receive_queue.get()
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncio\queues.py", line 159, in get
await getter
asyncio.exceptions.CancelledError
Process finished with exit code 0
Versions
(venv310) PS D:\Projects\some_project> python --version
Python 3.10.11
(venv310) PS D:\Projects\some_project> pip show uvicorn
Name: uvicorn
Version: 0.25.0
from uvicorn.
I'll close this since I can't run the provided code. Please create a minimal, reproducible, example.
from uvicorn.
Related Issues (20)
- `--reload-include` doesn't work with hidden files e.g. `--reload-include .env` HOT 1
- ContextVars pollution when uvicorn installed without [standard] extensions HOT 3
- Error: [WinError 10054] An existing connection was forcibly closed by the remote host
- WebSockets and --max-requests does not reload Workers HOT 5
- Add support for Fowarded header (RFC 7239) HOT 3
- uvicorn may respond to requests sent after the client asks for the connection to be closed HOT 11
- Django, uvicorn, gunicorn inside docker HOT 1
- await request.is_disconnected() brings up large ClientDisconnected error in Uvicorn v0.28.0 HOT 7
- WebSocket does not complete coroutine after disconnection HOT 2
- The service process always interrupts abnormally after certain requests
- Child Processes Not Terminating with Uvicorn 0.29.0 HOT 7
- Requirement typo? typing_extensions>=4.0 HOT 1
- Unexcepted behavior while reloading HOT 13
- https://uvicorn.org/ should redirect to https://www.uvicorn.org/ HOT 1
- Unable to get the transport ssl context from the request. This prevents checking the Client provided certificate and matching up the provided CN against allowed users/server.
- Should server shutdown after receiving "lifespan.shutdown.failed"? HOT 6
- `uvicorn.run` `env_file` Not Passed Correctly
- The reloader process does not complete if an error occurs during operation.
- what's the relationship between "log_config" in uvicorn.run('main:app', host="127.0.0.1", port=8051, log_config="log.ini") and logging.getLogger(__name__)? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from uvicorn.