Python 3(15)多线程

Python3 入门专栏

http://blog.csdn.net/column/details/19679.html



Python 3 多线程


python 3 提供了 threading 模块用于支持多线程,用于代替 python 2 中的 thread 模块(该模块在 python 3中已经废弃),常用的模块如下:
  • _thread:用于向下兼容 python 2 中的 thread 模块;
  • threading:多线程模块;
该模块下的的多线程机制类似于 Java,即:由执行程序所表示的单一进程中创建任务,竞争 cpu 时间同样也是抢占式的;



_thread 模块

python 的 _thread 模块用于创建简单线程;
可以使用  _thread.start_new_thread (function, args)函数创建新线程:
  • function:线程 run 方法函数;
  • args:向线程 run 方法传递的参数,为一个元组对象
 
import time
import _thread
# 线程 run 方法
def delay_print(thread_name, delay, print_content):
    count = delay
    while count >= 0:
        print("[%s] %d sec" % (thread_name, count))
        count -= 1
        time.sleep(1)
    print("[%s] %s" % (thread_name, print_content))
# 创建线程
_thread.start_new_thread(delay_print, ("Thread-1", 3, "Are you OK? Mi fans!"))
_thread.start_new_thread(delay_print, ("Thread-2", 5, "Derk dark ♂ fantastic"))
# 当前线程待命
while True:
    pass
输出以下:
[Thread-1] 3 sec
[Thread-2] 5 sec
[Thread-2] 4 sec
[Thread-1] 2 sec
[Thread-2] 3 sec
[Thread-1] 1 sec
[Thread-2] 2 sec
[Thread-1] 0 sec
[Thread-2] 1 sec
[Thread-1] Are you OK? Mi fans!
[Thread-2] 0 sec
[Thread-2] Derk dark ♂ fantastic





threading 模块


以上 _thread 模块只提供了低级别原始的线程,和一个简单的线程锁,而 threading 模块提供了更多的功能;

threading 除了包含 _thread 模块已有的所有方法外,还含有以下方法:
threading.currentThread()返回当前线程变量
threading.enumerate()返回一个包含正在运行线程的 list(即启动后、结束前的线程)
threading.activeCount()
返回正在运行的线程数量,同len(threading.enumerate())

由 threading 创建的Thread 类提供了以下线程方法:
Thread.isAive()检查线程是否活动
Thread.getName()获取线程名
Thread.setName()设置线程名
Thread.isDaemon()
检查线程是否为后台线程
Thread.setDaemon()
设置线程为后台线程
Thread.run()运行线程
Thread.start()启动线程
Thread.join([time])当前线程等待至调用线程 Thread 结束或终止,这个阻塞直到 Thread.join() 方法调用终止(正常退出,或抛出未处理的异常,或可选参数中的超时发生)

python threading 模块并没有提供线程暂停、线程中止的方法,实际上当 Thread 启动(调用 start() )后,线程处于失控状态,在 threading 模块提供的功能中无法控制线程的状态;
线程中断,可以通过手动发送一个 OS 中断信号来实现,详见下方;
线程暂停,可以通过实现线程锁来实现,详见下方:


创建线程

以下使用 threading 创建和以上 _thread 示例中一样功能的线程:
 
import time
import threading
# 线程实现类
class MyThread(threading.Thread):
    def __init__(self, thread_name, delay, print_content):
        threading.Thread.__init__(self)
        self.thread_name = thread_name
        self.delay = delay
        self.print_content = print_content
    def run(self):
        count = self.delay
        while count >= 0:
            print("[%s] %d sec" % (self.thread_name, count))
            count -= 1
            time.sleep(1)
        print("[%s] %s" % (self.thread_name, self.print_content))
# 创建线程
thread1 = MyThread("Thread-1", 3, "Are you OK? Mi fans!")
thread2 = MyThread("Thread-2", 3, "Derk dark ♂ fantastic")
# 运行线程
thread1.start()
thread2.start()
# 主线程阻塞直到2个线程执行结束
thread1.join()  
thread2.join()
print("[Thead-main] all thread completed")
输出:
[Thread-1] 3 sec
[Thread-2] 3 sec
[Thread-2] 2 sec
[Thread-1] 2 sec
[Thread-1] 1 sec
[Thread-2] 1 sec
[Thread-2] 0 sec
[Thread-1] 0 sec
[Thread-1] Are you OK? Mi fans!
[Thread-2] Derk dark ♂ fantastic
[Thead-main] all thread completed


线程中断的实现

theading.Thread 的线程中断,需要自己手动实现,可以类似如下:
 
import time
import threading
# 线程实现类,通过一个私有变量 running 来标志运行状态
class MyThread(threading.Thread):
    def __init__(self, delay):
        threading.Thread.__init__(self)
        self.delay = delay
        self.__running = True
    def interupt(self):
        self.__running = False
        print("thread is interrupt")
    def run(self):
        count = self.delay
        while count >= 0 and self.__running:
            print("%d sec" % count)
            count -= 1
            time.sleep(1)
        print("thread running completed")
# 创建线程
thread1 = MyThread(10)
# 运行线程
thread1.start()
# 3s 后中断线程 thread1
time.sleep(3)
thread1.interupt()
输出:
10 sec
9 sec
8 sec
thread is interrupt
thread running completed



线程同步

1)threading.Lock 和 threading.RLock 对象
  • threading 的 Lock 和 Rlock 对象用于对线程同步提供简单的支持,类似于 Java 的 Lock 对象,该对象有acquire() 、release() 方法分别用于加线程锁、释放线程锁;
  • Lock 和 RLock 的区别:RLock 允许在同一线程被多次 acquire,而 Lock 不允许这种情况,使用 RLock 的时候,acquire 和 release 必须成对出现,才能真正释放被占用的锁;
以下示例:
 
# 示例多个线程获取一个共享资源,每次获取 Resource 并减少 Resource.__count 值
import random
import time
import threading
# 线程实现类
class MyThread(threading.Thread):
    def __init__(self, thread_name, resource):
        threading.Thread.__init__(self)
        self.thread_name = thread_name
        self.__resource = resource
    def run(self):
        # 获取资源起始数量
        lock.acquire()
        count = self.__resource.get_count()
        lock.release()
        while count >= 0:
            # 获取资源,并修改 resource._count 值
            lock.acquire()
            count = self.__resource.get_count()
            count -= 1
            print("[%s] resource-count: %d" % (self.thread_name, count))
            self.__resource.set_count(count)
            lock.release()
            # 模拟处理资源时间
            time.sleep(random.random() * 3)
# 资源对象,储存线程共享资源
class Resource:
    def __init__(self, count):
        self.__count = count
    def get_count(self):
        return self.__count
    def set_count(self, count):
        self.__count = count
if __name__ == "__main__":
    resource = Resource(100)   # 创建资源(包含100个资源值)
    lock = threading.Lock()    # 创建线程锁对象
    threadpool = list()        # 使用一个list作为线程池
    # 向线程池中添加 5 个线程
    for i in range(1, 6):
        threadpool.append(MyThread("Thread-"+str(i), resource))
    # 运行线程池中的所有线程
    for thread in threadpool:
        thread.start()
    for thread in threadpool:
        thread.join()
输出:
[Thread-1] resource-count: 99
[Thread-2] resource-count: 98
[Thread-3] resource-count: 97
[Thread-4] resource-count: 96
[Thread-5] resource-count: 95
[Thread-1] resource-count: 94
[Thread-5] resource-count: 93
[Thread-4] resource-count: 92
[Thread-1] resource-count: 91
[Thread-5] resource-count: 90
......

2)threading.Condition 对象
threading.Condition 提供了比 Lock,RLock 更加高级的功能,允许控制更加复杂的线程同步问题,内部维护一个锁对象(默认为 RLock),可以在创建 Condition 对象时将锁对象作为参数传入;
同样的 Condition 提供了 acquire() 、release() 方法,常用方法如下:
Condition.acquire()
调用线程获取锁
Condition.release()调用线程释放锁
Condition.wait([timeout])调用线程释放锁,同时调用线程被挂起,直到被唤醒(或超时)时才重新获取锁,并继续执行下去;
Condition.notify()唤醒任意一个挂起线程(如果存在挂起的线程),但是不会释放锁占用的锁
Confition.notifyAll()同上,不过唤醒所有挂起线程,所有线程重新开始竞争CPU时间

以下使用 Codition 来实现一个经典的 生产者/消费者(producer / consumer) 模型:
 
import random
import time
import threading
# 生产者线程
class Producer(threading.Thread):
    def __init__(self, thread_name):
        threading.Thread.__init__(self)
        self.__thread_name = thread_name
    def run(self):
        while True:
            product = random.randrange(0, 100, 10)  # 模拟生产产品
            time.sleep(random.random() * 2)       # 模拟处理时间
            condition.acquire()
            resources.append(product)      # 向资源列表添加产品
            print("[%s] create product: %d , resource size: %d" % (self.__thread_name, product, len(resources)))
            condition.notify()             #唤醒一个挂起线程
            condition.release()
# 消费者线程
class Consumer(threading.Thread):
    def __init__(self, thread_name):
        threading.Thread.__init__(self)
        self.__thread_name = thread_name
    def run(self):
        while True:
            condition.acquire()
            if len(resources) <= 0:   # 当资源列表为空时,线程挂起
                condition.wait()
            product = resources.pop()    # 获取资源
            print("[%s] get product: %d , resource size: %d" % (self.__thread_name, product, len(resources)))
            condition.release()
            # 模拟处理资源时间
            time.sleep(random.random() * 8)
            
if __name__ == "__main__":
    resources = list()           # 资源列表对象
    condition = threading.Condition()
    threadpool = list()         # 线程池对象
    # 向线程池中添加 1 个生产者线程、3个消费者线程
    threadpool.append(Producer("Productor-1"))
    threadpool.append(Consumer("Consumer-1"))
    threadpool.append(Consumer("Consumer-2"))
    threadpool.append(Consumer("Consumer-3"))
    # 运行线程池中的所有线程
    for thread in threadpool:
        thread.start()
    for thread in threadpool:
        thread.join()
输出:
[Productor-1] create product: 50 , resource size: 1
[Consumer-1] get product: 50 , resource size: 0
[Productor-1] create product: 30 , resource size: 1
[Consumer-3] get product: 30 , resource size: 0
[Productor-1] create product: 50 , resource size: 1
[Productor-1] create product: 50 , resource size: 2
[Productor-1] create product: 80 , resource size: 3
[Consumer-1] get product: 80 , resource size: 2
[Productor-1] create product: 10 , resource size: 3
[Consumer-3] get product: 10 , resource size: 2
[Productor-1] create product: 40 , resource size: 3
.......




线程优先队列

python 内置的 queue 模块中提供了同步、线程安全的队列类,包括 FIFO 队列 Queue,LIFO 队列 LifoQueue,优先队列 PriorityQueue;
这些队列都是实现了锁原语,可以直接在多线程中使用,可以在队列中实现线程将的同步;

这些队列类的常用方法如下:
Queue.qsize()
返回队列的大小
Queue.empty()
如果队列为空,返回True
Queue.full()
如果队列满了,返回True,与 maxsize 大小对应
Queue.get([block[, timeout]])
获取队列,timeout等待时间,block 为是否阻塞,默认为 True
Queue.put(item [,block[, timeout]])
写入队列,timeout等待时间,block 为是否阻塞,默认为 True
Queue.task_done()
在完成一项工作之后,Queue.task_done()函数向任务已经完成的队列发送一个信号
Queue.join()
意味着等到队列为空,再执行别的操作

实际上 Queue 这种特性,很适合用来编写 生产者/消费者 模型;

以下用 Queue 改写以上的 生产者/消费者 示例:
 
# 示例多个线程获取一个共享资源,每次获取 Resource 并减少 Resource.__count 值
import queue
import random
import time
import threading
# 生产者线程
class Producer(threading.Thread):
    def __init__(self, thread_name):
        threading.Thread.__init__(self)
        self.__thread_name = thread_name
    def run(self):
        while True:
            product = random.randrange(0, 100, 10)  # 模拟生产产品
            time.sleep(random.random() * 2)       # 模拟处理时间
            resources.put(product)      # 向资源列表添加产品
            print("[%s] create product: %d , resource size: %d" % (self.__thread_name, product, resources.qsize()))
# 消费者线程
class Consumer(threading.Thread):
    def __init__(self, thread_name):
        threading.Thread.__init__(self)
        self.__thread_name = thread_name
    def run(self):
        while True:
            product = resources.get()    # 获取资源
            print("[%s] get product: %d , resource size: %d" % (self.__thread_name, product, resources.qsize()))
            # 模拟处理资源时间
            time.sleep(random.random() * 8)
            
if __name__ == "__main__":
    resources = queue.Queue(maxsize=15)    # 使用同步队列作为资源储存
    threadpool = list()
    # 向线程池中添加 1 个生产者线程、3个消费者线程
    threadpool.append(Producer("Productor-1"))
    threadpool.append(Consumer("Consumer-1"))
    threadpool.append(Consumer("Consumer-2"))
    threadpool.append(Consumer("Consumer-3"))
    # 运行线程池中的所有线程
    for thread in threadpool:
        thread.start()
    for thread in threadpool:
        thread.join()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值