You are not logged in.

#1 2010-08-20 13:36:23

gnu_D
Member
From: Macedonia
Registered: 2007-10-21
Posts: 46

SQLIte in python, using an instance of a database in threads.

Hi, I got a Database manager, it starts in background and waits someone to call add_task, when it does, the ask then is executed, and the content is then applied in the db.

But it raises an error, says:

ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was created in thread id -1218516784 and this is thread id -1223136400

This is the code: http://paste.pocoo.org/show/252409/

P.S: If someone can tell me where can I find some module which does that for me, for instance wait in the background for tasks.

Edit: typo big_smile.

Last edited by gnu_D (2010-08-20 13:43:54)


:: Python powered FOREVER ::

Offline

#2 2010-08-26 06:22:43

simlan
Member
From: Germany
Registered: 2010-05-30
Posts: 20

Re: SQLIte in python, using an instance of a database in threads.

A nice database handler: Sqlalchemy (http://www.sqlalchemy.org/)
Read the docs, there are examples how to manage threading and connection pooling.

Here is a specific section on threading:

http://www.sqlalchemy.org/docs/referenc … g-behavior

It does also clarifiy how to make your programm threadsafe.

Last edited by simlan (2010-08-26 06:26:35)

Offline

#3 2010-08-26 07:26:50

gnu_D
Member
From: Macedonia
Registered: 2007-10-21
Posts: 46

Re: SQLIte in python, using an instance of a database in threads.

Actually I tried, but the same thing happend.
So I left it be big_smile.


:: Python powered FOREVER ::

Offline

#4 2010-09-04 09:20:59

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: SQLIte in python, using an instance of a database in threads.

sqlite does not work well (at all?) with threaded / multiproc code accessing one database, unless you handle locking yourself. Just dont do it.

http://www.sqlite.org/faq.html#q5
http://www.sqlite.org/faq.html#q6  (note the section on specific compilation options)

If you need db access, and you need threading, I recommend you use some type of locking singleton that receives messages from worker threads, ala Actors or something.


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#5 2010-09-04 14:17:43

Mr.Elendig
#archlinux@freenode channel op
From: The intertubes
Registered: 2004-11-07
Posts: 4,094

Re: SQLIte in python, using an instance of a database in threads.

The other obligatory question is: Why threads? With python threads you can't run more than one task at a time anyway.

You might want to consider some async lib ala twisted instead.


Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest

Offline

Board footer

Powered by FluxBB