"""线程间通讯1.共享全局 变量方法通讯-参看上面GIL,有数据安全风险,不推荐2.通过queue方式实现,本身是线程安全,get、put本身就是阻塞方法,"""from queue import Queueimport timeurl_queue = Queue(maxsize=100)class GetHtml(threading.Thread): def __init__(self,url_queue): threading.Thread.__init__(self) self.url_queue = url_queue def run(self): for i in range(10): self.url_queue.put(i) print("生产了包子编号:"+str(i)) time.sleep(2)class GetUrl(threading.Thread): def __init__(self,url_queue,name): threading.Thread.__init__(self) self.url_queue = url_queue self.name = name def run(self): while True: print(self.name+'吃了包子编号:'+str(self.url_queue.get())) time.sleep(3)sc = GetHtml(url_queue)sc.start()xf = GetUrl(url_queue,'大黄狗')xf.start()sc.join()xf.join()