上一页 1 2 3 4 5 6 7 8 ··· 14 下一页
摘要: 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 狒狒桑 阅读(25) 评论(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 狒狒桑 阅读(24) 评论(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 狒狒桑 阅读(17) 评论(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 狒狒桑 阅读(23) 评论(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 狒狒桑 阅读(29) 评论(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 狒狒桑 阅读(273) 评论(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 狒狒桑 阅读(20) 评论(0) 推荐(0) 编辑
摘要: 加锁的作用: 1、多个线程同时修改同一个全局变量时,确保不被CPU切片阻断 2、加锁会加长执行时间 import threadingimport timemetalocA = threading.Lock()# metalocB = threading.Locknum = 0def func1(): 阅读全文
posted @ 2022-04-24 10:23 狒狒桑 阅读(220) 评论(0) 推荐(0) 编辑
摘要: import threadingimport timeimport requestsclass MyThreading(threading.Thread): def __init__(self, url): self.url = url # 给run方法传参,只能通过 self的属性 super() 阅读全文
posted @ 2022-04-23 22:24 狒狒桑 阅读(39) 评论(0) 推荐(0) 编辑
摘要: import threadingimport timefrom time import sleepdef func1(): for i in range(4): sleep(1) print(' func1 ', threading.current_thread())def func2(): for 阅读全文
posted @ 2022-04-23 22:04 狒狒桑 阅读(16) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 7 8 ··· 14 下一页