多线程

作者: 伤心的小码码 | 来源:发表于2022-12-24 21:45 被阅读0次

单纯的多线程操作,主要是 threading 模块的支持

1. 模块提供

  • Thread 类
  • Lock 类
  • RLock 类
  • Semaphore 类
  • Event 类
  • Condition 类
  • Barrier 类
  • Queue 类
import threading
# threading.Thread - 线程执行对象
# threading.Lock - 锁
# threading.RLock - 可重入锁
# threading.Semaphore - 信号量
# threading.Event - 事件
# threading.Condition - 条件
# threading.Barrier - 屏障
# threading.Queue - 队列

2. 使用

2.1. 线程使用

入门级使用,主要是就是知道怎么创建线程,以及线程之间锁的使用

2.1.1. Thread 类

用于创建一个线程实例,通过 start() 来启动线程,join() 来等待线程结束。

from threading import Thread

def t_runner():
  print("Hello Thread")

def main():
  t = Thread(target=t_runner)
  t.start()
  t.join() # 使用 join 的时候,会阻塞当前主线程,等待线程 t 执行完成

if __name__ == '__main__':
  main()

2.1.2. Lock 和 RLock

在多线程中,使用对于共享资源的更新操作,会让执行数据出现紊乱的情况,需要锁的机制来协调线程之间更新数据的操作顺序。
Lock 只能在一个线程中被 acquire 一次,但是可以在所有线程中 releaseRLock 可以在一个线程中 require 多次,但是只能在当前线程 release,并且要成双成对。

from threading import Thread, Lock, RLock
v = 1
v1 = 2

l = Lock()
rL = RLock()

def lock_runner():
  time.sleep(1)
  with l:
    v += 1
  print(v)

def rlock_runner():
  time.sleep(1)
  with rL:
    v += 1
    with rL:
        v1 += 2

def main():
  t1 = Thread(target=lock_runner)
  t2 = Thread(target=lock_runner)
  t1.start()
  t2.start()
  t1.join()
  t2.join()

if __name__ == '__main__':
  main()

2.2. 线程协作

线程协作主要就是由于线程的执行没有严格意义上的顺序,所以有时候为了控制线程之间先后顺序,或者是分支逻辑,需要一些工具来辅助使用,让线程的协作更丝滑。
在都是使用在线程执行的方法里面的。

2.2.1. Semaphore 与 Event

资源型的线程协作,主要是竞争有限资源

from threading import Thread, Semaphore, Event
s = Semaphore(0) # s.acquire(), s.release() # 有限资源的使用
e = Event() # e.wait(), e.set(), e.clear()

def main():
  pass

if __name__ == '__main__':
  main()

2.2.2. Condition 和 Barrier

condition & barrier.png

协作型线程,主要是多个线程之间等待一个信号,触发开始或者结束,相当于线程之间的一个 "伪同步"

from threading import Thread, Condition, Barrier
c = Condition() # c.acquire(), c.release(), c.wait(), c.notify()
b = Barrier(3) # b.wait()

def main():
  pass

if __name__ == '__main__':
  main()

2.2.3. Queue

queue.png

线程之间实现分工,通过 消费者和生产者的方式进行分工和处理

from threading import Thread, Queue

queue = Queue()

def consume():
  q = queue.get()
  while q:
    print(q)
    q = queue.get()

def produce():
  while True:
    time.sleep(1)
    q = random_v # 随机数
    queue.put(q)

def main():
  tc = Thread(target=consume)
  tp = Thread(target=produce)
  
  tc.start()
  tp.start()

  tc.join()
  tp.join()

if __name__ == '__main__':
  main()

相关文章

  • iOS多线程 NSOperation

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程 pthread、NSThread

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程: GCD

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程运用

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程基础

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • 多线程介绍

    一、进程与线程 进程介绍 线程介绍 线程的串行 二、多线程 多线程介绍 多线程原理 多线程的优缺点 多线程优点: ...

  • iOS进阶之多线程管理(GCD、RunLoop、pthread、

    深入理解RunLoopiOS多线程--彻底学会多线程之『GCD』iOS多线程--彻底学会多线程之『pthread、...

  • iOS多线程相关面试题

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

  • 多线程之--NSOperation

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

  • iOS多线程之--NSThread

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

网友评论

      本文标题:多线程

      本文链接:https://www.haomeiwen.com/subject/vmdwqdtx.html