可以将 SQLAlchemy 配置为非阻塞吗? [英] Can SQLAlchemy be configured to be non-blocking?

查看:28
本文介绍了可以将 SQLAlchemy 配置为非阻塞吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的印象是通过 SQLAlchemy 调用数据库会阻塞并且不适合在同步代码以外的任何地方使用.我是否正确(我希望我不是!)或者有没有办法将其配置为非阻塞?

I'm under the impression that database calls through SQLAlchemy will block and aren't suitable for use in anything other than synchronous code. Am I correct (I hope I'm not!) or is there a way to configure it to be non-blocking?

推荐答案

您可以使用 gevent 以非阻塞方式使用 SQLA.这是使用 psycopg2 的示例,使用 psycopg2 的 协程支持:

You can use SQLA in a non-blocking style using gevent. Here's an example using psycopg2, using psycopg2's coroutine support:

https://bitbucket.org/zzzeek/green_sqla/

我也听说人们对 pymysql 使用相同的想法.由于 pymysql 是纯 Python 并使用套接字库,因此 gevent 将套接字库修补为异步.

I've also heard folks use the same idea with pymysql. As pymysql is in pure Python and uses the sockets library, gevent patches the socket library to be asynchronous.

这篇关于可以将 SQLAlchemy 配置为非阻塞吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆