error when use multithreading and mysqldb

前端 未结 3 543
滥情空心
滥情空心 2020-12-29 00:54

getting error when multithreader program access data .

Exception in thread Thread-2:

ProgrammingError: (2014, \"Commands out of sync; you can\'t run this co         


        
相关标签:
3条回答
  • 2020-12-29 01:06

    here is detail about the error: http://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html.

    Mysqldb's manual suggest these:

    Don't share connections between threads. It's really not worth your effort or mine, and in the end, will probably hurt performance, since the MySQL server runs a separate thread for each connection. You can certainly do things like cache connections in a pool, and give those connections to one thread at a time. If you let two threads use a connection simultaneously, the MySQL client library will probably upchuck and die. You have been warned.

    For threaded applications, try using a connection pool. This can be done using the Pool module

    See more information search keyword threadsafety on MySQLdb manual,

    0 讨论(0)
  • 2020-12-29 01:07

    Thanks to your few informations, I can only guess.

    Probably you access the database from several threads without locking. That's bad.

    You should hold a lock to a threading.Lock() or threading.RLock() while accessing your DB. This prevents several threads from interfering with other thread's actions.

    0 讨论(0)
  • 2020-12-29 01:13

    According to PEP 249, data access modules have a module level constant threadsafety:

    Integer constant stating the level of thread safety the interface supports. Possible values are:

    0 Threads may not share the module.
    1 Threads may share the module, but not connections.
    2 Threads may share the module and connections.
    3 Threads may share the module, connections and cursors.

    Sharing in the above context means that two threads may use a resource without wrapping it using a mutex semaphore to implement resource locking. Note that you cannot always make external resources thread safe by managing access using a mutex: the resource may rely on global variables or other external sources that are beyond your control.

    According to MySQLdb User's Guide, the module supports level 1.

    The MySQL protocol can not handle multiple threads using the same connection at once. Some earlier versions of MySQLdb utilized locking to achieve a threadsafety of 2. While this is not terribly hard to accomplish using the standard Cursor class (which uses mysql_store_result()), it is complicated by SSCursor (which uses mysql_use_result(); with the latter you must ensure all the rows have been read before another query can be executed. It is further complicated by the addition of transactions, since transactions start when a cursor execute a query, but end when COMMIT or ROLLBACK is executed by the Connection object. Two threads simply cannot share a connection while a transaction is in progress, in addition to not being able to share it during query execution. This excessively complicated the code to the point where it just isn't worth it.

    The general upshot of this is: Don't share connections between threads. It's really not worth your effort or mine, and in the end, will probably hurt performance, since the MySQL server runs a separate thread for each connection. You can certainly do things like cache connections in a pool, and give those connections to one thread at a time. If you let two threads use a connection simultaneously, the MySQL client library will probably upchuck and die. You have been warned.

    0 讨论(0)
提交回复
热议问题