Skip to content

Commit 9455e76

Browse files
author
Kenneth Chan
committed
SDKPYTHON-13 Add timeout parameter in Client
1 parent 55f1275 commit 9455e76

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
# The short X.Y version.
5252
version = '0.6'
5353
# The full version, including alpha/beta/rc tags.
54-
release = '0.6.2'
54+
release = '0.6.3'
5555

5656
# The language for content autogenerated by Sphinx. Refer to documentation
5757
# for a list of supported languages.

predictionio/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
__copyright__ = "Copyright 2013, TappingStone, Inc."
1010
__license__ = "Apache License, Version 2.0"
1111

12-
__version__ = "0.6.2"
12+
__version__ = "0.6.3"
1313

1414

1515
# import packages
@@ -121,11 +121,13 @@ class Client(object):
121121
The asynchronous request becomes blocking once this size has been
122122
reached, until the queued requests are handled.
123123
Default value is 0, which means infinite queue size.
124+
:param timeout: timeout for HTTP connection attempts and requests in seconds (optional).
125+
default value is 5.
124126
125127
"""
126128

127129
def __init__(self, appkey, threads=1, apiurl="http://localhost:8000",
128-
apiversion="", qsize=0):
130+
apiversion="", qsize=0, timeout=5):
129131
"""Constructor of Client object.
130132
131133
"""
@@ -134,6 +136,7 @@ def __init__(self, appkey, threads=1, apiurl="http://localhost:8000",
134136
self.apiurl = apiurl
135137
self.apiversion = apiversion
136138
self.qsize = qsize
139+
self.timeout = timeout
137140

138141
# check connection type
139142
https_pattern = r'^https://(.*)'
@@ -149,7 +152,8 @@ def __init__(self, appkey, threads=1, apiurl="http://localhost:8000",
149152

150153
self._uid = None # identified uid
151154
self._connection = Connection(host=self.host, threads=self.threads,
152-
qsize=self.qsize, https=self.https)
155+
qsize=self.qsize, https=self.https,
156+
timeout=self.timeout)
153157

154158
def close(self):
155159
"""Close this client and the connection.

predictionio/connection.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,11 @@ def set_request(self, request):
157157

158158
class PredictionIOHttpConnection(object):
159159

160-
def __init__(self, host, https=True):
160+
def __init__(self, host, https=True, timeout=5):
161161
if https: # https connection
162-
self._connection = httplib.HTTPSConnection(host)
162+
self._connection = httplib.HTTPSConnection(host, timeout=timeout)
163163
else:
164-
self._connection = httplib.HTTPConnection(host)
164+
self._connection = httplib.HTTPConnection(host, timeout=timeout)
165165

166166
def connect(self):
167167
self._connection.connect()
@@ -245,7 +245,7 @@ def request(self, method, url, body={}, headers={}):
245245
return response # AsyncResponse object
246246

247247

248-
def connection_worker(host, request_queue, https=True, loop=True):
248+
def connection_worker(host, request_queue, https=True, timeout=5, loop=True):
249249
"""worker function which establishes connection and wait for request jobs
250250
from the request_queue
251251
@@ -257,11 +257,12 @@ def connection_worker(host, request_queue, https=True, loop=True):
257257
DELETE
258258
KILL
259259
https: HTTPS (True) or HTTP (False)
260+
timeout: timeout for HTTP connection attempts and requests in seconds
260261
loop: This worker function stays in a loop waiting for request
261262
For testing purpose only. should always be set to True.
262263
"""
263264

264-
connect = PredictionIOHttpConnection(host, https)
265+
connect = PredictionIOHttpConnection(host, https, timeout)
265266

266267
# loop waiting for job form request queue
267268
killed = not loop
@@ -307,27 +308,29 @@ class Connection(object):
307308
spawn multiple connection_worker threads to handle jobs in the queue q
308309
"""
309310

310-
def __init__(self, host, threads=1, qsize=0, https=True):
311+
def __init__(self, host, threads=1, qsize=0, https=True, timeout=5):
311312
"""constructor
312313
313314
Args:
314315
host: host of the server.
315316
threads: type int, number of threads to be spawn
316317
qsize: size of the queue q
317318
https: indicate it is httpS (True) or http connection (False)
319+
timeout: timeout for HTTP connection attempts and requests in seconds
318320
"""
319321
self.host = host
320322
self.https = https
321323
self.q = Queue.Queue(qsize) # if qsize=0, means infinite
322324
self.threads = threads
325+
self.timeout = timeout
323326
# start thread based on threads number
324327
self.tid = {} # dictionary of thread object
325328

326329
for i in xrange(threads):
327330
tname = "PredictionIOThread-%s" % i # thread name
328331
self.tid[i] = threading.Thread(
329332
target=connection_worker, name=tname,
330-
args=(self.host, self.q, self.https))
333+
kwargs={'host':self.host, 'request_queue':self.q, 'https':self.https, 'timeout':self.timeout})
331334
self.tid[i].setDaemon(True)
332335
self.tid[i].start()
333336

0 commit comments

Comments
 (0)