Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:not': 0.03; 'url:sourceforge': 0.03; '"""': 0.07; 'level,': 0.07; 'currently,': 0.09; 'cursor': 0.09; 'defines': 0.09; 'executed': 0.09; 'latter': 0.09; 'method,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows': 0.09; 'stating': 0.09; 'api': 0.11; 'python': 0.11; 'thread': 0.14; '>the': 0.16; 'adapter': 0.16; 'concurrency': 0.16; 'executed.': 0.16; 'means:': 0.16; 'message- id:@4ax.com': 0.16; 'once.': 0.16; 'permitted.': 0.16; 'processes.': 0.16; 'query,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'runtime.': 0.16; 'sqlite': 0.16; 'subject:Problems': 0.16; 'subject:object': 0.16; 'subject:threads': 0.16; 'terribly': 0.16; 'threads,': 0.16; 'threads.': 0.16; 'url:file': 0.16; 'exception': 0.16; 'weird': 0.16; 'library': 0.18; 'module': 0.19; 'commit': 0.19; 'result.': 0.19; 'separate': 0.22; 'creating': 0.23; 'integer': 0.24; 'url:home': 0.24; 'earlier': 0.24; 'versions': 0.24; '2.0': 0.26; 'query': 0.26; 'supported': 0.26; 'header:X-Complaints-To:1': 0.27; 'point': 0.28; 'errors': 0.30; '(which': 0.31; 'code': 0.31; 'apparently': 0.31; 'constant': 0.31; 'object.': 0.31; 'class': 0.32; 'interface': 0.32; 'another': 0.32; 'open': 0.33; 'url:python': 0.33; 'level.': 0.33; 'older': 0.33; 'sense': 0.34; 'maybe': 0.34; 'subject:from': 0.34; 'subject:with': 0.35; 'connection': 0.35; 'transaction': 0.35; 'objects': 0.35; 'but': 0.35; 'version': 0.36; '(we': 0.36; '+0200,': 0.36; 'module.': 0.36; 'url:org': 0.36; 'should': 0.36; 'so,': 0.37; 'two': 0.37; 'level': 0.37; 'being': 0.38; 'received:76': 0.38; 'server': 0.38; 'connections': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'highest': 0.39; 'subject:can': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'called': 0.40; 'ensure': 0.60; 'even': 0.60; 'read': 0.60; 'commands': 0.60; 'tell': 0.60; 'url:3': 0.61; 'simply': 0.61; 'further': 0.61; 'skip:n 10': 0.64; 'different': 0.65; 'worth': 0.66; 'url:0': 0.67; 'between': 0.67; 'approaches': 0.68; 'sharing': 0.69; 'study': 0.69; 'safe': 0.72; 'serial': 0.72; 'actually,': 0.84; 'execution.': 0.84; 'safe.': 0.84; 'url:psycopg': 0.84; 'utilized': 0.84; 'transactions': 0.91; 'url:latest': 0.91; 'hand,': 0.93; 'connection,': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: Problems with ZODB, I can not persist and object accessed from 2 threads Date: Tue, 06 May 2014 20:00:47 -0400 Organization: IISS Elusive Unicorn References: <87iopjyxsj.fsf@handshake.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: adsl-76-249-28-164.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 85 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1399420861 news.xs4all.nl 2838 [2001:888:2000:d::a6]:37731 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70993 On Tue, 06 May 2014 13:03:08 +0200, dieter declaimed the following: > >The database (we have called it "db") is global to all threads. >Each thread must open (and maybe close) its own connection to the >global database. You must never share the same connection or >objects loaded via the connection between threads - very weird >(and apparently non-deterministic) errors can result. > Actually, one should study the documentation for the dbapi adapter being used -- some may allow the connection (db = adapter.connect(...)) to be shared by threads, but require each thread to create its own cursor (cur = db.cursor()) Some may even allow cursors to be shared db-api defines a constant to tell one what is permitted. http://mysql-python.sourceforge.net/MySQLdb.html """ threadsafety Integer constant stating the level of thread safety the interface supports. This is set to 1, which means: Threads may share the module. 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. """ https://pysqlite.readthedocs.org/en/latest/sqlite3.html#multithreading """ Older SQLite versions had issues with sharing connections between threads. That’s why the Python module disallows sharing connections and cursors between threads. If you still try to do so, you will get an exception at runtime. The only exception is calling the interrupt() method, which only makes sense to call from a different thread. """ http://initd.org/psycopg/docs/usage.html#thread-and-process-safety """ The Psycopg module and the connection objects are thread-safe: many threads can access the same database either using separate sessions and creating a connection per thread or using the same connection and creating separate cursors. In DB API 2.0 parlance, Psycopg is level 2 thread safe. The difference between the above two approaches is that, using different connections, the commands will be executed in different sessions and will be served by different server processes. On the other hand, using many cursors on the same connection, all the commands will be executed in the same session (and in the same transaction if the connection is not in autocommit mode), but they will be serialized. """ http://www.firebirdtest.com/file/documentation/drivers_documentation/python/3.3.0/thread-safety-overview.html#definition-of-thread-safety """ Currently, the highest level of concurrency supported by any version of the Firebird client library is thread-safety at the connection level. When we say that the Firebird client library is thread-safe at the connection level, we mean that it is safe to use a particular connection in only one thread at a time, although the same connection can be manipulated by different threads in a serial fashion, and different connections can be manipulated by different threads in parallel. """ -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/