在Python中序列化Sqlite3 [英] Serializing Sqlite3 in Python

查看:210
本文介绍了在Python中序列化Sqlite3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了充分利用并发性,SQLite3允许线程以三种方式访问​​同一连接:

To fully utilize concurrency, SQLite3 allows threads to access the same connection in three ways:


  1. 单线程。在这种模式下,所有互斥都被禁用,SQLite在不止一个线程中同时使用是不安全的。

  2. 多线程。在这种模式下,SQLite可以安全地被多个线程使用,前提是在两个或更多线程中不同时使用单个数据库连接。

  3. 序列化。在序列化模式下,SQLite可以安全地由多个线程使用,没有限制。

有没有人知道如何使连接序列化为Python。

Python有check_same_thread在多线程和单线程之间切换;

Does anyone know how I can make the connection serialized in Python.
Python has "check_same_thread" which allows switching between multi-threading and single-threading; however, I can not find out how I should make it serialized.

推荐答案

Python SQLite模块不是线程安全的。如果禁用其检查,那么您需要确保所有代码都是序列化的,并且包括垃圾回收。 (我的APSW模块是线程安全的,也正确地处理错误消息线程安全问题)。

The Python SQLite module is not threadsafe. If you disable its checking then you need to ensure all code is serialized and that includes garbage collection. (My APSW module is threadsafe and also correctly handles the error message thread safety issues).

然而,在同一进程中同时使用多个独立连接是安全的,推荐你这样做。此外,请将数据库切换到向前写日志记录模式,即使大量写入也应该会获得非常好的性能。

It is however safe to use multiple independent connections concurrently in the same process and I would recommend you do that. Additionally switch the database into write ahead logging mode and you should get very good performance even with lots of writing.

这篇关于在Python中序列化Sqlite3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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