M
M
mad_god2015-11-24 01:33:27
Python
mad_god, 2015-11-24 01:33:27

What are the best examples of multi-threaded Python script with sqlite database?

Need an example showing how to properly handle errors if a transaction was not successful.
Will there be an increase from using multiple threads in working with the sqlite database?
If I run one script, then the processing takes 101 minutes, with two scripts - 62 minutes.
Four scripts work for 52 minutes, but if several scripts work without error handling, then the database is smaller, not all data is successfully added.
I feel like I'm doing it wrong, so I ask and google at the same time. Maybe it makes sense to lock the database at the time of recording and put a cycle that waits for unlock?
We need an example that is used, for example, in an open project, as it is humanly done.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Philipp, 2015-11-24
@mad_god

Maybe you should use MariaDB? Or create indexes. You clearly have blocking issues.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sqlite3 as lite
import sys

try:
    con = lite.connect('test.db')

    cur = con.cursor()  

    cur.executescript("""
        DROP TABLE IF EXISTS Cars;
        CREATE TABLE Cars(Id INT, Name TEXT, Price INT);
        INSERT INTO Cars VALUES(1,'Audi',52642);
        INSERT INTO Cars VALUES(2,'Mercedes',57127);
        INSERT INTO Cars VALUES(3,'Skoda',9000);
        INSERT INTO Cars VALUES(4,'Volvo',29000);
        INSERT INTO Cars VALUES(5,'Bentley',350000);
        INSERT INTO Cars VALUES(6,'Citroen',21000);
        INSERT INTO Cars VALUES(7,'Hummer',41400);
        INSERT INTO Cars VALUES(8,'Volkswagen',21600);
        """)

    con.commit()
    
except lite.Error, e:
    
    if con:
        con.rollback()
        
    print "Error %s:" % e.args[0]
    sys.exit(1)
    
finally:
    
    if con:
        con.close()

Taken from here zetcode.com/db/sqlitepythontutorial

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question