python 多线程 生产者消费者
import threading
import time
import logging
import random
import Queue
logging.basicConfig(level=logging.DEBUG,
format='(%(threadName)-9s) %(message)s',)
BUF_SIZE = 10
queue = Queue.Queue(BUF_SIZE)
class ProducerThread(threading.Thread):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None, verbose=None, queue):
super(ProducerThread,self).__init__()
self.target = target
self.name = name
self.queue = queue
def run(self):
while True:
if not self.queue.full():
item = random.randint(1,10)
self.queue.put(item)
logging.debug('Putting ' + str(item)
+ ' : ' + str(self.queue.qsize()) + ' items in queue')
time.sleep(random.random())
return
class ConsumerThread(threading.Thread):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None, verbose=None, queue):
super(ConsumerThread,self).__init__()
self.target = target
self.name = name
self.queue = queue
return
def run(self):
while True:
if not self.queue.empty():
item = self.queue.get()
logging.debug('Getting ' + str(item)
+ ' : ' + str(self.queue.qsize()) + ' items in queue')
time.sleep(random.random())
return
if __name__ == '__main__':
p = ProducerThread(name='producer', queue)
c = ConsumerThread(name='consumer', queue)
p.start()
time.sleep(2)
c.start()
time.sleep(2)
python 多线程 生产者消费者的更多相关文章
- Python多线程-生产者消费者模型
用多线程和队列来实现生产者消费者模型 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import threading imp ...
- java+反射+多线程+生产者消费者模式+读取xml(SAX)入数据库mysql-【费元星Q9715234】
java+反射+多线程+生产者消费者模式+读取xml(SAX)入数据库mysql-[费元星Q9715234] 说明如下,不懂的问题直接我[费元星Q9715234] 1.反射的意义在于不将xml tag ...
- 使用Python实现生产者消费者问题
之前用C++写过一篇生产者消费者的实现. 生产者和消费者主要是处理互斥和同步的问题: 队列作为缓冲区,需要互斥操作 队列中没有产品,消费者需要等待,直到生产者放入产品并通知它.队列慢的情况类似. 这里 ...
- Java实现多线程生产者消费者模式的两种方法
生产者消费者模式:生产者和消费者在同一时间段内共用同一存储空间,生产者向空间里生产数据,而消费者取走数据.生产者生产一个,消费者消费一个,不断循环. 第一种实现方法,用BlockingQueue阻塞队 ...
- 多线程-生产者消费者(synchronized同步)
正解博客:https://blog.csdn.net/u011863767/article/details/59731447 永远在循环(loop)里调用 wait 和 notify,不是在 If 语 ...
- java多线程 生产者消费者模式
package de.bvb; /** * 生产者消费者模式 * 通过 wait() 和 notify() 通信方法实现 * */ public class Test1 { public static ...
- [多线程] 生产者消费者模型的BOOST实现
说明 如果 使用过程中有BUG 一定要告诉我:在下面留言或者给我邮件(sawpara at 126 dot com) 使用boost::thread库来实现生产者消费者模型中的缓冲区! 仓库内最多可以 ...
- Java实现多线程生产者消费者模型及优化方案
生产者-消费者模型是进程间通信的重要内容之一.其原理十分简单,但自己用语言实现往往会出现很多的问题,下面我们用一系列代码来展现在编码中容易出现的问题以及最优解决方案. /* 单生产者.单消费者生产烤鸭 ...
- python进程——生产者消费者
生产者消费者模型介绍 为什么要使用生产者消费者模型 生产者指的是生产数据的任务,消费者指的是处理数据的任务,在并发编程中,如果生产者处理速度很快,而消费者处理速度很慢,那么生产者就必须等待消费者处理完 ...
随机推荐
- hdu1050
#include <cstdio> #include <algorithm> using namespace std; #define SIZE 205 struct Data ...
- How to install Samba server on Ubuntu 12.04
Part 1: Configuring anonymous share with samba server To install the samba package,enter the followi ...
- Ubuntu 安装 samba 实现文件共享和source insight 阅读uboot
环境:win10 + 虚拟机Ubuntu 12.04 一. samba的安装: # sudo apt-get install samba # sudo apt-get install smbfs 二. ...
- java第一天--Java开发环境的搭建以及使用eclipse从头一步步创建java项目
一.java 开发环境的搭建 这里主要说的是在windows 环境下怎么配置环境. 1.首先安装JDK java的sdk简称JDK ,去其官方网站下载最近的JDK即可..http://www.orac ...
- HTML 代码格式
<code> 元素不保留多余的空格和折行: 如需解决该问题,您必须在 <pre> 元素中包围代码:
- 2017-10-15 NOIP模拟赛
Stack #include<iostream> #include<cstdio> #define mod 7 using namespace std; ][],n; int ...
- HTML常用标签与CSS基础知识
一.HTML页面结构 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...
- 简单的vue.js的表单提交数据至flask然后数据库入库,再将表里面的数据展示在网页
一.先在数据库中创建表格 在mysql中建users库并建立一个含有ID,username,email三个字段的user表 二.去vue的组件里面写页面的表单代码,注意form标签里的action需要 ...
- 牛客练习赛34-C-little w and Segment Coverage(差分数组)
链接:https://ac.nowcoder.com/acm/contest/297/C 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...
- Codeforces Round #363 (Div. 2) A
Description There will be a launch of a new, powerful and unusual collider very soon, which located ...