我使用sqlite数据库来存储数据。我的python程序有一个包含5个线程的线程池。我创建了一个数据库连接,并与所有5个线程共享,但有时它会抛出异常,而这个异常不是由任何sqlite异常或泛型异常捕获的,我的python脚本会自动终止。在寻找解决办法之后,我遇到了
How to share single SQLite connection in multi-threaded Python application
我创建了一个单独的连接,如下所示class ProcessJob(object):
....
def process_job(self):
job = queue.get()
if job = 'xyz':
with sqlite3.connect(database_path, check_same_thread=False, timeout = 10) as db_conn:
db_conn.execute("insert query on table ABC")
db_conn.commit()
elif job = 'pqr':
with sqlite3.connect(database_path, check_same_thread=False, timeout = 10) as db_conn:
db_conn.execute("update query on table ABC")
db_conn.commit()
elif job = 'mno':
with sqlite3.connect(database_path, check_same_thread=False, timeout = 10) as db_conn:
db_conn.execute("insert query on table FOO")
db_conn.commit()
class MyThread(therading.Thread):
....
process_job_obj = ProcessJob()
def run(self):
while True:
try:
process_job_obj.process_job()
except Exception as e:
logger.exception('Exception : %s'%e)
def main():
for i in range(5):
trd = MyThread()
trd.start()
if __name__ == "__main__":
main()
那么,这是一个正确的方法,还是有任何缺陷或机会停止/终止python脚本?在