-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpClientsArray.py
45 lines (29 loc) · 1.09 KB
/
HttpClientsArray.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
__author__ = 'gluebag'
import tornado.httpclient
import tornado.curl_httpclient
import tornado.ioloop
import tornado.options
import tornado.log
import tornado.concurrent
import threading
class HttpClientsArray(object):
def __init__(self, number_of_clients, number_of_concurrent_per):
tornado.httpclient.AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")
self.clients = []
self.client_index = 0
self.client_lock = threading.Lock()
for i in range(number_of_clients):
self.clients.append(tornado.curl_httpclient.CurlAsyncHTTPClient(tornado.ioloop.IOLoop.instance(), force_instance=True, max_clients=number_of_concurrent_per))
def get_next_client(self):
ret = None
self.client_lock.acquire()
try:
if self.client_index >= len(self.clients):
self.client_index = 0
ret = self.clients[self.client_index]
self.client_index += 1
except Exception as e:
raise e
finally:
self.client_lock.release()
return ret