- use itertools.count() plus mutex for Query _new_runid, psycopg2 server

side cursor names, mentinoed in [ticket:2247]
This commit is contained in:
Mike Bayer
2011-08-06 14:21:19 -04:00
parent 669d0c47f8
commit c13dee0413
5 changed files with 25 additions and 14 deletions
+5
View File
@@ -21,6 +21,11 @@ CHANGES
the database regardless of whether C
extensions are in use or not.
- postgresql
- Use an atomic counter as the "random number"
source for server side cursor names;
conflicts have been reported in rare cases.
- ext
- Added local_attr, remote_attr, attr accessors
to AssociationProxy, providing quick access
@@ -97,7 +97,6 @@ The psycopg2 dialect will log Postgresql NOTICE messages via the
"""
import random
import re
import logging
@@ -165,6 +164,8 @@ SERVER_SIDE_CURSOR_RE = re.compile(
r'\s*SELECT',
re.I | re.UNICODE)
_server_side_id = util.counter()
class PGExecutionContext_psycopg2(PGExecutionContext):
def create_cursor(self):
# TODO: coverage for server side cursors + select.for_update()
@@ -187,7 +188,7 @@ class PGExecutionContext_psycopg2(PGExecutionContext):
if is_server_side:
# use server-side cursors:
# http://lists.initd.org/pipermail/psycopg/2007-January/005251.html
ident = "c_%s_%s" % (hex(id(self))[2:], hex(random.randint(0, 65535))[2:])
ident = "c_%s_%s" % (hex(id(self))[2:], hex(_server_side_id())[2:])
return self._dbapi_connection.cursor(ident)
else:
return self._dbapi_connection.cursor()
+1 -11
View File
@@ -3161,14 +3161,4 @@ class AliasOption(interfaces.MapperOption):
query._from_obj_alias = sql_util.ColumnAdapter(alias)
_runid = 1L
_id_lock = util.threading.Lock()
def _new_runid():
global _runid
_id_lock.acquire()
try:
_runid += 1
return _runid
finally:
_id_lock.release()
_new_runid = util.counter()
+1 -1
View File
@@ -27,7 +27,7 @@ from langhelpers import iterate_attributes, class_hierarchy, \
duck_type_collection, assert_arg_type, symbol, dictlike_iteritems,\
classproperty, set_creation_order, warn_exception, warn, NoneType,\
constructor_copy, methods_equivalent, chop_traceback, asint,\
generic_repr
generic_repr, counter
from deprecations import warn_deprecated, warn_pending_deprecation, \
deprecated, pending_deprecation
+15
View File
@@ -610,6 +610,21 @@ def constructor_copy(obj, cls, **kw):
return cls(**kw)
def counter():
"""Return a threadsafe counter function."""
lock = threading.Lock()
counter = itertools.count(1L)
def next():
lock.acquire()
try:
return counter.next()
finally:
lock.release()
return next
def duck_type_collection(specimen, default=None):
"""Given an instance or class, guess if it is or is acting as one of
the basic collection types: list, set and dict. If the __emulates__