下面是法度榜样的履行结不雅:
- thread TaskThread is running...
- thread TaskThread >>> 0
- thread TaskThread >>> 1
- thread TaskThread >>> 2
- thread TaskThread >>> 3
- thread TaskThread >>> 4
- thread TaskThread >>> 5
- thread TaskThread finished.
TaskThread类持续自threading模块中的Thread线程类。构造函数的name参数指定线程的名字,经由过程重载基类run函数实现具体义务。
在简单熟悉了Python的线程后,下面我们实现一个临盆者花费者模式。
- from Queue import Queue
- import random,threading,time
- #临盆者类
- class Producer(threading.Thread):
- def __init__(self, name,queue):
- threading.Thread.__init__(self, name=name)
- self.data=http://developer.51cto.com/art/201705/queue
- def run(self):
- for i in range(5):
- print("%s is producing %d to the queue!" % (self.getName(), i))
- self.data.put(i)
- time.sleep(random.randrange(10)/5)
- print("%s finished!" % self.getName())
- #花费者类
- class Consumer(threading.Thread):
- def __init__(self,name,queue):
- threading.Thread.__init__(self,name=name)
推荐阅读
VCHA(VCenter Server High Availability)应用一个包含三个节点——active、passive和witness——的集群来尽量避免体系故障可能带来的影响。如不雅治理员想要应用 vC>>>详细阅读
本文标题:用Python多线程实现生产者消费者模式
地址:http://www.17bianji.com/lsqh/35243.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示