网站大量收购独家精品文档,联系QQ:2885784924

python课件-第13章 多线程与多进程编程.ppt

python课件-第13章 多线程与多进程编程.ppt

此“教育”领域文档为创作者个人分享资料,不作为权威性指导和指引,仅供参考
  1. 1、本文档共66页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
13.3.2 Condition对象 生产者类 import threading class Producer(threading.Thread): def __init__(self, threadname): threading.Thread.__init__(self,name=threadname) def run(self): global x con.acquire() if x == 20: con.wait() else: print(\nProducer:, end= ) for i in range(20): print(x, end= ) x = x + 1 print(x) con.notify() con.release() 13.3.2 Condition对象 消费者类 class Consumer(threading.Thread): def __init__(self, threadname): threading.Thread.__init__(self, name =threadname) def run(self): global x con.acquire() if x == 0: con.wait() else: print(\nConsumer:, end= ) for i in range(20): print (x, end= ) x = x - 1 print(x) con.notify() con.release() 13.3.2 Condition对象 创建Condition对象以及生产者与消费者线程 con = threading.Condition() x = 0 p = Producer(Producer) c = Consumer(Consumer) p.start() c.start() p.join() c.join() print(\nAfter Producer and Consumer all done:,x) 运行结果 13.3.2 Condition对象 queue模块(在Python 2中为Queue模块)的Queue对象实现了多生产者/多消费者队列,尤其适合需要在多个线程之间进行信息交换的场合,实现了多线程编程所需要的所有锁语义。 13.3.3 Queue对象 13.3.3 Queue对象 使用Queue对象实现生产者-消费者线程同步 import threading import time from queue import Queue class Producer(threading.Thread): def __init__(self, threadname): threading.Thread.__init__(self, name = threadname) def run(self): global myqueue myqueue.put(self.getName()) print(self.getName(), put, self.getName(), to queue.) 13.3.3 Queue对象 class Consumer(threading.Thread): def __init__(self, threadname): threading.Thread.__init__(self, name = threadname) def run(self): global myqueue print(self.getName(), get, myqueue.get(), from queue.) 13.3.3 Queue对象 myqueue = Queue() plist = [] clist = [] for i i

文档评论(0)

k12教育文档 + 关注
实名认证
服务提供商

本人专注于k12教育,英语四级考试培训,本人是大学本科计算机专业毕业生,专注软件工程计算机专业,也可承接计算机专业的C语言程序设计,Java开发,Python程序开发。

1亿VIP精品文档

相关文档