并发 & 并行

并发:是指系统具有处理多个任务的能力

并行:是指系统具有 同时 处理多个任务的能力

并行 是  并发的一个子集

同步 & 异步

同步:当进程执行到一个I/O(等待外部数据的时候)----------等 : 同步

异步:                     ----------不等,直到接收到数据再回来执行

GIL:全局解释锁

  因为有GIL锁,所以同一时刻,只有一个线程被CPU执行

任务:IO密集型

   计算密集型

对于IO密集型:Python 的多线程是有意义的

       可以采用多进程+协程

对于计算密集型:Python 的多线程就不推荐了,不适用了。

线程锁(互斥锁Mutex)

一个进程下可以启动多个线程,多个线程共享父进程的内存空间,也就意味着每个线程可以访问同一份数据,此时,如果2个线程同时要修改同一份数据,会出现什么状况?

import time
import threading
 
def addNum():
    global num #在每个线程中都获取这个全局变量
    print('--get num:',num )
    time.sleep(1)
    lock.acquire() #修改数据前加锁
    num  -=1 #对此公共变量进行-1操作
    lock.release() #修改后释放
 
num = 100  #设定一个共享变量
thread_list = []
lock = threading.Lock() #生成全局锁
for in range(100):
    = threading.Thread(target=addNum)
    t.start()
    thread_list.append(t)
 
for in thread_list: #等待所有线程执行完毕
    t.join()
 
print('final num:', num )

RLock(递归锁)

说白了就是在一个大锁中还要再包含子锁

import threading,time

def run1():
print("grab the first part data")
lock.acquire()
global num
num +=
lock.release()
return num
def run2():
print("grab the second part data")
lock.acquire()
global num2
num2+=
lock.release()
return num2
def run3():
lock.acquire()
res = run1()
print('--------between run1 and run2-----')
res2 = run2()
lock.release()
print(res,res2) if __name__ == '__main__': num,num2 = ,
lock = threading.RLock()
for i in range():
t = threading.Thread(target=run3)
t.start() while threading.active_count() != :
print(threading.active_count())
else:
print('----all threads done---')
print(num,num2)

Semaphore(信号量)

互斥锁 同时只允许一个线程更改数据,而Semaphore是同时允许一定数量的线程更改数据 ,比如厕所有3个坑,那最多只允许3个人上厕所,后面的人只能等里面有人出来了才能再进去。

import threading,time

def run(n):
semaphore.acquire()
time.sleep()
print("run the thread: %s\n" %n)
semaphore.release() if __name__ == '__main__': num=
semaphore = threading.BoundedSemaphore() #最多允许5个线程同时运行
for i in range():
t = threading.Thread(target=run,args=(i,))
t.start() while threading.active_count() != :
pass #print threading.active_count()
else:
print('----all threads done---')
print(num)

Event

通过Event来实现两个或多个线程间的交互,下面是一个红绿灯的例子,即起动一个线程做交通指挥灯,生成几个线程做车辆,车辆行驶按红灯停,绿灯行的规则。

import threading,time
import random
def light():
if not event.isSet():
event.set() #wait就不阻塞 #绿灯状态
count =
while True:
if count < :
print('\033[42;1m--green light on---\033[0m')
elif count <:
print('\033[43;1m--yellow light on---\033[0m')
elif count <:
if event.isSet():
event.clear()
print('\033[41;1m--red light on---\033[0m')
else:
count =
event.set() #打开绿灯
time.sleep()
count +=
def car(n):
while :
time.sleep(random.randrange())
if event.isSet(): #绿灯
print("car [%s] is running.." % n)
else:
print("car [%s] is waiting for the red light.." %n)
if __name__ == '__main__':
event = threading.Event()
Light = threading.Thread(target=light)
Light.start()
for i in range():
t = threading.Thread(target=car,args=(i,))
t.start()

队列queue  (重点)

1,queue.Queue()  FIFO队列-先进先出

2,queue.LifoQueue() LIFO队列,先进后出

3,queue.PriorityQueue()  按照优先级,越低越先出

方法:

q.size 返回队列大小

q.empty() 如果队列为空返回true

q.full() 如果队列为满返回true

q.get () 里面可以设置block 的t,f

q.put()

q.join()实际是队列为空时再执行别的操作

生产者消费者模型

在并发编程中使用生产者和消费者模式能够解决绝大多数并发问题。该模式通过平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度。

为什么要使用生产者和消费者模式

在线程世界里,生产者就是生产数据的线程,消费者就是消费数据的线程。在多线程开发当中,如果生产者处理速度很快,而消费者处理速度很慢,那么生产者就必须等待消费者处理完,才能继续生产数据。同样的道理,如果消费者的处理能力大于生产者,那么消费者就必须等待生产者。为了解决这个问题于是引入了生产者和消费者模式。

什么是生产者消费者模式

生产者消费者模式是通过一个容器来解决生产者和消费者的强耦合问题。生产者和消费者彼此之间不直接通讯,而通过阻塞队列来进行通讯,所以生产者生产完数据之后不用等待消费者处理,直接扔给阻塞队列,消费者不找生产者要数据,而是直接从阻塞队列里取,阻塞队列就相当于一个缓冲区,平衡了生产者和消费者的处理能力。

import threading
import queue def producer():
for i in range():
q.put("骨头 %s" % i ) print("开始等待所有的骨头被取走...")
q.join()
print("所有的骨头被取完了...") def consumer(n): while q.qsize() >: print("%s 取到" %n , q.get())
q.task_done() #告知这个任务执行完了 q = queue.Queue() p = threading.Thread(target=producer,)
p.start() c1 = consumer("李闯")
import time,random
import queue,threading
q = queue.Queue()
def Producer(name):
count =
while count <:
time.sleep(random.randrange())
q.put(count)
print('Producer %s has produced %s baozi..' %(name, count))
count +=
def Consumer(name):
count =
while count <:
time.sleep(random.randrange())
if not q.empty():
data = q.get()
print(data)
print('\033[32;1mConsumer %s has eat %s baozi...\033[0m' %(name, data))
else:
print("-----no baozi anymore----")
count +=
p1 = threading.Thread(target=Producer, args=('A',))
c1 = threading.Thread(target=Consumer, args=('B',))
p1.start()
c1.start()

多进程multiprocessing

from multiprocessing import Process
import os def info(title):
print(title)
print('module name:', __name__)
print('parent process:', os.getppid())
print('process id:', os.getpid())
print("\n\n") def f(name):
info('\033[31;1mfunction f\033[0m')
print('hello', name) if __name__ == '__main__':
info('\033[32;1mmain process line\033[0m')
p = Process(target=f, args=('bob',))
p.start()
p.join()

进程间通讯  

不同进程间内存是不共享的,要想实现两个进程间的数据交换,可以用以下方法:

Queues

使用方法跟threading里的queue差不多

from multiprocessing import Process, Queue

def f(q):
q.put([, None, 'hello']) if __name__ == '__main__':
q = Queue()
p = Process(target=f, args=(q,))
p.start()
print(q.get()) # prints "[42, None, 'hello']"
p.join()
from multiprocessing import Process, Pipe

def f(conn):
conn.send([, None, 'hello'])
conn.close() if __name__ == '__main__':
parent_conn, child_conn = Pipe()
p = Process(target=f, args=(child_conn,))
p.start()
print(parent_conn.recv()) # prints "[42, None, 'hello']"
p.join()

Managers

Manager返回的Manager对象()控制一个承载Python对象的服务器进程,并允许其他进程使用代理来操纵它们。

经理返回的经理()将支持类型 listdictNamespaceLockRLockSemaphoreBoundedSemaphoreConditionEventBarrierQueueValue and Array

from multiprocessing import Process, Manager

def f(d, l):
d[] = ''
d[''] =
d[0.25] = None
l.append()
print(l) if __name__ == '__main__':
with Manager() as manager:
d = manager.dict() l = manager.list(range())
p_list = []
for i in range():
p = Process(target=f, args=(d, l))
p.start()
p_list.append(p)
for res in p_list:
res.join() print(d)
print(l)

进程池  

进程池内部维护一个进程序列,当使用时,则去进程池中获取一个进程,如果进程池序列中没有可供使用的进进程,那么程序就会等待,直到进程池中有可用进程为止。

进程池中有两个方法:

  • apply
  • apply_async  
    from  multiprocessing import Process,Pool
    import time def Foo(i):
    time.sleep()
    return i+ def Bar(arg):
    print('-->exec done:',arg) pool = Pool() for i in range():
    pool.apply_async(func=Foo, args=(i,),callback=Bar)
    #pool.apply(func=Foo, args=(i,)) print('end')
    pool.close()
    pool.join()#进程池中进程执行完毕后再关闭,如果注释,那么程序直接关闭。

python-多线程等概念的更多相关文章

  1. Python多线程锁

    [Python之旅]第六篇(四):Python多线程锁   python lock 多线程 多线程使用方法 多线程锁 摘要:   在多线程程序执行过程中,为什么需要给一些线程加锁以及如何加锁,下面就来 ...

  2. Day9 - Python 多线程、进程

    Python之路,Day9, 进程.线程.协程篇   本节内容 操作系统发展史介绍 进程.与线程区别 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线 ...

  3. 搞定python多线程和多进程

    1 概念梳理: 1.1 线程 1.1.1 什么是线程 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发 ...

  4. 进程,线程,GIL,Python多线程,生产者消费者模型都是什么鬼

    1. 操作系统基本知识,进程,线程 CPU是计算机的核心,承担了所有的计算任务: 操作系统是计算机的管理者,它负责任务的调度.资源的分配和管理,统领整个计算机硬件:那么操作系统是如何进行任务调度的呢? ...

  5. python多线程、多进程以及GIL

    多线程 使用threading模块创建线程 传入一个函数 这种方式是最基本的,即调用threading中的Thread类的构造函数,然后指定参数target=func,再使用返回的Thread的实例调 ...

  6. 浅析Python多线程

    学习Python多线程的资料很多,吐槽Python多线程的博客也不少.本文主要介绍Python多线程实际应用,且假设读者已经了解多线程的基本概念.如果读者对进程线程概念不甚了解,可参见知名博主 阮一峰 ...

  7. day-3 python多线程编程知识点汇总

    python语言以容易入门,适合应用开发,编程简洁,第三方库多等等诸多优点,并吸引广大编程爱好者.但是也存在一个被熟知的性能瓶颈:python解释器引入GIL锁以后,多CPU场景下,也不再是并行方式运 ...

  8. Python多线程操作

    多线程是一门编程语言的重要操作. GIL(全局解释器锁)存在于python解释器中,用来确保当前只有一个线程被执行,当一个线程获得GIL后,这个线程将被执行,退出时释放GIL,由下一个获得GIL的线程 ...

  9. python多线程和多进程

    1 概念梳理: 1.1 线程 1.1.1 什么是线程 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发 ...

  10. Python 多线程和线程池

    一,前言 进程:是程序,资源集合,进程控制块组成,是最小的资源单位 特点:就对Python而言,可以实现真正的并行效果 缺点:进程切换很容易消耗cpu资源,进程之间的通信相对线程来说比较麻烦 线程:是 ...

随机推荐

  1. Atitit 列表表格按照字段排序数据解决方案

    Atitit 列表表格按照字段排序数据解决方案 1.1. 排序ui1 1.1.1. C:\Users\Administrator\Desktop\00oa\com.attilax\ui\orderAr ...

  2. apache kylin2.10在原生hadoop集群上安装

    Install Kylin Download latest Kylin binaries at http://kylin.apache.org/download Export KYLIN_HOME p ...

  3. 教你一招:解决Win 10安装软件时提示:文件系统错误 (-1073740940)

    1.win+R输入 gpedit.msc 2.左边计算机配置 windows设置——安全设置——本地策略——安全选项 3.在安全选项右边选择 用户账户控制:管理员批准模式中管理员的提升权限提示的行为, ...

  4. JavaScript高级用法一之事件响应与网页交互

    综述 本篇的主要内容来自慕课网,事件响应与网页交互,主要内容如下 1 什么是事件 2 鼠标单击事件( onclick ) 3 鼠标经过事件(onmouseover) 4 鼠标移开事件(onmouseo ...

  5. 详解Linux安装GCC

    为你详解Linux安装GCC方法 2009-12-11 14:05 佚名 博客园 字号:T | T 现在很多程序员都应用GCC,怎样才能更好的应用GCC.本文以在Redhat Linux安装GCC4. ...

  6. python3 zip压缩文件压缩多个不同文件夹内的文件方法

    #!/usr/bin/env python # -*- coding:utf-8 -*- import zipfile def addzip(): f = zipfile.ZipFile('test. ...

  7. 手动升级11.2.0.1的rac数据库到11.2.0.4

    ① 关闭两个节点上的数据库 crsctl stop resource ora.ORA11G.db ② 命令行单节点启动数据库, 注意这里的SQLPLUS 一定是升级后的软件地址 sqlplus / a ...

  8. Memcached 总结 启动多个Memcached服务 配置文件详解

    一. 1.解压下载的安装包到指定目录. 2.服务安装,使用管理员权限运行以下命令: c:\memcached\memcached.exe -d install 二.同一台Windows机器中启动多个M ...

  9. Spark学习笔记——读写MySQL

    1.使用Spark读取MySQL中某个表中的信息 build.sbt文件 name := "spark-hbase" version := "1.0" scal ...

  10. mysqldump导出数据时,某些表不导出,排除某些表,不导出某些表

    需求说明: 今天一同事问,在通过mysqldump导出数据库的时候,能不能把某些表不导出,或者叫做排除在外呢, 记得应该是可以实现,就搜索了下,通过mysqldump的--ignore-table参数 ...