psycopg / asyncio设置甚至循环策略
希望你一切都好!
我对 psycopg 模块允许与PostgreSQL数据库进行交互。
当我启动http服务器时(我使用 fastapi and uvicorn )通常,我将请求发送给服务器,我有一个错误:
error connecting in 'pool-1': Psycopg cannot use the 'ProactorEventLoop' to run in async mode. Please use a compatible event loop, for instance by setting 'asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())'
我已经完成了错误解决方案建议的建议,也就是说,设置事件loop我做的WindowsSelectoreventlooplicy的策略。
import asyncio
from asyncio import WindowsSelectorEventLoopPolicy
asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())
我将此行放在主文件中(这是我的应用程序的中心文件) 但是仍然什么都没有,当我向服务器发送请求时,我仍然存在相同的错误。
但是奇怪的是,当我使用启动服务器时 - reload
选项(用于重新加载根据uvicorn/fastapi的文档,在修改的情况下,这些文件会自动自动进行,但不能适应生产,当我发送请求时,我没有错误,并且一切顺利进行,
您可以 正确地进行。告诉我问题的原因是什么以及如何解决问题?
PS:这是我从Windows PowerShell启动HTTP的方式,
这是我正常启动HTTP服务器(无效)的方式
uvicorn src.main:app --port 2314
,这是我使用-RELOAD选项启动HTTP服务器的方式(它可以很好地工作)
uvicorn src.main:app --port 2314 --reload
Hope you're well!
I have a problem with the Psycopg module allowing to interact with the PostgreSQL database.
When I start my HTTP server (I use FastAPI and Uvicorn) normally, and I send a request to my server, I have this error:
error connecting in 'pool-1': Psycopg cannot use the 'ProactorEventLoop' to run in async mode. Please use a compatible event loop, for instance by setting 'asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())'
I have already done what the error solution suggests, that is, set the event loop policy to WindowsSelectorEventLoopPolicy, which I did.
import asyncio
from asyncio import WindowsSelectorEventLoopPolicy
asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())
I put this line in my main file (which is the central file of my app)
But still nothing, I still have the same error when I send a request to my server.
But what is strange is that when I start my server with the --reload
option (which is used to reload the files automatically in case of modification but is not adapted for production, according to the documentation of Uvicorn/FastAPI), I have no error when I send my requests and everything goes correctly
Can you tell me what is the cause of the problem and how to solve it?
P.S: Here is how i start my HTTP from Windows Powershell
Here is how I start my HTTP server normally (which doesn't works)
uvicorn src.main:app --port 2314
Here is how I start my HTTP server with the --reload option (which works perfectly)
uvicorn src.main:app --port 2314 --reload
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Uvicorn使用
WindowsSelectoreventlooppolicy
策略使用reload
option> Windows默认情况下,否则不会。这就是为什么您面对这种不匹配行为的原因。更改另一个政策应该有效,也许是放错了位置。
以下工作:
如果我们删除设置策略的行,并且已安装
uvloop
(uvicorn
自动选择uvloop
(如果已安装)) ,我们可以看到打印了uvloop
策略。如果没有放错位置,我建议在
uvicorn
上开设讨论。免责声明:我是Uvicorn的维护者。
Uvicorn sets the
WindowsSelectorEventLoopPolicy
policy when using thereload
option and Windows by default, otherwise it doesn't. That's why you are facing this mismatch behavior.Changing another policy should have worked, maybe it's misplaced.
The below works:
If we remove the line on which we set the policy, and we have
uvloop
installed (uvicorn
automatically selectsuvloop
if installed), we can see that theuvloop
policy is printed.In case it's not misplaced, I recommend open a discussion on
uvicorn
.Disclaimer: I'm a maintainer of Uvicorn.
谢谢你!经过几次测试并再次阅读了Uvicorn的文档。
一切都有我的错
我从终端(在我的情况powershell中)直接从命令行(在我的情况下)启动了我启动UVICORN HTTP服务器(没有
- Reload
选项)的,我遇到了错误。因为这种做事的方式没有读取我在main.py文件中所做的导入,而只是启动了服务器而不关心任何事情,因此,错误要求我将事件循环策略设置为 windowsselectorectorectoreectorecorectoreventlooppolicy (我已经完成了),仅仅是因为从未达到定义事件循环策略的导入线。
而不是直接在终端中启动我的uvicorn服务器
像这样执行我的python代码,
,我必须在main.py中修改我的代码。
因此,现在我没有 带有
的http服务器 - 重新加载
这样的选项是@marcelo-trylesinski 说,
- reload
选项将事件循环设置为 WindowsSelectoreventloopolicy 。我希望可以在文档中通知 /提到这一点。再次感谢@marcelo-trylesinski 的帮助
Thank you! After having made several tests and having read again the documentation of uvicorn.
Everything was a little bit my fault
I launched the uvicorn HTTP server (without the
--reload
option) directly from the command line from a terminal (in my case Powershell), I got the error.Because this way of doing things didn't read the import I had done in my main.py file, but simply started the server without caring about anything, hence the error asking me to set the event loop policy to WindowsSelectorEventLoopPolicy (which I had done), simply because the import line defining my event loop policy was never reached.
So now instead of launching my uvicorn server directly in the terminal like this
I execute my python code like this
and I had to modify my code in main.py like this
The reason why it was working without any problem in the terminal is by running my HTTP server with the
--reload
option like thisis that as @marcelo-trylesinski said, the
--reload
option automatically sets event loop to WindowsSelectorEventLoopPolicy. I wish this could be notified / mentioned in the documentation.Thanks again to @marcelo-trylesinski for his help