SQLiteconnect()由于独立进程上不相关的connect()而阻塞

2024-04-16 05:15:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想启动一个连接到sqlitedb的单独进程(最终目标是在该进程上运行服务)。这通常很好。但是,当我在启动进程之前连接到另一个db文件时,connect()命令是完全阻塞的:它既没有完成,也没有引发错误。你知道吗

import sqlite3, multiprocessing, time

def connect(filename):
    print 'Creating a file on current process!'
    sqlite3.connect(filename).close()

def connect_process(filename):
    def process_f():
        print 'Gets here...'
        conn = sqlite3.connect(filename)
        print '...but not here when local process has previously connected to any unrelated sqlite-file!!'
        conn.close()

    process = multiprocessing.Process(target=process_f)
    process.start()
    process.join()

if(__name__=='__main__'):
    connect_process('my_db_1') # Just to show that it generally works
    time.sleep(0.5)

    connect('any_file') # Connect to unrelated file

    connect_process('my_db_2') # Does not get to the end!!
    time.sleep(2)

这将返回:

> Gets here...
...but not here when local process has connected to any unrelated sqlite-file!!
Creating a file on current process!
Gets here..

因此,我们希望在...but not here when...末尾打印另一行

备注:

  • 我知道SQLite不能支持并发访问。然而,它应该 在这里工作有两个原因:1)我在本地进程上连接到的文件 与单独创建的不同。2) 连接 在前一个文件上,当进程得到 创建。你知道吗
  • 我在这里使用的唯一操作是连接到DB和 然后立即关闭连接(如果没有,则会创建文件) 现有)。我当然已经证实了如果我们 做任何有意义的事。。。你知道吗
  • 这段代码只是我真正想做的一个最小的工作示例。目标是测试服务 使用SQLite的。因此,在测试设置中,我需要创建一些mock SQLite文件…然后在一个单独的进程上启动服务,以便通过相应的客户端对其进行测试。你知道吗

Tags: 文件todbheretime进程defconnect