I'm testing out how multiprocessing works and would like an explanation why I'm getting this exception and if it is even possible to pass the sqlite3 Connection Object this way:
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.')
I'm getting this exception thrown:
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.
Sorry if this has been asked before but google couldn't seem to find a decent answer to the exception above.