Skip to content

Commit 385e613

Browse files
authored
Merge pull request #70 from pinecone-io/ch9489
Adding timeout parameter
2 parents 0eda02b + e685c36 commit 385e613

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

pinecone/manage.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def _get_status(name: str):
5757
def create_index(
5858
name: str,
5959
dimension: int,
60-
wait: bool = True,
60+
timeout: int = None,
6161
index_type: str = "approximated",
6262
metric: str = "cosine",
6363
replicas: int = 1,
@@ -69,8 +69,6 @@ def create_index(
6969
:param name: the name of the index.
7070
:type name: str
7171
:param dimension: the dimension of vectors that would be inserted in the index
72-
:param wait: wait for the index to deploy. Defaults to ``True``
73-
:type wait: bool
7472
:param index_type: type of index, one of {"approximated", "exact"}, defaults to "approximated".
7573
The "approximated" index uses fast approximate search algorithms developed by Pinecone.
7674
The "exact" index uses accurate exact search algorithms.
@@ -89,6 +87,8 @@ def create_index(
8987
Use 1 shard per 1GB of vectors
9088
:type shards: int,optional
9189
:param index_config: Advanced configuration options for the index
90+
:type timeout: int, optional
91+
:param timeout: Timeout for wait until index gets ready. If None, wait indefinitely; if >=0, time out after this many seconds; if -1, return immediately and do not wait. Default: None
9292
"""
9393
api_instance = _get_api_instance()
9494

@@ -107,35 +107,50 @@ def is_ready():
107107
ready = status['ready']
108108
return ready
109109

110-
timeout = time.time() + 300
111-
if wait:
112-
while (not is_ready()) and (time.time() <= timeout):
110+
if timeout == -1:
111+
return
112+
if timeout is None:
113+
while not is_ready():
113114
time.sleep(5)
114-
if time.time() > timeout:
115-
raise (TimeoutError('Index created, but it did not get ready in time.'))
115+
else:
116+
while (not is_ready()) and timeout >= 0:
117+
time.sleep(5)
118+
timeout -= 5
119+
if timeout and timeout < 0:
120+
raise (TimeoutError(
121+
'Please call the describe_index API ({}) to confirm index status.'.format(
122+
'https://www.pinecone.io/docs/api/operation/describe_index/')))
116123

117124

118125
@sentry
119-
def delete_index(name: str, wait: bool = True):
126+
def delete_index(name: str, timeout: int = None):
120127
"""Deletes a Pinecone index.
121128
122129
:param name: the name of the index.
123130
:type name: str
124-
:param wait: wait for the index to deploy. Defaults to ``True``
125-
:type wait: bool
131+
:param timeout: Timeout for wait until index gets ready. If None, wait indefinitely; if >=0, time out after this many seconds; if -1, return immediately and do not wait. Default: None
132+
:type timeout: int, optional
126133
"""
127134
api_instance = _get_api_instance()
128135
api_instance.delete_index(name)
129136

130137
def get_remaining():
131138
return name in api_instance.list_indexes()
132139

133-
timeout = time.time() + 300
134-
if wait:
135-
while get_remaining() and (time.time() <= timeout):
140+
if timeout == -1:
141+
return
142+
143+
if timeout is None:
144+
while get_remaining():
145+
time.sleep(5)
146+
else:
147+
while get_remaining() and timeout >= 0:
136148
time.sleep(5)
137-
if time.time() > timeout:
138-
raise (TimeoutError('Index deletion timed out.'))
149+
timeout -= 5
150+
if timeout and timeout < 0:
151+
raise (TimeoutError(
152+
'Please call the list_indexes API ({}) to confirm if index is deleted'.format(
153+
'https://www.pinecone.io/docs/api/operation/list_indexes/')))
139154

140155

141156
@sentry

0 commit comments

Comments
 (0)