博客
关于我
python----线程、进程、协程的区别及多线程详解
阅读量:796 次
发布时间:2023-03-08

本文共 5869 字,大约阅读时间需要 19 分钟。

线程与进程的区别

进程与线程的基本概念

进程是操作系统资源分配的独立单位,是应用程序的载体。一个进程可以包含多个线程,进程之间通过共享内存等资源进行通信。进程是资源分配的独立单位,相比之下,线程是资源分配的最小单位。每个线程都共享同一个进程的资源,包括CPU、内存等。

线程的创建与管理

在Python中,线程可以通过内置的threading模块创建和管理。线程类Thread提供了多种方法来操作线程,例如start()用于启动线程,join()用于等待线程完成等。

函数创建线程

import threadingimport timedef myThread(i):    start = time.time()    print(f'当前线程为:{threading.current_thread().name},线程ID:{threading.current_thread().ident},所在进程为:{os.getpid()}')    time.sleep(i)    print(f'{threading.current_thread().name}线程运行时间结束,耗时{s}...' .format(s=time.time() - start))def fun():    t_list = []    for i in range(1,4):        t = threading.Thread(target=myThread, name=f'线程{i}', args=(i,))        t.start()        t_list.append(t)    for t in t_list:        t.join()if __name__ == '__main__':    fun()

类创建线程

import threadingimport timeimport osclass MyThread(threading.Thread):    def __init__(self, name=None):        super().__init__()        self.name = name        self.start_time = None    def run(self):        self.start_time = time.time()        print(f'当前线程为:{threading.current_thread().name},线程ID:{threading.current_thread().ident},所在进程为:{os.getpid()}')        time.sleep(3)        print(f'{self.name}线程运行时间结束,耗时{s}...' .format(s=time.time() - self.start_time))def fun():    t_list = []    for i in range(1,4):        t = MyThread(name=f'线程{i}')        t.start()        t_list.append(t)    for t in t_list:        t.join()if __name__ == '__main__':    fun()

线程锁的实现

线程锁用于防止多个线程同时访问共享资源,避免数据竞态和不一致。Python提供了两种锁:threading.Lock和递归锁threading.RLock。

简单锁

import threadingx = 0lock = threading.Lock()def increment():    global x    with lock:        for _ in range(1000000):            x += 1        print(f'当前线程:{threading.current_thread().name},x={x}')threads = []for _ in range(3):    t = threading.Thread(target=increment)    threads.append(t)    t.start()for t in threads:    t.join()print(f'Final value of x: {x}')

递归锁

import threadingx = 0lock = threading.RLock()def increment():    global x    lock.acquire()    lock.acquire()    try:        for _ in range(1000000):            x += 1        print(f'当前线程:{threading.current_thread().name},x={x}')    finally:        lock.release()        lock.release()threads = []for _ in range(3):    t = threading.Thread(target=increment)    threads.append(t)    t.start()for t in threads:    t.join()print(f'Final value of x: {x}')

线程通信

Condition

Condition是一种更高级的锁,常用于实现线程间的条件等待。

import threadingimport timefrom queue import Queueclass Producer(threading.Thread):    def run(self):        global count        while True:            if con.acquire():                if count <= 1000:                    con.wait()                else:                    count += 100                    print(f'{self.name} produce 100, count={count}')                    con.notify()                con.release()                time.sleep(1)class Consumer(threading.Thread):    def run(self):        global count        while True:            if con.acquire():                if count >= 100:                    con.wait()                else:                    count -= 5                    print(f'{self.name} consume 5, count={count}')                    con.notify()                con.release()                time.sleep(1)count = 0con = threading.Condition()def test():    for _ in range(2):        p = Producer()        p.start()    for _ in range(5):        c = Consumer()        c.start()if __name__ == '__main__':    test()

Semaphore

Semaphore用于控制并发访问的数量。

import threadingimport timesemaphore = threading.Semaphore(2)def foo():    semaphore.acquire()    time.sleep(2)    print('当前时间:', time.ctime())    semaphore.release()if __name__ == '__main__':    threads = []    for _ in range(6):        t = threading.Thread(target=foo)        t.start()        threads.append(t)    for t in threads:        t.join()    print('程序结束!')

Event

Event用于线程间的事件通知。

from threading import Eventimport timeevent = Event()def traffic_light(e):    while True:        if e.is_set():            time.sleep(2)            e.clear()            print('红灯亮')        else:            time.sleep(2)            e.set()            print('绿灯亮')def people(e, i):    if not e.is_set():        print(f' people {i} 在等待')        e.wait()    print(f' people {i} 通过了')if __name__ == '__main__':    e = Event()    p = threading.Thread(target=traffic_light, args=(e,))    p.daemon = True    p.start()    process_list = []    for i in range(1,7):        time.sleep(random.randrange(0,4,2))        p = threading.Thread(target=people, args=(e, i))        p.start()        process_list.append(p)    for p in process_list:        p.join()

Queue

Queue是Python内置的队列模块,支持多种类型的队列,如FIFO、LIFO和优先级队列。

FIFO队列

from queue import Queuequeue = Queue()for i in range(4):    queue.put(i)while not queue.empty():    print(queue.get())

LIFO队列

from queue import LifoQueuequeue = LifoQueue()for i in range(4):    queue.put(i)while not queue.empty():    print(queue.get())

优先级队列

from queue import PriorityQueuepq = PriorityQueue()pq.put((5, 12))pq.put((2, 11))pq.put((3, 15))while not pq.empty():    print(pq.get())

线程池

线程池通过预先创建线程来提高效率,适用于大量的短时间任务。

from concurrent.futures import ThreadPoolExecutordef test(value1, value2=None):    print(f'{threading.current_thread().name} threading is printed {value1}, {value2}')    time.sleep(2)    return 'finished'def test_result(future):    print(future.result())if __name__ == '__main__':    import numpy as np    threadPool = ThreadPoolExecutor(max_workers=4, thread_name_prefix="test_")    for i in range(0,10):        future = threadPool.submit(test, i, i+1)        future.add_done_callback(test_result)    threadPool.shutdown(wait=True)    print('main finished')

定时器

定时器用于在特定时间执行任务。

from threading import Timerimport timedef add(x, y):    print(x + y)t = Timer(2, add, args=(4,5,))t.start()time.sleep(2)t.cancel()

以上是对线程、进程、协程等多个方面的详细内容,涵盖了创建、管理、锁、通信、Queue和线程池等内容。这些内容能够帮助开发者更好地理解和使用多线程编程。

转载地址:http://cwlfk.baihongyu.com/

你可能感兴趣的文章