python 多线程(线程池)

参考自定义线程池:https://www.cnblogs.com/zhang293/p/7954353.html

最佳线程数的获取:

1、通过用户慢慢递增来进行性能压测,观察QPS(即每秒的响应请求数,也即是最大吞吐能力。),响应时间

2、根据公式计算:服务器端最佳线程数量=((线程等待时间+线程cpu时间)/线程cpu时间) * cpu数量

3、单用户压测,查看CPU的消耗,然后直接乘以百分比,再进行压测,一般这个值的附近应该就是最佳线程数量。

方式 1 :(推荐)

ThreadPoolExecutor  返回为future(T)
'''
Created on 2019年10月11日

@author: sea
'''
# -*- coding: utf-8 -*-
from concurrent.futures import ThreadPoolExecutor
import time
from com.sea.email.MailUtils import send
'''
ThreadPoolExecutor中的submit()
<T> Future<T> submit(Callable<T> callable);
<T> Future<T> submit(Runnable var1, T result);
Future<?> submit(Runnable runnable);
'''
print(" 线程池 ThreadPoolExecutor 的使用")
def sayhello(a):
    print("hello  "+a)
#     time.sleep(1)
    return "结果 nihao : "+a



def test1():#submit()
    pool = ThreadPoolExecutor(3)
    submit1 = pool.submit(sayhello,("sea"))  #方法名 ,参数()
#     submit1 = pool.submit(sayhello,"sea")  #方法名 ,参数()

    submit2 = pool.submit(sayhello,("sea2"))
    submit3 = pool.submit(sayhello,("sea3"))
    submit4 = pool.submit(sayhello,("sea4"))
    
    print(submit4.result())#打印应返回值  阻塞-直到返回结果
    print(submit1.result())
    print(submit2.result())
    print(submit3.result())
    print("over")

def test2():#map() 
    seed=["a","b","c"]
    pool = ThreadPoolExecutor(3)
    rsultList = pool.map(sayhello,seed)#返回值为list 结果集
    for result in rsultList:
        print(result)#打印应返回值
    print("over")

#     with ThreadPoolExecutor(3) as executor1:
#         executor1.map(sayhello,seed)
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库
import vthread

@vthread.pool(6)
def some(a,b,c):
    import time;time.sleep(1)
    print(a+b+c)

for i in range(10):
    some(i,i,i)

分组线程池
import vthread

@vthread.pool(6)
def some1(a,b,c):
    import time;time.sleep(1)
    print("some1",a+b+c)

@vthread.pool(3,1)
def some2(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 -*-

import threadpool
import time

def sayhello (a):
    print("hello: "+a)
    time.sleep(2)

def main():
    global result
    seed=["a","b","c"]
    start=time.time()
    task_pool=threadpool.ThreadPool(5)
    requests=threadpool.makeRequests(sayhello,seed)
    for req in requests:
        task_pool.putRequest(req)
    task_pool.wait()
    end=time.time()
    time_m = end-start
    print("time: "+str(time_m))
    start1=time.time()
    for each in seed:
        sayhello(each)
    end1=time.time()
    print("time1: "+str(end1-start1))

if __name__ == '__main__':
    main()

猜你喜欢

转载自www.cnblogs.com/lshan/p/11654495.html