摘要:
from queue import Queueq = Queue()def func1(): while not q.empty(): i = q.get() print(f' func1 {i}') yield idef func2(): while not q.empty(): i = q.ge 阅读全文
摘要:
进程与进程之间不共享全局变量 from multiprocessing import Process, Queueimport time,osdef func1(q:Queue): while not q.empty(): print(' func1 {0} {1}'.format(q.get(), 阅读全文
摘要:
from multiprocessing import Pool, Managerimport time,osdef func1(q): print(' func1 {0} {1}'.format(q.get(), os.getpid())) time.sleep(1)if __name__ == 阅读全文
摘要:
import queuefrom threading import Threadimport timeq = queue.Queue()class Productor(Thread): def run(self) -> None: count = 0 while True: if q.qsize() 阅读全文
摘要:
队列: import queue# 1. 先进先出q1 = queue.Queue(5)q1.put(11)q1.put(22, block=False) # 插入不等待q1.put_nowait(33) # 插入不等待print(q1.qsize()) # 队列长度q1.get()q1.get(b 阅读全文
摘要:
线程效率: 1、CPU计算密集型:单线程 比多线程 快 2、网络IO密集型:多线程 比 单线程 快 import requestsimport threadingimport timenum = 0def func1(): for i in range(100): # global num # nu 阅读全文
摘要:
多个线程互相等待锁,会导致死锁 import threadingimport timemetalocA = threading.Lock()metalocB = threading.Lock()num = 0def func1(): for i in range(1000000): global n 阅读全文
摘要:
加锁的作用: 1、多个线程同时修改同一个全局变量时,确保不被CPU切片阻断 2、加锁会加长执行时间 import threadingimport timemetalocA = threading.Lock()# metalocB = threading.Locknum = 0def func1(): 阅读全文