翻阅Python的threading模块API文档,觉得既有Java的影子(如threading.Thread类及其start/run方法),又有pthread的影子(如threading.Lock/threading.Condition)。总得来说简单易掌握,呵呵,随手写个大学OS课程里面“生产者与消费者”的Test。
相关类及方法就不多解释了,直接参考threading的API文档吧。
#! /usr/bin/env python
# coding=utf-8
import threading
import time
class Container(object):
def __init__(self, size = 10):
self.size = size
self.container = []
self.condition = threading.Condition()
def isEmpty(self):
self.condition.acquire()
size = len(self.container)
self.condition.release()
return size == 0
def isFull(self):
self.condition.acquire()
size = len(self.container)
self.condition.release()
return size == self.size
def add(self, obj):
self.condition.acquire()
while len(self.container) >= self.size:
self.condition.wait()
self.container.append(obj)
self.condition.notifyAll()
self.condition.release()
def get(self):
self.condition.acquire()
while len(self.container) == 0:
self.condition.wait()
obj = self.container.pop(0)
self.condition.release()
return obj
class Producer(threading.Thread):
def __init__(self, container, size, group = None, target = None, name = None, args = (), kwargs = {}):
threading.Thread.__init__(self, group, target, name, args, kwargs)
self.container = container
self.size = size
self.count = 0
def produce(self):
cur = threading.currentThread()
# s = str(cur.ident) + " " + cur.getName() + " (" + str(self.count) + ')'
s = cur.getName() + " (" + str(self.count) + ')'
return s
def run(self):
while self.count < self.size:
if not self.container.isFull():
obj = self.produce()
self.container.add(obj)
self.count += 1
time.sleep(1)
class Customer(threading.Thread):
def __init__(self, container, size, group = None, target = None, name = None, args = (), kwargs = {}):
threading.Thread.__init__(self, group, target, name, args, kwargs)
self.container = container
self.size = size
self.count = 0
def consume(self, obj):
cur = threading.currentThread()
# s = str(cur.ident) + ' ' + cur.getName() + ' (' + str(self.count) + '): ' + obj
s = cur.getName() + ' (' + str(self.count) + '): ' + obj
print s
def run(self):
while self.count < self.size:
self.consume(self.container.get())
time.sleep(1)
self.count += 1
def main():
container = Container()
p1 = Producer(container, 30, name = 'Producer 1')
c1 = Customer(container, 10, name = 'Customer 1')
c2 = Customer(container, 10, name = 'Customer 2')
c3 = Customer(container, 10, name = 'Customer 3')
c1.start()
c2.start()
c3.start()
p1.start()
if __name__ == '__main__':
main()
分享到:
相关推荐
python库。 资源全名:click_threading-0.5.0-py2.py3-none-any.whl
python多线程-threading模块.pdf
本文结合threading模块源码及官方文档,对threading模块内容(类,方法,属性)进行了系统性的整理,通过阅读此文档便可对threading模块有一个系统性的认知
Intel Threading Building Blocks - Outfitting C++ for Multi-Core Processor Parallelism 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系...
threading in C# Part 1 Getting Started Overview and Concepts ............................................................................................................. 3 How Threading Works .......
python多线程-threading模块.docx
//消费者线程将调用ReadFromCell()读取cellContents的内容并且显示出来,生产者进程将调用WriteToCell()方法向cellContents写入数据。 public class Cell { int cellContents; // Cell对象里边的内容 bool ...
threading-更高级别的线程接口 源代码:Lib/threading.py 该模块在较低级别thread模块之上构建更高级别的线程接口。另请参见mutex和Queue模块。 该dummy_threading模块适用于threading因thread缺失而无法使用的情况...
资源分类:Python库 所属语言:Python 资源全名:continuous_threading-1.1.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
C++ Threading A Generic-Programming Approach kevlin@curbralan.com Kevlin Henney 2
主要介绍了举例详解Python中threading模块的几个常用方法,threading模块用来创建和操作线程,是Python学习当中的重要知识,需要的朋友可以参考下
threading模块API是面向对象的,其中最重要的是线程类Thread,此外还有很多线程相关函数,这些函数常用的有:返回当前处于活动状态的线程数。例如:#
Python 兵法编程 260_threading模块创建线程.mp4
在Python中我们主要是通过thread和 threading这两个模块来实现的,其中Python的threading模块是对thread做了一些包装的,可以更加方便的被使用,所以我们使用 threading模块实现多线程编程。这篇文章我们主要来看看...
Wrox - APress - C# Threading Handbook
tbb-Intel Threading Building Blocks 线程构建模块,是Intel公司开发的并行编程开发的工具,能很好的支持C++并行计算编程。
Threading-Concepts-In-C:此程序采用整数ArrayList来对单独的线程进行排序,然后使用另一个线程进行合并
采用python3完成的生产者消费者问题,涉及到多线程、多线程队列,有需要的小伙伴们可以看看哎~ 欢迎交流学习~