[pysqlite] How do I use pysqlite in a multi-threading env.?

Gerhard Haering gh at ghaering.de
Fri May 20 03:22:43 EDT 2005


On Fri, May 20, 2005 at 08:42:54AM +0200, F. GEIGER wrote:
> In my wxPython-app a part of it gathers data, when a button is pressed, and
> stores it into a db.
> 
> The GUI part should display the stuff being stored in the db.
> 
> When both parts work on the same connection, I get "SQL statements in
> progress errors". Seems ok to me, you can't do that.

You get that error message with which pysqlite call?

> So, next step: Both parts get a separate connection. Now I get "Database
> locked" errors. Hmm, yes, it's GUI stuff after all, being all in the same
> thread.

One connection did not commit() or rollback() within the default
timeout (5 seconds). SQLite is not a good solution if you need
long transactions, because other SQLite connections are blocked, then.

> So, next step: I put the data gathering part into a real thread, that's
> started, wehn the "Scan" button is pressed. This way the "Database locked"-
> errors should got away. But now I get "SQLite objects created in a thread
> can only be used in that same thread.The object was created in thread id
> 3576 and this is thread id 1040". Hmm, true, I want to display db content,
> that was stored by an other thread. But that's what multi-threading
> capabilities are for!

Yes, but pysqlite's threadsafety level is 1, that means you cannot share
a connection object among multiple threads. In order to ensure that and
to avoid that you get random and unexplainable crashes, I implemented
this warning.

> I must be missing something here. Anyway, I'm a bit lost now, really. So,
> how are you doing such stuff with pysqlite?

Use multiple connections and commit/rollback early.

HTH,

-- Gerhard
-- 
Gerhard Häring - gh at ghaering.de - Python, web & database development
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20050520/c7577cd4/attachment.sig>


More information about the Python-list mailing list