12 并发编程-(线程)-线程queue&进程池与线程池
- queue 英 /kjuː/ 美 /kju/ 队列
1、class queue.Queue(maxsize=0) #队列:先进先出
- import queue
- q=queue.Queue()
- q.put('first')
- q.put('second')
- q.put('third')
- print(q.get())
- print(q.get())
- print(q.get())
- '''
- 结果(先进先出):
- first
- second
- third
- '''
2、class queue.LifoQueue(maxsize=0) #堆栈:last in fisrt out
- import queue
- q=queue.LifoQueue()
- q.put('first')
- q.put('second')
- q.put('third')
- print(q.get())
- print(q.get())
- print(q.get())
- '''
- 结果(后进先出):
- third
- second
- first
- '''
3、class queue.PriorityQueue(maxsize=0) #优先级队列:存储数据时可设置优先级的队列
- import queue
- q=queue.PriorityQueue()
- #put进入一个元组,元组的第一个元素是优先级(通常是数字,也可以是非数字之间的比较),数字越小优先级越高
- q.put((20,'a'))
- q.put((10,'b'))
- q.put((30,'c'))
- print(q.get())
- print(q.get())
- print(q.get())
- '''
- 结果(数字越小优先级越高,优先级高的优先出队):
- (10, 'b')
- (20, 'a')
- (30, 'c')
- '''
二、进程池与线程池
1、基本的概念:
在刚开始学多进程或多线程时,我们迫不及待地基于多进程或多线程实现并发的套接字通信,然而这种实现方式的致命缺陷是:
服务的开启的进程数或线程数都会随着并发的客户端数目地增多而增多,这会对服务端主机带来巨大的压力,甚至于不堪重负而瘫痪,
于是我们必须对服务端开启的进程数或线程数加以控制,让机器在一个自己可以承受的范围内运行,这就是进程池或线程池的用途,
例如进程池,就是用来存放进程的池子,本质还是基于多进程,只不过是对开启进程的数目加上了限制
- 官网:https://docs.python.org/dev/library/concurrent.futures.html
- concurrent.futures模块提供了高度封装的异步调用接口
- ThreadPoolExecutor:线程池,提供异步调用
- ProcessPoolExecutor: 进程池,提供异步调用
- Both implement the same interface, which is defined by the abstract Executor class.
- 1、submit(fn, *args, **kwargs)
- 异步提交任务
- 2、map(func, *iterables, timeout=None, chunksize=1)
- 取代for循环submit的操作 取代for + submit
- 3、shutdown(wait=True)
- 相当于进程池的pool.close()+pool.join()操作
- wait=True,等待池内所有任务执行完毕回收完资源后才继续
- wait=False,立即返回,并不会等待池内的任务执行完毕
- 但不管wait参数为何值,整个程序都会等到所有任务执行完毕
- submit和map必须在shutdown之前
- 4、result(timeout=None)
- 取得结果
- 5、add_done_callback(fn)
- 回调函数
2.1、没有shutdown(wait=True)
- import os,time,random
- def task(name):
- print(f"{name} {os.getpid()} run")
- time.sleep(random.randint(1,3))
- if __name__ == '__main__':
- pool = ProcessPoolExecutor(4) # 进程池的容量设定,
- for i in range(10):
- pool.submit(task,'alex %s'%i)
- #pool.shutdown(waite= True)
- print('主')
- 主
- alex 0 9412 run
- alex 1 9624 run
- alex 2 9904 run
- alex 3 1452 run
- alex 4 9904 run
- alex 5 9412 run
- alex 6 1452 run
- alex 7 9624 run
- alex 8 9624 run
- alex 9 9904 run
2.2、有shutdown(wait=True)
- from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
- # 为什么建池 :我们必须对服务器开启的进程数或者线程数加以控制,让机器在一个自己可以承受的范围内运行
- # 这就是进程池或线程池的作用
- import os,time,random
- def task(name):
- print(f"{name} pid:{os.getpid()} run")
- time.sleep(random.randint(1,3))
- if __name__ == '__main__':
- pool = ProcessPoolExecutor(4) # 进程池的容量设定,
- for i in range(10):
- pool.submit(task,'alex %s'%i)
- pool.shutdown(waite= True)#等待池内所有任务执行完毕回收完资源后才继续
- print('主')
- alex 0 pid:10228 run
- alex 1 pid:9584 run
- alex 2 pid:7768 run
- alex 3 pid:9464 run
- alex 4 pid:9584 run
- alex 5 pid:10228 run
- alex 6 pid:7768 run
- alex 7 pid:9464 run
- alex 8 pid:9584 run
- alex 9 pid:7768 run
- 主
2.3、线程池的用法
- from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
- # 为什么建池 :我们必须对服务器开启的进程数或者线程数加以控制,让机器在一个自己可以承受的范围内运行
- # 这就是进程池或线程池的作用
- import os,time,random
- from threading import currentThread
- def task(name):
- print(f"{name} 线程:{currentThread().getName()} pid:{os.getpid()} run")
- time.sleep(random.randint(1,3))
- if __name__ == '__main__':
- pool = ThreadPoolExecutor(4) # 4个线程池的容量设定,
- for i in range(10):
- pool.submit(task,'alex %s'%i)
- pool.shutdown(wait=True)#等待池内所有任务执行完毕回收完资源后才继续
- print('主')
- alex 0 线程:ThreadPoolExecutor-0_0 pid:11164 run
- alex 1 线程:ThreadPoolExecutor-0_1 pid:11164 run
- alex 2 线程:ThreadPoolExecutor-0_2 pid:11164 run
- alex 3 线程:ThreadPoolExecutor-0_3 pid:11164 run
- alex 4 线程:ThreadPoolExecutor-0_2 pid:11164 run
- alex 5 线程:ThreadPoolExecutor-0_3 pid:11164 run
- alex 6 线程:ThreadPoolExecutor-0_0 pid:11164 run
- alex 7 线程:ThreadPoolExecutor-0_0 pid:11164 run
- alex 8 线程:ThreadPoolExecutor-0_1 pid:11164 run
- alex 9 线程:ThreadPoolExecutor-0_3 pid:11164 run
- 主
2.4 回调函数 add_done_callback(fn)
可以为进程池或线程池内的每个进程或线程绑定一个函数,该函数在进程或线程的任务执行完毕后自动触发,并接收任务的返回值当作参数,该函数称为回调函数
12 并发编程-(线程)-线程queue&进程池与线程池的更多相关文章
- Pthread 并发编程(二)——自底向上深入理解线程
Pthread 并发编程(二)--自底向上深入理解线程 前言 在本篇文章当中主要给大家介绍线程最基本的组成元素,以及在 pthread 当中给我们提供的一些线程的基本机制,因为很多语言的线程机制就是建 ...
- 六星经典CSAPP-笔记(12)并发编程(上)
六星经典CSAPP-笔记(12)并发编程(上) 1.并发(Concurrency) 我们经常在不知不觉间就说到或使用并发,但从未深入思考并发.我们经常能"遇见"并发,因为并发不仅仅 ...
- python并发编程基础之守护进程、队列、锁
并发编程2 1.守护进程 什么是守护进程? 表示进程A守护进程B,当被守护进程B结束后,进程A也就结束. from multiprocessing import Process import time ...
- Python并发编程03 /僵孤进程,孤儿进程、进程互斥锁,进程队列、进程之间的通信
Python并发编程03 /僵孤进程,孤儿进程.进程互斥锁,进程队列.进程之间的通信 目录 Python并发编程03 /僵孤进程,孤儿进程.进程互斥锁,进程队列.进程之间的通信 1. 僵尸进程/孤儿进 ...
- Python 3 并发编程多进程之守护进程
Python 3 并发编程多进程之守护进程 主进程创建守护进程 其一:守护进程会在主进程代码执行结束后就终止 其二:守护进程内无法再开启子进程,否则抛出异常:AssertionError: daemo ...
- python并发编程02 /多进程、进程的创建、进程PID、join方法、进程对象属性、守护进程
python并发编程02 /多进程.进程的创建.进程PID.join方法.进程对象属性.守护进程 目录 python并发编程02 /多进程.进程的创建.进程PID.join方法.进程对象属性.守护进程 ...
- Java并发编程原理与实战三十七:线程池的原理与使用
一.简介 线程池在我们的高并发环境下,实际应用是非常多的!!适用频率非常高! 有过使用过Executors框架的朋友,可能不太知道底层的实现,这里就是讲Executors是由ThreadPoolExe ...
- java并发编程(十七)Executor框架和线程池
转载请注明出处:http://blog.csdn.net/ns_code/article/details/17465497 Executor框架简介 在Java 5之后,并发编程引入了一堆新的启动 ...
- day31_8.12并发编程二线程
一.进程间的通信 在进程中,可以通过Queue队列进行通信,队列有两个特点: 1.先进先出.(先来的数据先被取出) 2.管道式存取.(数据取出一次后就不会在有了) 在python中有以下方法来操作数据 ...
随机推荐
- 011PHP文件处理——文件处理 文件内容分页操作类
<?php /** * 文件内容分页操作类: */ //访问地址:http://basicphp.com/006file/011.php?&page=1 class StrPage { ...
- mysql中limit的用法详解[数据分页常用]
在我们使用查询语句的时候,经常要返回前几条或者中间某几行的数据,这个时候怎么办呢?不用担心,mysql已经为我们提供了这样一个功能. SELECT * FROM table LIMIT [offset ...
- (转)RESTful API 设计最佳实践
原文:http://www.oschina.net/translate/best-practices-for-a-pragmatic-restful-api 数据模型已经稳定,接下来你可能需要为web ...
- java静态代理和动态代理(一)
代理Proxy: Proxy代理模式是一种结构型设计模式,主要解决的问题是:在直接访问对象时带来的问题. 代理是一种常用的设计模式,其目的就是为其他对象提供一个代理以控制对某个对象的访问.代理类负责为 ...
- 仿智能社官网:原生JS实现简单又酷炫的3D立方体时钟
先放一下我做的效果:https://linrunzheng.github.io/3Dclock/3Dclock/new.html 至于3D立方体怎么做这里就不在阐述了,可以看一下我之前的博客. 这里默 ...
- Centos7 服务 service 设置命令 systemctl 用法 (替代service 和 chkconfig)
在Centos 中 systemctl 是设置系统服务的命令,即 service , 它融合之前service和chkconfig的功能于一体. 可以使用它永久性或只在当前会话中启用/禁用服务 ...
- JS 实战2(邮箱选人功能)
点击选人打开新的窗口,新窗口中选取之后点击确定将所选内容传给原窗口,并关闭当前窗口 <%@ Page Language="C#" AutoEventWireup=" ...
- python print的用法
第一种是格式化的 strHello = "the length of (%s) is %d" %('Hello World',len('Hello World')) import ...
- flow flow-typed 定义简单demo
flow-typed 安装 全局 npm install -g flow-typed 测试代码 一个简单全局函数 目录根目录 flow-typed userLibDef.js declare func ...
- json-patch 了解
What is JSON Patch? JSON Patch is a format for describing changes to a JSON document. It can be used ...