I’m working on a personal project using SQLite and a whole lot of data. To speed it up, I want to do multiprocessing. What I’ve found when trying to implement this is that I get a pickling error if I have the sqlite connection created within the __init__
, but it doesn’t happen when created outside of __init__
. Why is this? Sample code showing the error below.
import sqlite3
import multiprocessing
class db_class:
def __init__(self):
self.conn = sqlite3.connect("test.db")
self.conn.execute("create table if not exists mytable (field INT)")
self.conn.commit()
self.conn.execute("insert into mytable VALUES (1)")
self.conn.commit()
def test(self, q):
self.conn.execute(q)
if __name__ == '__main__':
query = "select * from mytable"
db = db_class()
procA = multiprocessing.Process(target=db.test, args=(query,))
procB = multiprocessing.Process(target=db.test, args=(query,))
procA.start()
procB.start()
procA.join()
procB.join()