在 Windows 服务中启动 uvicorn 应用程序时,服务无法启动,并出现错误,表明信号只能在主线程中处理。
以下是事件日志中捕获的错误消息:
Traceback (most recent call last):
File "site-packages\win32\lib\win32serviceutil.py", line 839, in SvcRun
File "WindowsService.py", line 49, in SvcDoRun
File "site-packages\uvicorn\main.py", line 279, in run
File "site-packages\uvicorn\main.py", line 307, in run
File "asyncio\base_events.py", line 584, in run_until_complete
File "site-packages\uvicorn\main.py", line 319, in serve
File "site-packages\uvicorn\main.py", line 459, in install_signal_handlers
File "signal.py", line 47, in signal
ValueError: signal only works in main thread
该项目在调试模式下工作。
.\dist\WindowsService.exe debug
没有参数允许我指定不处理信号。
以下是我如何以实用的方式启动 uvicorn 应用程序:
uvicorn.run(app.main,
host=config['server']['host'],
port=config['server']['port'],
log_level=config['server']['loglevel'],
reload=False)
该应用程序使用win32serviceutil.ServiceFramework编译为冻结的可执行文件
pyinstaller -F --hidden-import=win32timezone --additional-hooks-dir pyinstaller-hooks WindowsService.py
我希望启动 Windows 服务并运行 uvicorn 应用程序。