Python3-22/456- 015Python多线程

一、什么是多线程?

        Python多线程适用于I/O密集型的任务中。对于I/O密集型任务来说,较少的时间在CPU计算上,较多的时间用在I/O上,比如文件读写,web请求,数据库请求等。而对于计算密集型任务,应该使用多进程。

        多线程类似于同时执行多个不同程序!每个独立的线程有一个程序运行的入口、顺序执行序列、程序的出口。但线程不能独立执行,必须依附在应用程序中,由应用程序提供多个线程执行控制。每个线程都有它自己的一组CPU寄存器,称为线程的上下文,该上下文反映了线程上次运行该线程的CPU寄存器的状态。

        上下文中两个最为重要的寄存器,指令指针和堆栈指针寄存器,线程总是在进程得到上下文中运行的,这些地址都用于标志拥有线程的进程地址空间中的内存。线程可以被抢占(中断),在其它线程正在运行时,线程可以暂时搁置(也称为睡眠),即线程的退让!

        线程可以分为两类:一类是内核线程,由操作系统内核创建和撤销;一类是用户线程,不需要内核支持而在用户程序中实现的线程!

二、Python多线程

        在Python3多线程中,有两个模块,其一是_thread,其二是threading(推荐使用)。thread模块已经被废弃,用户可以使用threading模块替代,在Python3中不再使用"thread"模块,为了兼容性,Python3将thread重命名为"_thread"

        在Python中,使用线程有两种方式:函数或者使用类来包装线程对象。函数式:调用_thread模块中的start_new_thread()函数来产生新线程,语法结构如下:

        _thread.start_new_thread(function, args[, kwargs]) ,function-线程函数,args-传递给线程函数的参数,它必须是个tuple类型。

import _thread
import time

def print_time(threadName, delay):
    count = 0
    while count <5:
        time.sleep(delay)
        count +=1
        print("%s: %s" % (threadName, time.ctime(time.time())))

try:
    _thread.start_new_thread(print_time, ("Thread-1", 2, ))
    _thread.start_new_thread(print_time, ("Thread-2", 4, ))

except:
    print('Error: 无法启动多线程!')

while 1:
    pass

 三、线程模块

        Python3中,通过两个标准库_thread和threading提供对线程的支持。其中_thread提供了低级别、原始的线程以及一个简单的锁,它相比于threading模块的功能还是比较有限的,threading模块除了包含_thread模块中的所有方法外,还提供了如下方法:

  • threading.currentThread():返回当前的线程变量
  • threading.enumerate():返回一个包含正在运行的线程的list,正在运行的线程指的是启动后,结束前,不包括启动前和结束后的线程。
  • threading.activeCount():返回一个正在运行线程的数量,等同len(threading.enumerate()

四、使用threading模块创建线程

        Python中的threading模块,我们可以直接通过threading.Thread继承创建一个新的子类,并实例化后调用start()方法启动新线程,即它调用了线程的run()方法。

        以下示例是通过一个多线程来打印时间

import threading
import time

exitFlag = 0
class myThread(threading.Thread):
    def __init__(self, threadID, name, delay):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.delay = delay
    def run(self):
        print("开始线程:" + self.name)
        print_time(self.name, self.delay, 10) #counter=10次!
        print("退出出线程:" + self.name)
def print_time(threadName, delay, counter):
    while counter:
        if exitFlag:
            threadName.exit()
        time.sleep(delay)
        print("%s: %s" % (threadName, time.ctime(time.time())))
        counter -= 1
#创建新线程
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 1)
thread3 = myThread(3, "Thread-3", 1)
thread1.start()
thread2.start()
thread3.start()
thread1.join()
thread2.join()
thread3.join()
print("退出主线程!")

五、线程同步(互斥锁)

        在使用多线程共同对某个数据进行修改,可能会有不可预料的结果,为保证数据的正确性,一致性,需要对多个线程进行同步。使用Thread对象的Lock和Rlock可实现简单的线程同步,他们都有acquire()和release()方法,对于那些需要每次只允许一个线程操作的数据,可以将操作放到acquire()和release()方法之间。

import threading
import time

class myThread(threading.Thread):
    def __init__(self, threadID, name, delay):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.delay = delay

    def run(self):
        print("开启线程:" + self.name)
        #获取锁!数据同步
        threadLock.acquire()
        print_time(self.name, self.delay, 3)
        #释放锁,开启下一个线程
        threadLock.release()

def print_time(threadName, delay, counter):
    while counter:
        time.sleep(delay)
        print("%s:%s" % (threadName, time.ctime(time.time())))
        counter -= 1
threadLock = threading.Lock()
threads = []

#创建新线程
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(1, "Thread-2", 1)

#开启新线程
thread1.start()
thread2.start()

#添加线程到线程列表
threads.append(thread1)
threads.append(thread2)

#等待所有线程完成
for t in threads:
    t.join()
print("退出主线程!")

七、线程优先级(Queue)

        Python中的Queue模块,提供了同步、线程安全的队列类,包括FIFO(先进先出),LIFO(),先进先出的队列queue,后进先出的LifoQueue,优先级队列PriorityQueue,这些队列都实现了锁原语,可以直接使用从而实现线程的同步,以下举例是有一个小冰箱,假如只能放5瓶饮料,A不停的放,B不停的取,A和B的放取速度不一致,就可以通过队列来实现。

        通常在并发编程中,使用生产者和消费者模式可以解决大多数的并发问题,如果生产者处理的速度很快,而消费者处理速度很慢,那么生产者就必须等待消费者处理完,才能继续生产数据,同理,如消费者的处理能力大于生产者,那么消费者就必须等待生产者。

        生产者和消费者模式就是通过一个容器(队列)来解决强耦合问题,生产者和消费者之间不通信,阻塞队列就相当于一个缓冲区,平衡了生产者和消费者的处理能力。

import threading, time
import queue

q = queue.Queue(maxsize=5)

def ProducerA():
    count = 1
    while True:
        q.put(f"冷饮 {count}")
        print(f"{time.strftime('%H:%M:%S')} A 放入:[冷饮 {count}]")
        count += 1
        time.sleep(1)
        print(f"放入后队列长度:{q.qsize()}")

def ConsumerB():
    while True:
        print(f"{time.strftime('%H:%M:%S')} B 取出 [{q.get()}]")
        print('--', q.qsize())
        print(f'取出后队列长度:{q.qsize()}')
        time.sleep(5)

p = threading.Thread(target=ProducerA)
c = threading.Thread(target=ConsumerB)
c.start()
p.start()

八、参考

python3多线程实战(python3经典编程案例)_cui_yonghua的博客-CSDN博客_python多线程实例

 Python3 多线程 | 菜鸟教程

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值