Python开发【模块】:Concurrent
concurrent 模块
回顾:
对于python来说,作为解释型语言,Python的解释器必须做到既安全又高效。我们都知道多线程编程会遇到的问题,解释器要留意的是避免在不同的线程操作内部共享的数据,同时它还要保证在管理用户线程时保证总是有最大化的计算资源。而python是通过使用全局解释器锁来保护数据的安全性:
多线程执行方式:
- 设置GIL(global interpreter lock).
- 切换到一个线程执行。
- 运行:
- a,指定数量的字节码指令。
- b,线程主动让出控制(可以调用time.sleep(0))。
- 把线程设置为睡眠状态。
- 解锁GIL.
- 再次重复以上步骤。
threading使用回顾:
import threading
import time def run(n):
semaphore.acquire()
time.sleep(2)
print("run the thread: %s" % n)
semaphore.release() if __name__ == '__main__':
start_time = time.time()
thread_list = []
semaphore = threading.BoundedSemaphore(5) # 信号量,最多允许5个线程同时运行
for i in range(20):
t = threading.Thread(target=run, args=(i,))
t.start()
thread_list.append(t)
for t in thread_list:
t.join() used_time = time.time() - start_time
print('用时',used_time) # 用时 8.04102110862732
ThreadPoolExecutor多并发:
1、submit
import time
from concurrent import futures def run(n):
time.sleep(2)
print("run the thread: %s" % n) if __name__ == '__main__':
start = time.time()
with futures.ThreadPoolExecutor(5) as executor:
for i in range(20):
executor.submit(run,i) print(time.time()-start) # 8.006775379180908
2、map
import time
from concurrent import futures def run(n):
time.sleep(2)
print("run the thread: %s" % n) if __name__ == '__main__':
start = time.time()
with futures.ThreadPoolExecutor(5) as executor:
executor.map(run,range(20)) print(time.time()-start) # 8.006775379180908
executor.submit 和 futures.as_completed 这个组合比executor.map 更灵活,因为 submit 方法能处理不同的可调用对象和参数,而 executor.map 只能处理参数不同的同一个可调用对象。此外,传给 futures.as_completed 函数的期物集合可以来自多个 Executor 实例,例如一些由 ThreadPoolExecutor 实例创建,另一些由ProcessPoolExecutor创建
ProcessPoolExecutor多并发:
1、submit
import time
from concurrent import futures import time
from concurrent import futures def run(n):
time.sleep(2)
print("run the thread: %s" % n) if __name__ == '__main__':
start = time.time()
with futures.ProcessPoolExecutor(5) as executor:
for i in range(20):
executor.submit(run, i) print(time.time() - start) # 8.365714311599731
2、map
import time
from concurrent import futures import time
from concurrent import futures def run(n):
time.sleep(2)
print("run the thread: %s" % n) if __name__ == '__main__':
start = time.time()
with futures.ProcessPoolExecutor(5) as executor:
executor.map(run, range(20)) print(time.time() - start) # 8.317736864089966
接口压力测试的脚本
# #!/usr/bin/env python
# # -*- coding:utf-8 -*- import os
import time
import logging
import requests
import threading
from multiprocessing import Lock,Manager
from concurrent import futures download_url = 'http://192.168.188.105:8888'
workers = 250
cpu_count = 4 session = requests.Session() def handle(cost,mutex,contain):
with mutex:
min_cost = contain['min_cost']
max_cost = contain['max_cost']
hit_count = contain['hit_count']
average_cost = contain['average_cost']
if min_cost == 0:
contain['min_cost'] = cost
if min_cost > cost:
contain['min_cost'] = cost
if max_cost < cost:
contain['max_cost'] = cost
average_cost = (average_cost*hit_count + cost) / (hit_count + 1)
hit_count +=1
contain['average_cost'] = average_cost
contain['hit_count'] = hit_count
logging.info(contain) def download_one(mutex,contain):
while True:
try:
stime = time.time()
request = requests.Request(method='GET', url=download_url,)
prep = session.prepare_request(request)
response = session.send(prep, timeout=50)
etime = time.time()
print(response.status_code)
logging.info('process[%s] thread[%s] status[%s] cost[%s]',os.getpid(),threading.current_thread().ident,
response.status_code,etime-stime)
handle(float(etime-stime),mutex,contain)
# time.sleep(1)
except Exception as e:
logging.error(e)
print(e) def new_thread_pool(mutex,contain):
with futures.ThreadPoolExecutor(workers) as executor:
for i in range(workers):
executor.submit(download_one,mutex,contain) def subprocess():
manager = Manager()
mutex = manager.Lock()
contain = manager.dict({'average_cost': 0, 'min_cost': 0, 'max_cost': 0, 'hit_count': 0}) with futures.ProcessPoolExecutor(cpu_count) as executor:
for i in range(cpu_count):
executor.submit(new_thread_pool,mutex,contain) if __name__ == '__main__':
logging.basicConfig(filename="client.log", level=logging.INFO,
format="%(asctime)s [%(filename)s:%(lineno)d] %(message)s", datefmt="%m/%d/%Y %H:%M:%S [%A]")
subprocess()
Python开发【模块】:Concurrent的更多相关文章
- python 全栈开发,Day42(Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures)
昨日内容回顾 线程什么是线程?线程是cpu调度的最小单位进程是资源分配的最小单位 进程和线程是什么关系? 线程是在进程中的 一个执行单位 多进程 本质上开启的这个进程里就有一个线程 多线程 单纯的在当 ...
- python全栈开发,Day42(Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures)
昨日内容回顾 线程 什么是线程? 线程是cpu调度的最小单位 进程是资源分配的最小单位 进程和线程是什么关系? 线程是在进程中的一个执行单位 多进程 本质上开启的这个进程里就有一个线程 多线程 单纯的 ...
- Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures
参考博客: https://www.cnblogs.com/xiao987334176/p/9046028.html 线程简述 什么是线程?线程是cpu调度的最小单位进程是资源分配的最小单位 进程和线 ...
- Python标准模块--concurrent.futures(进程池,线程池)
python为我们提供的标准模块concurrent.futures里面有ThreadPoolExecutor(线程池)和ProcessPoolExecutor(进程池)两个模块. 在这个模块里他们俩 ...
- Python标准模块--concurrent.futures
1 模块简介 concurrent.futures模块是在Python3.2中添加的.根据Python的官方文档,concurrent.futures模块提供给开发者一个执行异步调用的高级接口.con ...
- Python标准模块--concurrent.futures 进程池线程池终极用法
concurrent.futures 这个模块是异步调用的机制concurrent.futures 提交任务都是用submitfor + submit 多个任务的提交shutdown 是等效于Pool ...
- python开发模块基础:re正则
一,re模块的用法 #findall #直接返回一个列表 #正常的正则表达式 #但是只会把分组里的显示出来#search #返回一个对象 .group()#match #返回一个对象 .group() ...
- python开发模块基础:异常处理&hashlib&logging&configparser
一,异常处理 # 异常处理代码 try: f = open('file', 'w') except ValueError: print('请输入一个数字') except Exception as e ...
- python开发模块基础:os&sys
一,os模块 os模块是与操作系统交互的一个接口 #!/usr/bin/env python #_*_coding:utf-8_*_ ''' os.walk() 显示目录下所有文件和子目录以元祖的形式 ...
- python开发模块基础:序列化模块json,pickle,shelve
一,为什么要序列化 # 将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化'''比如,我们在python代码中计算的一个数据需要给另外一段程序使用,那我们怎么给?现在我们能想到的方法就是存在文 ...
随机推荐
- 模拟Excel中SUBSTITUTE函数
Excel中的SUBSTITUTE是一个很有用的字符串替换函数,其说明如下: 说明 在文本字符串中用 new_text 替换 old_text. 如果需要在某一文本字符串中替换指定的文本,请使用函数 ...
- MVC+LINQToSQL的Repository模式之(一)数据工厂 DataContext绑定线程
namespace Data{ /// <summary> /// 数据库建立工厂 /// Created By : 张占岭 /// Created Date:20 ...
- 怎样用MathType输入带分数
MathType作为一种常用的数学公式编辑器.虽然其操作已经很简单了,但是对于刚刚接触MathType的新用户来说,一些最基本的MathType输入也是有一定难度的,一些人在MathType分数的编辑 ...
- ssh通过密钥进行连接
sshd服务提供两种安全验证的方法: 基于口令的安全验证:经过验证帐号与密码即可登陆到远程主机. 基于密钥的安全验证:需要在本地生成"密钥对"后将公钥传送至服务端,进行公共密钥的比 ...
- 安卓教程:提取APK程序里图片资源的方法
有些APK程序里的图标.图片很漂亮,在使用程序时你可能会想,如果能把这些漂亮的图标.图片提取出来就好了,其实这是可以办到的,请看教程. 本教程以“电影超人”的APK安装包为例,其它APK程序的提取方法 ...
- surfaceView和View的区别
概念:view在UI线程去更新自己:而SurfaceView则在一个子线程中去更新自己 surfaceView是在一个新起的单独线程中可以重新绘制画面,而View必须在UI的主线程中更新画面 在UI的 ...
- vue中的小踩坑(01)
前言: 昨天算是使用vue2.0+element-ui做了一点小小的页面,可是源于其中遇到的问题,特地整理一下,以防自己还有其他的小伙伴们继续踩坑. 过程: 1.不知道大家有没有注意到 ...
- django实现瀑布流、组合搜索、阶梯评论、验证码
django实现图片瀑布流布局 我们在一些图片网站上经常会看到,满屏都是图片,而且图片都大小不一,却可以按空间排列.默认一个div是占用一行,当想把div里的图片并排显示的时候,只能使用float属性 ...
- poj_3468 伸展树
题目大意 一个数列,每次操作可以是将某区间数字都加上一个相同的整数,也可以是询问一个区间中所有数字的和.(这里区间指的是数列中连续的若干个数)对每次询问给出结果. 思路 1. 伸展树的一般规律 对于区 ...
- nodeJS基础---->nodeJS的使用(一)
Node.js是一个Javascript运行环境(runtime).实际上它是对Google V8引擎进行了封装.V8引擎执行Javascript的速度非常快,性能非常好.Node.js对一些特殊用例 ...