网络编程基础--多线程---concurrent.futures 模块---事件Event---信号量Semaphore---定时器Timer---死锁现象 递归锁----线程队列queue
1 concurrent.futures 模块:
# from abc import abstractmethod,ABCMeta
#
# class A(metaclass=ABCMeta):
# def mai(self):
# pass
# @classmethod
# class B(A):
# def mai(self):
# pass # 抽象类----定义子类的一些接口标准 @abstractmethod =================== 进程池 与 线程池 =================== 引入池 的 概念是为了 控制个数 concurrent.futures =======>>> 异步调用 # 1 ====== shutdown(wait=True)====>> close()+join() shutdown(wait=False)=======>> close()
# 2 ====== submit ===>> apply_async()
# 3 ====== map=====
# 4 ======add_done_callback(fn)
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
import time,random,os ===============================计算多的 进程池================================================== # def work(n):
# print('%s is running'%os.getpid())
# time.sleep(random.randint(1,3))
# return n*n
# # 1
# if __name__ == '__main__':
# executor_p=ProcessPoolExecutor(4)
# futures=[]
# for i in range(10):
# future=executor_p.submit(work,i) # 得到异步提交的future对象结果
# futures.append(future)
# executor_p.shutdown(wait=True) # executor_p ==== shutdown
# print('main')
# for obj in futures:
# print(obj.result()) # 结果
#
# # 2 ===== with as
# if __name__ == '__main__':
# with ProcessPoolExecutor(4) as e:
# futures=[]
# for i in range(6):
# future=e.submit(work,i)
# futures.append(future )
# print('Main')
# for obj in futures:
# print(obj.result()) # ================================ 简写 =========================================== # from concurrent.futures import ThreadPoolExecutor
# from threading import current_thread,enumerate,active_count
#
# def work(n):
# print('%s is running'%current_thread())
# return n**2
#
# if __name__ == '__main__':
# with ThreadPoolExecutor() as executor:
# futures=[executor.submit(work,i) for i in range(40)]
#
# for i in futures:
# print(i.result()) ======================================= IO多的 线程池 ===========================
#
# def work(n):
# print('%s is running'%os.getpid())
# time.sleep(random.randint(1,3))
# return n*n
# # 1
# if __name__ == '__main__':
# executor_p=ThreadPoolExecutor(30)
# futures=[]
# for i in range(10):
# future=executor_p.submit(work,i) # 得到异步提交的future对象结果
# futures.append(future)
# executor_p.shutdown(wait=True) # executor_p ==== shutdown
# print('main')
# for obj in futures:
# print(obj.result()) # 结果
#
# # 2 ===== with as
# if __name__ == '__main__':
# with ThreadPoolExecutor(40) as e:
# futures=[]
# for i in range(6):
# future=e.submit(work,i)
# futures.append(future )
# print('Main')
# for obj in futures:
# print(obj.result()) =========================== map ========循环提交 多次结果====================================
# from concurrent.futures import ThreadPoolExecutor
# from threading import current_thread,enumerate,active_count
#
# def work(n):
# print('%s is running'%current_thread())
# return n**2
#
# if __name__ == '__main__':
# with ThreadPoolExecutor() as executor:
# futures=[executor.map(work,range(40))] # [1,2,3,4,5]
# executor.map(work,range(6)) 把后边的可迭代对象当做 参数传给work # ===============================future.add_done_callback(fn)========回调函数=============================================
#
# from concurrent.futures import ProcessPoolExecutor
#
# def work(n):
# return n**n
#
# def work2(m):
# m=m.result()
# print( m/2)
#
# if __name__ == '__main__':
# with ProcessPoolExecutor() as e:
# for i in range(6):
# e.submit(work,i).add_done_callback(work2) #======================future.exeption(4)========等待时间--等待超时异常=============================================
2.事件Event:
两个进程之间的协同工作
from threading import Event,current_thread,Thread
import time
e=Event() def check():
print('%s 正在检测'%current_thread().getName())
time.sleep(3)
e.set() # 确认设置
def conn():
count=1
while not e.is_set():
if count >3:
raise TimeoutError('连接超时')
print('%s 正在等待连接'%current_thread().getName())
e.wait(timeout=0.1) # 超时时间限制 尝试次数
count+=1
print('%s 正在连接'%current_thread().getName()) if __name__ == '__main__':
t1=Thread(target=check)
t2=Thread(target=conn)
t3=Thread(target=conn)
t4=Thread(target=conn) t1.start()
t2.start()
t3.start()
t4.start()
3.信号量Semaphore:
from threading import Semaphore 信号量 Semaphore ---本质是一把锁======控制同时运行的进程数量(后台可以开启更多的进程) 进程池 ----同时运行的进程数量()
4.-定时器Timer:
# from threading import Timer
#
# def hello():
# print('hello')
#
# t=Timer(4,hello) # Timer--是Thread的子类 4秒后启动
# t.start()
5.死锁现象 递归锁:
============================== 死锁现象 ================================= 递归锁 RLock---可以aquire多次 每aquire一次 计数 加一,只要计数不为一 就不能被其他线程抢到 互斥锁 ----只能aquire一次 from threading import Thread,Lock,current_thread
import time
mutexA=Lock()
mutexB=Lock() class Mythread(Thread):
def run(self):
self.f1()
self.f2() def f1(self):
with mutexA:
print('%s 抢到了A'%current_thread().getName())
with mutexB:
print('抢到了B'%current_thread().getName())
def f2(self):
with mutexB:
print('抢到了B'%current_thread().getName())
time.sleep(0.1)
with mutexA:
print('抢到了A'%current_thread().getName()) if __name__ == '__main__':
for i in range(6):
t = Mythread()
t.start()
6.线程队列queue:
import queue q=queue.Queue(4) q.put(2)
q.put(2)
q.put(2)
q.put(2) print(q.get())
print(q.get())
print(q.get())
print(q.get()) # 1 优先级队列
q=queue.PriorityQueue(3)
q.put((10,'tsd'))
q.put((4,'ard')) # 相同的优先级 比较 后面的数据
q.put((10,'asd')) # 数字越小 优先级越高 print(q.get())
print(q.get())
print(q.get()) # 2 先进后出 ---堆栈 队列
q=queue.LifoQueue(3)
网络编程基础--多线程---concurrent.futures 模块---事件Event---信号量Semaphore---定时器Timer---死锁现象 递归锁----线程队列queue的更多相关文章
- 线程使用方法 锁(lock,Rlock),信号了(Semaphore),事件(Event),条件(Ccndition),定时器(timer)
2线程的使用方法 (1)锁机制 递归锁 RLock() 可以有无止尽的锁,但是会有一把万能钥匙 互斥锁: Lock() ...
- python并发编程之线程(创建线程,锁(死锁现象,递归锁),GIL锁)
什么是线程 进程:资源分配单位 线程:cpu执行单位(实体),每一个py文件中就是一个进程,一个进程中至少有一个线程 线程的两种创建方式: 一 from threading import Thread ...
- Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures
参考博客: https://www.cnblogs.com/xiao987334176/p/9046028.html 线程简述 什么是线程?线程是cpu调度的最小单位进程是资源分配的最小单位 进程和线 ...
- python 全栈开发,Day42(Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures)
昨日内容回顾 线程什么是线程?线程是cpu调度的最小单位进程是资源分配的最小单位 进程和线程是什么关系? 线程是在进程中的 一个执行单位 多进程 本质上开启的这个进程里就有一个线程 多线程 单纯的在当 ...
- Python之线程 3 - 信号量、事件、线程队列与concurrent.futures模块
一 信号量 二 事件 三 条件Condition 四 定时器(了解) 五 线程队列 六 标准模块-concurrent.futures 基本方法 ThreadPoolExecutor的简单使用 Pro ...
- python全栈开发,Day42(Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures)
昨日内容回顾 线程 什么是线程? 线程是cpu调度的最小单位 进程是资源分配的最小单位 进程和线程是什么关系? 线程是在进程中的一个执行单位 多进程 本质上开启的这个进程里就有一个线程 多线程 单纯的 ...
- 线程池、进程池(concurrent.futures模块)和协程
一.线程池 1.concurrent.futures模块 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 Pro ...
- concurrent.futures模块
1.concurrent.futures模块介绍 2.ThreadPoolExecutor线程池使用 3.ProcessPoolExecutor进程池使用 4.其他方法使用 1.concurrent. ...
- 创建进程池与线程池concurrent.futures模块的使用
一.进程池. 当并发的任务数量远远大于计算机所能承受的范围,即无法一次性开启过多的任务数量就应该考虑去 限制进程数或线程数,从而保证服务器不会因超载而瘫痪.这时候就出现了进程池和线程池. 二.conc ...
随机推荐
- SourceTree的基本使用---基本介绍/本地开发
转载自https://www.cnblogs.com/tian-xie/p/6264104.html 1. SourceTree是什么 拥有可视化界面的项目版本控制软件,适用于git项目管理 wind ...
- ODS
一般在带有ODS的系统体系结构中,ODS都设计为如下几个作用: 1.在业务系统和数据仓库之间形成一个隔离层 一般的数据仓库应用系统都具有非常复杂的数据来源,这些数据存放在不同的地理位置.不同的数据库. ...
- Dubbo框架入门介绍
背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当网站流量很小时,只需一个 ...
- iOS开发过程中常见错误问题及解决方案
错误原因:ld: x duplicate symbol for architecture x86_64 clang: error: linker command failed with exit co ...
- Centos7 关闭Ipv6
- poj1753模拟
题目链接http://poj.org/problem?id=1573 题意:从第一行第k个出发按照已给的方向前进,问第几步走出去或第几步进入一个有多少步的循环. 就是按照题意模拟就好了. 代码写完了w ...
- 【P3572】little bird(单调队列+DP)
一眼看上去这个题就要DP,可是应该怎么DP呢,我们发现,数据范围最多支持O(NlogN),但是这种DP貌似不怎么有,所以应该是O(N)算法,自然想到单调队列优化DP. 然后我们先考虑如果不用单调队列应 ...
- 基于jetty镜像的ossfs镜像docker镜像构建
阿里云ossfs:https://help.aliyun.com/document_detail/32196.html?spm=5176.product31815.6.514.yVI0xM 以上是阿里 ...
- python中的import一个注意事项
import math def foo(): import math x = math.pi # 如果math在下面import会出错,因为import是个写的过程(添加到sys.modules中), ...
- psd文件中截取固定大小的图片
1.选择需要操作的图层 使用选框工具, 设置固定大小和固定大小的值,在图层上拉取选区 2.使用移动工具 使用垂直.水平居中 使选择的icon在选区块中间 3.再选择好块区域调整好位置后 使用截取工具 ...