Day9 - Python 多线程、进程
Python之路,Day9, 进程、线程、协程篇
本节内容
- 操作系统发展史介绍
- 进程、与线程区别
- python GIL全局解释器锁
- 线程
- 语法
- join
- 线程锁之Lock\Rlock\信号量
- 将线程变为守护进程
- Event事件
- queue队列
- 生产者消费者模型
- Queue队列
- 开发一个线程池
-
语法
操作系统发展史
手工操作(无操作系统)
1946年第一台计算机诞生--20世纪50年代中期,还未出现操作系统,计算机工作采用手工操作方式。
手工操作
程序员将对应于程序和数据的已穿孔的纸带(或卡片)装入输入机,然后启动输入机把程序和数据输入计算机内存,接着通过控制台开关启动程序针对数据运行;计算完毕,打印机输出计算结果;用户取走结果并卸下纸带(或卡片)后,才让下一个用户上机。
1 #_*_coding:utf-8_*_ 2 __author__ = 'Alex Li' 3 import threading 4 import time 5 import random 6 7 def door(): 8 door_open_time_counter = 0 9 while True: 10 if door_swiping_event.is_set(): 11 print("\033[32;1mdoor opening....\033[0m") 12 door_open_time_counter +=1 13 14 else: 15 print("\033[31;1mdoor closed...., swipe to open.\033[0m") 16 door_open_time_counter = 0 #清空计时器 17 door_swiping_event.wait() 18 19 20 if door_open_time_counter > 3:#门开了已经3s了,该关了 21 door_swiping_event.clear() 22 23 time.sleep(0.5) 24 25 26 def staff(n): 27 28 print("staff [%s] is comming..." % n ) 29 while True: 30 if door_swiping_event.is_set(): 31 print("\033[34;1mdoor is opened, passing.....\033[0m") 32 break 33 else: 34 print("staff [%s] sees door got closed, swipping the card....." % n) 35 print(door_swiping_event.set()) 36 door_swiping_event.set() 37 print("after set ",door_swiping_event.set()) 38 time.sleep(0.5) 39 door_swiping_event = threading.Event() #设置事件 40 41 42 door_thread = threading.Thread(target=door) 43 door_thread.start() 44 45 46 47 for i in range(5): 48 p = threading.Thread(target=staff,args=(i,)) 49 time.sleep(random.randrange(3)) 50 p.start()
queue队列
queue is especially useful in threaded programming when information must be exchanged safely between multiple threads.
- class
queue.
Queue
(maxsize=0) #先入先出
- class
queue.
LifoQueue
(maxsize=0) #last in fisrt out - class
queue.
PriorityQueue
(maxsize=0) #存储数据时可设置优先级的队列
-
Constructor for a priority queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.
The lowest valued entries are retrieved first (the lowest valued entry is the one returned by
sorted(list(entries))[0]
). A typical pattern for entries is a tuple in the form:(priority_number, data)
.
- exception
queue.
Empty
-
Exception raised when non-blocking
get()
(orget_nowait()
) is called on aQueue
object which is empty.
- exception
queue.
Full
-
Exception raised when non-blocking
put()
(orput_nowait()
) is called on aQueue
object which is full.
Queue.
qsize
()
Queue.
empty
() #return True if empty
Queue.
full
() # return True if full
Queue.
put
(item, block=True, timeout=None)-
Put item into the queue. If optional args block is true and timeout is None (the default), block if necessary until a free slot is available. If timeout is a positive number, it blocks at most timeout seconds and raises the
Full
exception if no free slot was available within that time. Otherwise (block is false), put an item on the queue if a free slot is immediately available, else raise theFull
exception (timeout is ignored in that case).
Queue.
put_nowait
(item)-
Equivalent to
put(item, False)
.
Queue.
get
(block=True, timeout=None)-
Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the
Empty
exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise theEmpty
exception (timeout is ignored in that case).
Queue.
get_nowait
()-
Equivalent to
get(False)
.
Two methods are offered to support tracking whether enqueued tasks have been fully processed by daemon consumer threads.
Queue.
task_done
()-
Indicate that a formerly enqueued task is complete. Used by queue consumer threads. For each
get()
used to fetch a task, a subsequent call totask_done()
tells the queue that the processing on the task is complete.If a
join()
is currently blocking, it will resume when all items have been processed (meaning that atask_done()
call was received for every item that had beenput()
into the queue).Raises a
ValueError
if called more times than there were items placed in the queue.
Queue.
join
() block直到queue被消费完毕
生产者消费者模型
在并发编程中使用生产者和消费者模式能够解决绝大多数并发问题。该模式通过平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度。
为什么要使用生产者和消费者模式
在线程世界里,生产者就是生产数据的线程,消费者就是消费数据的线程。在多线程开发当中,如果生产者处理速度很快,而消费者处理速度很慢,那么生产者就必须等待消费者处理完,才能继续生产数据。同样的道理,如果消费者的处理能力大于生产者,那么消费者就必须等待生产者。为了解决这个问题于是引入了生产者和消费者模式。
什么是生产者消费者模式
生产者消费者模式是通过一个容器来解决生产者和消费者的强耦合问题。生产者和消费者彼此之间不直接通讯,而通过阻塞队列来进行通讯,所以生产者生产完数据之后不用等待消费者处理,直接扔给阻塞队列,消费者不找生产者要数据,而是直接从阻塞队列里取,阻塞队列就相当于一个缓冲区,平衡了生产者和消费者的处理能力。
下面来学习一个最基本的生产者消费者模型的例子
多进程multiprocessing
multiprocessing
is a package that supports spawning processes using an API similar to the threading
module. The multiprocessing
package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing
module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows.
To show the individual process IDs involved, here is an expanded example:
进程间通讯
不同进程间内存是不共享的,要想实现两个进程间的数据交换,可以用以下方法:
Queues
使用方法跟threading里的queue差不多
Pipes
The Pipe()
function returns a pair of connection objects connected by a pipe which by default is duplex (two-way). For example:
ManagersThe two connection objects returned by
Pipe()represent the two ends of the pipe. Each connection object hassend()andrecv()methods (among others). Note that data in a pipe may become corrupted if two processes (or threads) try to read from or write to the same end of the pipe at the same time. Of course there is no risk of corruption from processes using different ends of the pipe at the same time.
A manager object returned by
Manager()controls a server process which holds Python objects and allows other processes to manipulate them using proxies.
A manager returned by
Manager()will support types
list,
dict,
Namespace,
Lock,
RLock,
Semaphore,
BoundedSemaphore,
Condition,
Event,
Barrier,
Queue,
Valueand
Array. For example,
进程同步
Without using the lock output from the different processes is liable to get all mixed up.
进程池
进程池内部维护一个进程序列,当使用时,则去进程池中获取一个进程,如果进程池序列中没有可供使用的进进程,那么程序就会等待,直到进程池中有可用进程为止。
进程池中有两个方法:
- apply
- apply_async
作业需求:
题目:简单主机批量管理工具
需求:
- 主机分组
- 主机信息配置文件用configparser解析
- 可批量执行命令、发送文件,结果实时返回,执行格式如下 batch_run -h h1,h2,h3 -g web_clusters,db_servers -cmd "df -h"
- batch_scp -h h1,h2,h3 -g web_clusters,db_servers -action put -local test.py -remote /tmp/
分类: Python自动化开发之路 好文要顶 关注我 收藏该文 金角大王
关注 - 0
粉丝 - 639 +加关注 0 0 « 上一篇:Python Select 解析 posted @ 2016-03-01 13:18 金角大王 阅读(2834) 评论(0) 编辑 收藏
刷新评论刷新页面返回顶部
- Python多线程和多进程谁更快?
- 【Python】多线程 & 多进程(3)
- python量化分析系列之---python分别使用多线程和多进程获取所有股票实时数据
- python 多进程和多线程
- python 多线程和多进程的区别 mutiprocessing theading
- Python多进程并发与多线程并发编程实例总结
- 搞定python多线程和多进程
- Python的多线程和多进程
- 多进程与多线程区别、优缺点(Python)
- Python多线程和多进程
- Python tkinter多进程多线程前邮箱,再用pyinstaller编译成exe
- python高阶进程4--多线程
- 为什么在Python里推荐使用多进程而不是多线程?
- 学习python多线程和多进程的一点感想
- python 多线程和多进程的区别 mutiprocessing theading
- 搞定python多线程和多进程
- python 基于消息的多进程多线程程序
- python多线程和多进程
- python-day9-进程、线程、协程篇
- python的多线程与多进程介绍及多进程详解(调用multiprocessing组件)