python2线程池_python多线程(线程池的⼏种使⽤)(2)最佳线程数的获取:
1、通过⽤户慢慢递增来进⾏性能压测,观察QPS(即每秒的响应请求数,也即是最⼤吞吐能⼒。),响应时间微波炉能烤蛋糕吗
2、根据公式计算:服务器端最佳线程数量=((线程等待时间+线程cpu时间)/线程cpu时间) * cpu数量
3、单⽤户压测,查看CPU的消耗,然后直接乘以百分⽐,再进⾏压测,⼀般这个值的附近应该就是最佳线程数量。
⽅式 1 :(推荐)
线程的监听:
pools = ThreadPoolExecutor(5)
def testKafkaConsumerMutil():
consumer= KafkaConsumer("Sea",bootstrap_rvers = ['192.168.18.129:9092'],
group_id='test0',
auto_offt_ret='earliest',
max_poll_records=100,
consumer_timeout_ms=30000,
ssion_timeout_ms=30000,
enable_auto_commit=Fal)for msg inconsumer:
offt=msg.offt
print("offt"+str(offt))
data=json.loads(msg.value)
pools.submit(printt,data)
#监听当前线程池的线程数量
print("qsize"+str(pools._work_queue.qsize()))if pools._work_queue.qsize()>=6:
pass
time.sleep(30)
def printt(value):
# print(value)
time.sleep(2)
pass
股利无关论
ThreadPoolExecutor 返回为future(T)
'''Created on 2019年10⽉11⽇
@author: a'''
#-*- coding: utf-8 -*-
from concurrent.futures importThreadPoolExecutorimporttimefrom ail.MailUtils
importnd'''ThreadPoolExecutor中的submit()
Future submit(Callable callable);
Future submit(Runnable var1, T result);
Future> submit(Runnable runnable);'''
print("线程池 ThreadPoolExecutor 的使⽤")defsayhello(a):print("hello"+a)#time.sleep(1)
return "结果 nihao :"+adef test1():#submit()
pool = ThreadPoolExecutor(3)
submit1= pool.submit(sayhello,("a")) #⽅法名 ,参数()如果有多个参数,直接写多个就好,eg : say(a,b)-----福水
>pool.submit(say,"aaa","bbb")#submit1 = pool.submit(sayhello,"a") #⽅法名 ,参数()
submit2= pool.submit(sayhello,("a2"))
submit3= pool.submit(sayhello,("a3"))
submit4= pool.submit(sayhello,("a4"))sult())#打印应返回值 阻塞-直到返回结果
sult())sult())sult())print("over")def test2():#map()
ed=["a","b","c"]
pool= ThreadPoolExecutor(3)
rsultList= pool.map(sayhello,ed)#返回值为list 结果集
for result inrsultList:print(result)#打印应返回值
print("over")#with ThreadPoolExecutor(3) as executor1:#executor1.map(sayhello,ed)
自我介绍一分钟if __name__ == '__main__':#test1()
test2()
concurrent.futures.ThreadPoolExecutor,在提交任务的时候,有两种⽅式,⼀种是submit()函数,另⼀种是map()函数,两者的主要区别在于:
2.1、map可以保证输出的顺序, submit输出的顺序是乱的大棕熊
2.2、如果你要提交的任务的函数是⼀样的,就可以简化成map。但是假如提交的任务函数是不⼀样的,或者执⾏的过程之可能出现异常(使⽤map执⾏过程中发现问题会直接抛出错误)就要⽤到submit()
性生活不和谐怎么办
2.3、submit和map的参数是不同的,submit每次都需要提交⼀个⽬标函数和对应的参数,map只需要提交⼀次⽬标函数,⽬标函数的参数放在⼀个迭代器(列表,字典)⾥就可以。
3.现在?
这⾥要考虑⼀个问题,以上两种线程池的实现都是封装好的,任务只能在线程池初始化的时候添加⼀次,那么,假设我现在有这样⼀个需求,需要在线程池运⾏时,再往⾥⾯添加新的任务(注意,是新任务,不是新线程),那么要怎么办?
⽅式2 :python3的vthread库
可以试试python3的vthread库importvthread
@vthread.pool(6)defsome(a,b,c):import time;time.sleep(1)print(a+b+c)for i in range(10):
some(i,i,i)
分组线程池importvthread
@vthread.pool(6)defsome1(a,b,c):import time;time.sleep(1)print("some1",a+b+c)
@vthread.pool(3,1)defsome2(a,b,c):import time;time.sleep(1)print("some2",a*b*c)for i in range(10): some1(i,i,i)
some2(i,i,i)
加锁或其他操作,help()⼀下基本就能看懂。
⽅式3 :(⽐较⽼)
使⽤threadpool模块 具体使⽤⽅式如下:
#! /usr/bin/env python#-*- coding: utf-8 -*-
importthreadpoolimporttimedefsayhello (a):print("hello:"+a)手抄报推广普通话
time.sleep(2)defmain():globalresult
ed=["a","b","c"]
start=time.time()
task_pool=threadpool.ThreadPool(5)
requests=threadpool.makeRequests(sayhello,ed)for req inrequests:
task_pool.putRequest(req)
task_pool.wait()
end=time.time()
time_m= end-startprint("time:"+str(time_m))
start1=time.time()for each ined:
sayhello(each)
end1=time.time()print("time1:"+str(end1-start1))if __name__ == '__main__':
main()
属羊和什么属相最配