摘要: import geventfrom gevent import monkeyimport time# monkey.patch_all() # 补丁包,协程有IO等待时,触发协程切换def work1(): for i in range(10): print(f' work1 {i}') # tim 阅读全文
posted @ 2022-04-24 17:08 狒狒桑 阅读(36) 评论(0) 推荐(0)
摘要: from greenlet import greenletdef work1(): for i in range(10): print(f' work1 {i}') g2.switch()def work2(): for i in range(10): print(f' work2 {i}') g1 阅读全文
posted @ 2022-04-24 16:48 狒狒桑 阅读(30) 评论(0) 推荐(0)
摘要: 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 阅读全文
posted @ 2022-04-24 16:36 狒狒桑 阅读(41) 评论(0) 推荐(0)
摘要: 进程与进程之间不共享全局变量 from multiprocessing import Process, Queueimport time,osdef func1(q:Queue): while not q.empty(): print(' func1 {0} {1}'.format(q.get(), 阅读全文
posted @ 2022-04-24 15:21 狒狒桑 阅读(30) 评论(0) 推荐(0)
摘要: from multiprocessing import Pool, Managerimport time,osdef func1(q): print(' func1 {0} {1}'.format(q.get(), os.getpid())) time.sleep(1)if __name__ == 阅读全文
posted @ 2022-04-24 15:21 狒狒桑 阅读(28) 评论(0) 推荐(0)
摘要: import queuefrom threading import Threadimport timeq = queue.Queue()class Productor(Thread): def run(self) -> None: count = 0 while True: if q.qsize() 阅读全文
posted @ 2022-04-24 13:03 狒狒桑 阅读(37) 评论(0) 推荐(0)
摘要: 队列: 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 阅读全文
posted @ 2022-04-24 12:32 狒狒桑 阅读(42) 评论(0) 推荐(0)
摘要: 线程效率: 1、CPU计算密集型:单线程 比多线程 快 2、网络IO密集型:多线程 比 单线程 快 import requestsimport threadingimport timenum = 0def func1(): for i in range(100): # global num # nu 阅读全文
posted @ 2022-04-24 10:42 狒狒桑 阅读(332) 评论(0) 推荐(0)
摘要: 多个线程互相等待锁,会导致死锁 import threadingimport timemetalocA = threading.Lock()metalocB = threading.Lock()num = 0def func1(): for i in range(1000000): global n 阅读全文
posted @ 2022-04-24 10:32 狒狒桑 阅读(33) 评论(0) 推荐(0)
摘要: 加锁的作用: 1、多个线程同时修改同一个全局变量时,确保不被CPU切片阻断 2、加锁会加长执行时间 import threadingimport timemetalocA = threading.Lock()# metalocB = threading.Locknum = 0def func1(): 阅读全文
posted @ 2022-04-24 10:23 狒狒桑 阅读(247) 评论(0) 推荐(0)