python – SQLAlchemy线程中的事务是否安全?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – SQLAlchemy线程中的事务是否安全?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1476字,纯文字阅读大概需要3分钟。
内容图文
![python – SQLAlchemy线程中的事务是否安全?](/upload/InfoBanner/zyjiaocheng/898/233c9b9ce2014490bf5ea69583ddd14e.jpg)
我正在使用SQLAlchemy的表达式语言开发一个Web应用程序,而不是它的orm.我想在我的应用程序中使用多个线程,但我不确定线程??安全性.我正在使用this section of the documentation进行连接.我认为这是线程安全的,因为我在每个请求中引用了一个特定的连接.这个线程安全吗?
解决方法:
connections和sessions的文档说,线程不是线程安全的,也不打算在线程之间共享.
The Connection object is not thread-safe. While a Connection can be shared among threads using properly synchronized access, it is still possible that the underlying DBAPI connection may not support shared access between threads. Check the DBAPI documentation for details.
The Session is very much intended to be used in a non-concurrent fashion, which usually means in only one thread at a time.
The Session should be used in such a way that one instance exists for a single series of operations within a single transaction.
The bigger point is that you should not want to use the session with multiple concurrent threads.
在多个线程中使用相同的连接(和事务上下文)时,无法保证行为正确或一致.
您应该为每个线程使用一个连接或会话.如果需要保证数据,则应设置引擎或会话的隔离级别.对于Web应用程序,SQLAlchemy suggests每个请求周期使用一个连接.
This simple correspondence of web request and thread means that to associate a Session with a thread implies it is also associated with the web request running within that thread, and vice versa, provided that the Session is created only after the web request begins and torn down just before the web request ends.
内容总结
以上是互联网集市为您收集整理的python – SQLAlchemy线程中的事务是否安全?全部内容,希望文章能够帮你解决python – SQLAlchemy线程中的事务是否安全?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。