python中的模块如何学习_在python中学习队列模块(如何运行它)
最近引入了队列设计,关于延迟处理能力以及实现“FIFO”等.
查看文档以尝试获取示例队列,以了解如何在我自己的设计/程序中实现它.但我遇到运行此代码的问题:
import queue
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()
def main():
q = queue.Queue(maxsize=0)
for i in range(num_worker_threads):
t = Thread(target=worker)
t.daemon = True
t.start()
for item in source():
q.put(item)
q.join() # block until all tasks are done
main()
问题:希望有人解释for循环正在做什么,我只是运行代码时出错,所以我不得不遗漏一些东西.
出现问题错误:
NameError:未定义全局名称’num_worker_threads’
谢谢-Python新手 –
for循环启动了许多工作线程来执行“worker”定义的功能.这是应该在python 2.7中运行在您的系统上的工作代码.
import Queue
import threading
# input queue to be processed by many threads
q_in = Queue.Queue(maxsize=0)
# output queue to be processed by one thread
q_out = Queue.Queue(maxsize=0)
# number of worker threads to complete the processing
num_worker_threads = 10
# process that each worker thread will execute until the Queue is empty
def worker():
while True:
# get item from queue, do work on it, let queue know processing is done for one item
item = q_in.get()
q_out.put(do_work(item))
q_in.task_done()
# squares a number and returns the number and its square
def do_work(item):
return (item,item*item)
# another queued thread we will use to print output
def printer():
while True:
# get an item processed by worker threads and print the result. Let queue know item has been processed
item = q_out.get()
print "%d squared is : %d" % item
q_out.task_done()
# launch all of our queued processes
def main():
# Launches a number of worker threads to perform operations using the queue of inputs
for i in range(num_worker_threads):
t = threading.Thread(target=worker)
t.daemon = True
t.start()
# launches a single "printer" thread to output the result (makes things neater)
t = threading.Thread(target=printer)
t.daemon = True
t.start()
# put items on the input queue (numbers to be squared)
for item in range(10):
q_in.put(item)
# wait for two queues to be emptied (and workers to close)
q_in.join() # block until all tasks are done
q_out.join()
print "Processing Complete"
main()
@handle的Python 3版本
import queue
import threading
# input queue to be processed by many threads
q_in = queue.Queue(maxsize=0)
# output queue to be processed by one thread
q_out = queue.Queue(maxsize=0)
# number of worker threads to complete the processing
num_worker_threads = 10
# process that each worker thread will execute until the Queue is empty
def worker():
while True:
# get item from queue, do work on it, let queue know processing is done for one item
item = q_in.get()
q_out.put(do_work(item))
q_in.task_done()
# squares a number and returns the number and its square
def do_work(item):
return (item,item*item)
# another queued thread we will use to print output
def printer():
while True:
# get an item processed by worker threads and print the result. Let queue know item has been processed
item = q_out.get()
print("{0[0]} squared is : {0[1]}".format(item) )
q_out.task_done()
# launch all of our queued processes
def main():
# Launches a number of worker threads to perform operations using the queue of inputs
for i in range(num_worker_threads):
t = threading.Thread(target=worker)
t.daemon = True
t.start()
# launches a single "printer" thread to output the result (makes things neater)
t = threading.Thread(target=printer)
t.daemon = True
t.start()
# put items on the input queue (numbers to be squared)
for item in range(10):
q_in.put(item)
# wait for two queues to be emptied (and workers to close)
q_in.join() # block until all tasks are done
q_out.join()
print( "Processing Complete" )
main()
python中的模块如何学习_在python中学习队列模块(如何运行它)相关推荐
- python 内存溢出能捕获吗_从0基础学习Python (19)[面向对象开发过程中的异常(捕获异常~相关)]...
从0基础学习Python (Day19) 面向对象开发过程中的=>异常 什么是异常 当程序在运行过程中出现的一些错误,或者语法逻辑出现问题,解释器此时无法继续正常执行了,反而出现了一些错误的 ...
- python库和模块的区别_在函数中导入python库与全局导入之间有何区别?
假设我要导入一个在函数内部使用的python库.在函数中导入库还是在全局范围内导入更好? 做这个 def test_func: import pandas as pd # code implement ...
- python模块的发布_(转载)Python中模块的发布与安装
模块(Module) Python中有一个概念叫做模块(module),这个和C语言中的头文件以及Java中的包很类似,比如在Python中要调用sqrt函数,必须用import关键字引入math这个 ...
- python中的ssl模块不能用_解决Python找不到ssl模块问题 No module named _ssl的方法
python安装完毕后,提示找不到ssl模块: [www@pythontab.com ~]$ python Python 2.7.15 (default, Oct 23 2018, 18:08:43) ...
- python中numpy数组的合并_基于Python中numpy数组的合并实例讲解
基于Python中numpy数组的合并实例讲解 Python中numpy数组的合并有很多方法,如 - np.append() - np.concatenate() - np.stack() - np. ...
- python源码学习_【Python学习】Python源码阅读(一)
最近想读读Python源码,任何东西学习方法基本都是一样的,先从总体框架进行了解,再从自己侧重的方面逐步深入. 1. Python总体架构 左边是Python提供的大量的模块.库以及用户自定义的模块. ...
- 导入python标准数学函数模块的语句_《Python编程快速上手——让繁琐工作自动化》——2.8 导入模块...
本节书摘来自异步社区<Python编程快速上手--让繁琐工作自动化>一书中的第2章,第2.8节,作者[美] Al Sweigart,王海鹏 译,更多章节内容可以访问云栖社区"异步 ...
- python一切皆对象的理解_在 Python 中万物皆对象
在 Python 中一切都是对象,并且几乎一切都有属性和方法.所有的函数都有一个内置的 __doc__ 属性,它会返回在函数源代码中定义的 doc string:sys 模块是一个对象,它有一个叫作 ...
- python网络爬虫权威指南 豆瓣_豆瓣Python大牛写的爬虫学习路线图,分享给大家!...
豆瓣Python大牛写的爬虫学习路线图,分享给大家! 今天给大家带来我的Python爬虫路线图,仅供大家参考! 第一步,学会自己安装python.库和你的编辑器并设置好它 我们学习python的最终目 ...
- python支持的编程范式有_【Python学习手册】chapter1 前面
读书笔记! Python命名:以英国喜剧组'Monty Python'命名--BBC20世纪70年代的<Monty Python's and the Holy Grai>的制片方,和我们认 ...
最新文章
- ASP.NET 2.0 读取配置文件[INI](示例代码下载)
- BusinessFrameWork
- VC6如何使用VS2005中的CImage类功能
- Debian耳机声音问题
- c++ 图的连通分量是什么_学习数据结构第五章:图(图的遍历操作)
- 基于USB通信的开关量输入输出板
- GNU-Radio USRP Example
- python 录制网易云登陆_Python爬虫教程,爬取网易云的音乐
- matlab工具箱参数修改,使用matlab工具箱标定摄像头(内参数)
- stl文件的解析和在线3d打印
- UA MATH523A 实分析3 积分理论例题 Fubini定理计算简单一元定积分的一个例题
- Python3学习(34)--简单网页内容抓取(爬虫入门一)
- 数值分析常见算法C++实现
- Liber 1. 《活着》:人生如逆旅,我亦是行人
- windows 2000/XP/2003服务全集
- 今天面了个腾讯拿30k出来的,真是小母牛按门铃,牛逼到家了
- 信息学奥赛与大学计算机课程,信息学是什么课程
- python三维曲面拟合_用Python拟合多项式曲面
- 装饰器设计模式代码套路-咖啡加奶
- ROS官网初级教程学习总结(1-4)