python 多线程队列
##Using Queue with multiprocessing – Chapter : Process Based Parallelism
import multiprocessing
import random
import time class producer(multiprocessing.Process):
def __init__(self, queue):
multiprocessing.Process.__init__(self)
self.queue = queue def run(self) :
for i in range():
item = random.randint(, )
self.queue.put(item)
print ("Process Producer : item %d appended to queue %s"\
% (item,self.name))
time.sleep()
print ("The size of queue is %s"\
% self.queue.qsize()) class consumer(multiprocessing.Process):
def __init__(self, queue):
multiprocessing.Process.__init__(self)
self.queue = queue def run(self):
while True:
if (self.queue.empty()):
print("the queue is empty")
break
else:
time.sleep()
item = self.queue.get()
print ('Process Consumer : item %d popped from by %s \n'\
% (item, self.name))
time.sleep() if __name__ == '__main__':
queue = multiprocessing.Queue()
process_producer = producer(queue)
process_consumer = consumer(queue)
process_producer.start()
process_consumer.start()
process_producer.join()
process_consumer.join()
python 多线程队列的更多相关文章
- Python多线程-队列
队列就是存东西取东西,多用于多线程中 按照顺序 对传入的数据按规定的顺序输出 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" ...
- Python 多线程 队列 示例
Python3,开一个线程,间隔1秒把一个递增的数字写入队列,再开一个线程,从队列中取出数字并打印到终端 #! /usr/bin/env python3 import time import thre ...
- Python 用队列实现多线程并发
# Python queue队列,实现并发,在网站多线程推荐最后也一个例子,比这货简单,但是不够规范 # encoding: utf-8 __author__ = 'yeayee.com' # 由本站 ...
- Python多线程与队列
Python多线程与Queue队列多线程在感官上类似于同时执行多个程序,虽然由于GIL的存在,在Python中无法实现线程的真正并行,但是对于某些场景,多线程仍不失为一个有效的处理方法: 1,不紧急的 ...
- Python 多线程同步队列模型
Python 多线程同步队列模型 我面临的问题是有个非常慢的处理逻辑(比如分词.句法),有大量的语料,想用多线程来处理. 这一个过程可以抽象成一个叫“同步队列”的模型. 具体来讲,有一个生产者(Dis ...
- python多线程编程
Python多线程编程中常用方法: 1.join()方法:如果一个线程或者在函数执行的过程中调用另一个线程,并且希望待其完成操作后才能执行,那么在调用线程的时就可以使用被调线程的join方法join( ...
- Python 多线程教程:并发与并行
转载于: https://my.oschina.net/leejun2005/blog/398826 在批评Python的讨论中,常常说起Python多线程是多么的难用.还有人对 global int ...
- 【python,threading】python多线程
使用多线程的方式 1. 函数式:使用threading模块threading.Thread(e.g target name parameters) import time,threading def ...
- python消息队列snakemq使用总结
Python 消息队列snakemq总结 最近学习消息总线zeromq,在网上搜了python实现的消息总线模块,意外发现有个消息队列snakemq,于是拿来研究一下,感觉还是很不错的,入手简单使用也 ...
随机推荐
- 查看项目中的laravel的版本
方法1: 使用php artisan --version 方法2: 在项目文件中找vendor\laravel\framework\src\Illuminate\Foundation\Applicat ...
- Weka——PrincipalComponents分析
package weka.filters.unsupervised.attribute; PrincipalComponents 属性: /** The data to transform analy ...
- python中的re模块中的向后引用和零宽断言
1.后向引用 pattern = re.compile(r"(\w+)")#['hello', 'go', 'go', 'hello'] # pattern = re.compil ...
- Math.abs(~2018) —— 入群问答题
这道题的关键点在于对位操作符“~”的理解,以及内部的具体实现(设计到补码) 最后的结果是:2019 参考文章: http://www.w3school.com.cn/js/pro_js_operato ...
- echarts2简单笔记
1.代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- 数据仓库基础(十一)Informatica小技巧(2)
本文转载自:http://www.cnblogs.com/evencao/p/3152384.html 1.User shortcuts:shortcuts能实现快捷方式的复用.快捷方式可以根据源的变 ...
- 数据仓库基础(九)Informatica小技巧(1)
本文转载自:http://www.cnblogs.com/evencao/p/3148373.html link path:查看某个字段的来源去处,非常有参考的价值.右击你想要看的字段,选择 sele ...
- linux常用命令:split 命令
split是linux下常用的分割文件命令.Linux下文件分割可以通过split命令来实现,而用cat进行文件合并.而分割可以指定按行数分割和按大小分割两种模式. 1.命令格式: split [OP ...
- Selenium2+python自动化54-unittest生成测试报告(HTMLTestRunner)
前言 批量执行完用例后,生成的测试报告是文本形式的,不够直观,为了更好的展示测试报告,最好是生成HTML格式的. unittest里面是不能生成html格式报告的,需要导入一个第三方的模块:HTMLT ...
- quartz-job实现定时任务配置
使用quartz开源调度框架,写服务实现在一些指定场景发送特定短信,创建一个实现org.quartz.Job接口的java类.Job接口包含唯一的方法: public void execute(Job ...