python queue 调试_python:如何创建用于调试的持久内存结构
首先,可以使用以下方法对孔对象的不同部分进行pickle:
# gen_objects.py
import random
import pickle
class BigBadObject(object):
def __init__(self):
self.a_dictionary={}
for x in xrange(random.randint(1, 1000)):
self.a_dictionary[random.randint(1,98675676)]=random.random()
self.a_list=[]
for x in xrange(random.randint(1000, 10000)):
self.a_list.append(random.random())
self.a_string=''.join([chr(random.randint(65, 90))
for x in xrange(random.randint(100, 10000))])
if __name__=="__main__":
output=open('lotsa_objects.pickled', 'wb')
for i in xrange(10000):
pickle.dump(BigBadObject(), output, pickle.HIGHEST_PROTOCOL)
output.close()
一旦在不同的部分中生成了BigFile,就可以用一个python程序读取它,同时运行多个python程序读取每个不同的部分。
# reader.py
from threading import Thread
from Queue import Queue, Empty
import cPickle as pickle
import time
import operator
from gen_objects import BigBadObject
class Reader(Thread):
def __init__(self, filename, q):
Thread.__init__(self, target=None)
self._file=open(filename, 'rb')
self._queue=q
def run(self):
while True:
try:
one_object=pickle.load(self._file)
except EOFError:
break
self._queue.put(one_object)
class uncached(object):
def __init__(self, filename, queue_size=100):
self._my_queue=Queue(maxsize=queue_size)
self._my_reader=Reader(filename, self._my_queue)
self._my_reader.start()
def __iter__(self):
while True:
if not self._my_reader.is_alive():
break
# Loop until we get something or the thread is done processing.
try:
print "Getting from the queue. Queue size=", self._my_queue.qsize()
o=self._my_queue.get(True, timeout=0.1) # Block for 0.1 seconds
yield o
except Empty:
pass
return
# Compute an average of all the numbers in a_lists, just for show.
list_avg=0.0
list_count=0
for x in uncached('lotsa_objects.pickled'):
list_avg+=reduce(operator.add, x.a_list)
list_count+=len(x.a_list)
print "Average: ", list_avg/list_count
这种读取pickle文件的方式将占用另一种方式所需时间的1%。这是因为您同时运行100个并行线程。
python queue 调试_python:如何创建用于调试的持久内存结构相关推荐
- python queue 多线程_Python如何实现并行的多线程?
如果大家在学习中遇到困难,想找一个python学习交流环境,可以点击下方加入我们一起学习,欢迎零基础和大佬加入正在跳转jq.qq.com 有关Python问题都可以给我留言喔 接下来说正事,如果待处 ...
- python process 函数_Python Process创建进程的2种方法详解
前面介绍了使用 os.fork() 函数实现多进程编程,该方法最明显的缺陷就是不适用于 Windows 系统.本节将介绍一种支持 Python 在 Windows 平台上创建新进程的方法. Pytho ...
- python queue函数_Python模块:queue
一.Python queue Python queue模块有三种队列: 1.FIFO队列先进先出. 2.LIFO类似于堆,即先进后出. 3.还有一种是优先级队列级别越低越先出来. 针对这三种队列分别有 ...
- python queue 多进程_python中的Queue与多进程(multiprocessing)
最近接触一个项目,要在多个虚拟机中运行任务,参考别人之前项目的代码,采用了多进程来处理,于是上网查了查python中的多进程 一.先说说Queue(队列对象) Queue是python中的标准库,可以 ...
- python 类构造函数_Python构造函数创建类对象
python 类构造函数 In the last tutorial, we learned how to create a class, its instance and how to access ...
- python queue模块_Python的queue模块详解
Queue Queue是python标准库中的线程安全的队列(FIFO)实现,提供了一个适用于多线程编程的先进先出的数据结构,即队列,用来在生产者和消费者线程之间的信息传递 基本FIFO队列 clas ...
- python做词典_Python实现创建字典
Python编写一个名为 make_album() 的函数,它创建一个描述音乐专辑的字典. 1.这个函数应接受歌手的名字和专辑名,并返回一个包含这两项信息的字典.使用这个函数创建三个表示不同专辑的字典 ...
- 怎么创建python django项目_python怎么创建django
python怎么创建django?下面给大家带来具体的步骤: 一.在命令行中创建Django项目(Windows平台.python3.6) 1.建立虚拟环境(创建一个独立的Python运行环境) 为项 ...
- python queue死锁_Python学习【第24篇】:死锁,递归锁,信号量,Event事件,线程Queue...
一.死锁现象与递归锁 进程也是有死锁的 所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用, 它们都将无法推进下去.此时称系统处于死锁状态或系统 ...
最新文章
- java 日志_跟着Tomcat学编码:Java 原生日志框架分析
- 【技能提升】delegate为什么要用weak
- mysql数据库实战_主题:MySQL数据库操作实战
- php 实现自动加载更多,$.ajax+php实战教程之下拉时自动加载更多文章原理分析二...
- leetcode 706. 设计哈希映射
- JSP 简介(转载)
- linux 下安装minio并配置
- 阿里有php的研发团队么,【阿里巴巴】阿里集团-MMC技术部-研发工程师JAVA
- python web中的并发请求
- 牛客练习赛52 C 烹饪(容斥+扩展欧几里得)
- 面向过程编程与面向对象编程
- 春节怎么抢红包,python一招告诉你再也不会错过微信红包
- ubuntu18.04引导界面、登录界面美化
- GridWorld实训总结报告
- Photoshop 更改图片颜色
- 关于笔记本电脑蓝牙设置开关消失的处理方法
- Akka Actor模型的简介与Actor的创建方式
- 2021美亚杯(个人赛)练习记录
- 视频教程-PPT吸金大法 20+万年薪工作总结不用愁-Office/WPS
- photoshop二次开发python_PhotoShop工具开发之Python(二)