Skip to content

Commit

Permalink
make sure the server object is threadsafe.
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitc committed Feb 9, 2011
1 parent a314a6e commit b43bcf3
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions couchdbkit/client.py
Expand Up @@ -35,6 +35,7 @@
from itertools import groupby
from mimetypes import guess_type
import re
import threading
import time
import urlparse
import warnings
Expand Down Expand Up @@ -95,6 +96,7 @@ def __init__(self, uri='http://127.0.0.1:5984',
pool_instance=pool_instance,
filters=filters)
self._uuids = []
self._lock = threading.Lock()

def info(self):
""" info of server
Expand Down Expand Up @@ -179,15 +181,19 @@ def next_uuid(self, count=None):
"""
return an available uuid from couchdbkit
"""
if count is not None:
self._uuid_batch_count = count
else:
self._uuid_batch_count = self.uuid_batch_count

self.uuids = self.uuids or []
if not self._uuids:
self._uuids = self.uuids(count=self._uuid_batch_count)["uuids"]
return self._uuids.pop()
self._lock.acquire()
try:
if count is not None:
self._uuid_batch_count = count
else:
self._uuid_batch_count = self.uuid_batch_count

self.uuids = self.uuids or []
if not self._uuids:
self._uuids = self.uuids(count=self._uuid_batch_count)["uuids"]
return self._uuids.pop()
finally:
self._lock.release()

def __getitem__(self, dbname):
return Database(self._db_uri(dbname), server=self)
Expand Down

0 comments on commit b43bcf3

Please sign in to comment.