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

Discussion in 'Python' started by F. GEIGER, May 20, 2005.

  1. F. GEIGER

    F. GEIGER Guest

    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.

    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.

    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!

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

    Kind regards
    Franz GEIGER
    F. GEIGER, May 20, 2005
    #1
    1. Advertising

  2. 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 - - Python, web & database development

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.5 (GNU/Linux)

    iD8DBQFCjZBDdIO4ozGCH14RAjnnAJ9mc+xjol3Ya8AyhQKByb6EuKmrRwCfRsjQ
    aH9eY/8sZk2VKt5ufoZwVX0=
    =X36A
    -----END PGP SIGNATURE-----
    Gerhard Haering, May 20, 2005
    #2
    1. Advertising

  3. F. GEIGER

    F. GEIGER Guest

    "Gerhard Haering" <> schrieb im Newsbeitrag
    news:...

    I've completely rewritten the db handling stuff, which formerly did too much
    behind the scenes (explicit is better than implicit...). Now everything
    looks much better. No unexpected errors and - most important - locking works
    as expected. I have no idea yet, what went wrong with the former (probably
    more pythonic) solution. It always did it with MySQL as backend.

    Anyway, I'm quite happy now, that I can work on a db, that is incorporated
    into the app itself.

    Thanks for your help, Gerhard! And, sorry for the troubles...

    Kind regards
    Franz GEIGER
    F. GEIGER, May 21, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    38
    Views:
    1,255
    Dennis Lee Bieber
    Feb 15, 2005
  2. Ankit Mehta
    Replies:
    1
    Views:
    1,390
    Simon Brooke
    Sep 29, 2006
  3. Gerhard Häring
    Replies:
    3
    Views:
    366
    Gerhard Häring
    Mar 13, 2008
  4. akineko
    Replies:
    3
    Views:
    2,597
    Jesse Noller
    Jan 29, 2009
  5. TDR
    Replies:
    3
    Views:
    161
    Daniel Berger
    Aug 31, 2007
Loading...

Share This Page