PDA

View Full Version : Advice using sqlite3 with threading



enricong
22nd December 2010, 18:46
The program is a GUI job manager to give an interface to allow the user to setup a job queue, and monitor the status of what is running.
I am using a database to store job parameters and results of jobs.
There is a separate module which allows me to actually send commands to start jobs and get data back.

Since I do not want the GUI to be frozen while jobs are running I have worker thread which is used to do all the work of sending out jobs, getting data, and storing it in a database.

ANYWAYS, here is my problem. I was using mysqldb and it was working fine, however, I decided to switch to sqlite3. However, when I try to execute a db command from my worker thread, I get the following errors:

QObject::setParent: New parent must be in the same thread as the previous parent
QFont: It is not safe to use text and fonts outside the GUI thread

1. Is using a thread a good way to implement what I am trying to do? I cannot think of another way to allow jobs to run (monitor the status) without having the GUI frozen.
2. How can I use sqlite3 within my worker thread? What would be the best approach here?

Thanks

Here are some Python Code excerpts:

def store_job(db, results)
db.execute("INSERT INTO `results` name values ?", results)

def process_job(client):
job_status = "Running Job"
client.start_job()

def run_jobs_thread():
client = job_module(jobid) ### job interface instance
process_job(client)
del client
job_status = "Storing Data"
if running:
store_job(db_cursor,client.results)
job_status = "Complete"

def update_job_status():
if jobt.isRunning():
mw.joblist.item(jobid,3).setText(job_status)

def monitor_log():
###check some stuff and update running variable

class jobs(QThread):
def run(self):
run_jobs_thread()

def init_form()
jobt = jobs(app)
timer = QTimer(app)
timer2 = QTimer(app)
QObject.connect(timer, QtCore.SIGNAL("timeout()"), update_job_status)
QObject.connect(timer2, QtCore.SIGNAL("timeout()"), monitor_status)
timer.start(1000)
timer2.start(2000)
running = False

boudie
22nd December 2010, 22:29
To avoid multiple threads with time-consuming processes you can also call QApplication:: processEvents to keep the application responsive.
Another way to avoid multi-threading is starting a new QProcess for every job that has to be done and connect some signals to your application to keep track of progress.