Skip to content

Commit dc70912

Browse files
committed
PYTHON-1003 Avoid checking out multiple sockets per thread.
1 parent 52ebf27 commit dc70912

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

pymongo/collection.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,19 +1418,20 @@ def options(self):
14181418
cursor = self.__database._list_collections(sock_info,
14191419
slave_ok,
14201420
criteria)
1421-
result = None
1422-
for doc in cursor:
1423-
result = doc
1424-
break
14251421

1426-
if not result:
1427-
return {}
1422+
result = None
1423+
for doc in cursor:
1424+
result = doc
1425+
break
14281426

1429-
options = result.get("options", {})
1430-
if "create" in options:
1431-
del options["create"]
1427+
if not result:
1428+
return {}
14321429

1433-
return options
1430+
options = result.get("options", {})
1431+
if "create" in options:
1432+
del options["create"]
1433+
1434+
return options
14341435

14351436
def aggregate(self, pipeline, **kwargs):
14361437
"""Perform an aggregation using the aggregation framework on this

pymongo/database.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -487,15 +487,20 @@ def collection_names(self, include_system_collections=True):
487487
will not include system collections (e.g ``system.indexes``)
488488
"""
489489
with self.__client._socket_for_reads(
490-
ReadPreference.PRIMARY) as (sock_info, slave_okay):
490+
ReadPreference.PRIMARY) as (sock_info, slave_okay):
491491

492+
wire_version = sock_info.max_wire_version
492493
results = self._list_collections(sock_info, slave_okay)
493-
names = [result["name"] for result in results]
494-
if sock_info.max_wire_version <= 2:
495-
# MongoDB 2.4 and older return index namespaces and collection
496-
# namespaces prefixed with the database name.
497-
names = [n[len(self.__name) + 1:] for n in names
498-
if n.startswith(self.__name + ".") and "$" not in n]
494+
495+
# Iterating the cursor to completion may require a socket for getmore.
496+
# Ensure we do that outside the "with" block so we don't require more
497+
# than one socket at a time.
498+
names = [result["name"] for result in results]
499+
if wire_version <= 2:
500+
# MongoDB 2.4 and older return index namespaces and collection
501+
# namespaces prefixed with the database name.
502+
names = [n[len(self.__name) + 1:] for n in names
503+
if n.startswith(self.__name + ".") and "$" not in n]
499504

500505
if not include_system_collections:
501506
names = [name for name in names if not name.startswith("system.")]

0 commit comments

Comments
 (0)