forked from siddontang/imagecrawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskpool.py
40 lines (31 loc) · 1.05 KB
/
taskpool.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
#coding:utf-8
#siddontang@gmail.com
import traceback
from tornado.httpclient import AsyncHTTPClient
from tornado import ioloop
from tornado.curl_httpclient import CurlAsyncHTTPClient
AsyncHTTPClient.configure(CurlAsyncHTTPClient)
class TaskPool:
def __init__(self, maxClients):
self._ioloop = ioloop.IOLoop()
self._httpClient = AsyncHTTPClient(self._ioloop, maxClients)
self._taskNum = 0
def run(self):
self._check()
self._ioloop.start()
def spawn(self, request, callback, **kwargs):
def wapped(response):
self._taskNum -= 1
try:
callback(response)
except:
print 'spwan error:', traceback.format_exc()
pass
self._taskNum += 1
self._httpClient.fetch(request, wapped, **kwargs)
def _check(self):
def callback():
if self._taskNum == 0:
self._ioloop.stop()
return self._check()
self._ioloop.add_callback(callback)