我正在测试多处理是如何工作的,并想解释为什么我会得到这个异常,以及是否有可能以这种方式传递 sqlite3 连接对象:
import sqlite3
from multiprocessing import Queue, Process
def sql_query_worker(conn, query_queue):
# Creating the Connection Object here works...
#conn = sqlite3.connect('test.db')
while True:
query = query_queue.get()
if query == 'DO_WORK_QUIT':
break
c = conn.cursor()
print('executing query: ', query)
c.execute(query)
conn.commit()
if __name__ == "__main__":
connection = sqlite3.connect('test.db')
commands = ( '''CREATE TABLE IF NOT EXISTS test(value text)''',
'''INSERT INTO test VALUES('test value')''',
'''INSERT INTO test VALUES('test value')''',
'''INSERT INTO test VALUES('test value')''',
'''INSERT INTO test VALUES('test value')''',
'''INSERT INTO test VALUES('test value')''',
'''INSERT INTO test VALUES('test value')''',
'''INSERT INTO test VALUES('test value')''',
'''INSERT INTO test VALUES('test value')''',
'''INSERT INTO test VALUES('test value')''',
'''INSERT INTO test VALUES('test value')''',
'''INSERT INTO test VALUES('test value')''',
'''INSERT INTO test VALUES('test value')''',
'''INSERT INTO test VALUES('test value')''',
'''INSERT INTO test VALUES('test value')''',
'''DO_WORK_QUIT''',
)
cmd_queue = Queue()
sql_process = Process(target=sql_query_worker, args=(connection, cmd_queue))
sql_process.start()
for x in commands:
cmd_queue.put(x)
sql_process.join()
print('Done.')
我抛出了这个异常:
Process Process-1:
Traceback (most recent call last):
File "C:\Python33\lib\multiprocessing\process.py", line 258, in _bootstrap
self.run()
File "C:\Python33\lib\multiprocessing\process.py", line 95, in run
self._target(*self._args, **self._kwargs)
File "testmp.py", line 11, in sql_query_worker
c = conn.cursor()
sqlite3.ProgrammingError: Base Connection.__init__ not called.
Done.
抱歉,如果之前有人问过这个问题,但谷歌似乎无法找到上述异常的正确答案。