使用Python的threadpool模块例子

  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'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值