mysqldb + multi-threading

skip at pobox.com skip at pobox.com
Fri Sep 8 22:29:52 CEST 2006


    Bryan> MySQL has its own process. It doesn't know nor care whether your
    Bryan> application makes connections from different processes or
    Bryan> different threads in the same process.

Yes, but on the client side the data structures may or may not be
thread-safe.  A session on my web server:

    [skip at orca ~]$ python
    Python 2.3.4 (#1, Mar 10 2006, 06:12:09)
    [GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import MySQLdb
    >>> MySQLdb.threadsafety
    1
    >>> MySQLdb.version_info
    (1, 1, 7, 'final', 1)

Looking up threadsafety in PEP 249 we see:

    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.

So if you want to use multiple threads they can't share connections.

    >> My gut feeling is to use threads (note: each transaction is finite,
    >> so the process/thread die fairly quickly).

    Bryan> Go with your gut. Python threads are reasonably portable, and
    Bryan> work well on modern MS-Windows.

Maybe ignore your gut and read the documentation. ;-)

Skip



More information about the Python-list mailing list