1 #!/usr/bin/env python
2 import threadpool
3 import time
4 import random
5
6 def hello(self):
7 time.sleep(2);
8 return "hello";
9
10 def print_result(request, result):
11 print "the result is %s %r"%(request.requestID, result)
12
13 data = [ random.randint(1, 10) for i in range(20) ]
14
15 pool = threadpool.ThreadPool(5)
16 requests = threadpool.makeRequests(hello, data, print_result)
17 [pool.putRequest(req) for req in requests]
18 pool.wait()
19
输出结果:
the result is 140118496492576 'hello'
the result is 140118496492720 'hello'
the result is 140118496492864 'hello'
the result is 140118496493152 'hello'
the result is 140118496493008 'hello'
the result is 140118496493440 'hello'
the result is 140118496497744 'hello'
the result is 140118496493296 'hello'
the result is 140118496497888 'hello'
the result is 140118496498032 'hello'
the result is 140118496498176 'hello'
the result is 140118496498320 'hello'
the result is 140118496498464 'hello'
the result is 140118496498608 'hello'
the result is 140118496498752 'hello'
the result is 140118496498896 'hello'
the result is 140118496499040 'hello'
the result is 140118496499184 'hello'
the result is 140118496499472 'hello'
the result is 140118496499328 'hello'