在python中使用队列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14584958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Queue in python
提问by Bain
I'm trying to run the following in Eclipse (using PyDev) and I keep getting error :
我试图在 Eclipse 中运行以下(使用 PyDev),但我不断收到错误:
q = queue.Queue(maxsize=0) NameError: global name 'queue' is not defined
q = queue.Queue(maxsize=0) NameError:未定义全局名称“队列”
I've checked the documentations and appears that is how its supposed to be placed. Am I missing something here? Is it how PyDev works? or missing something in the code? Thanks for all help.
我检查了文档,似乎这就是它应该放置的方式。我在这里错过了什么吗?这是 PyDev 的工作原理吗?或者在代码中遗漏了什么?感谢所有帮助。
from queue import *
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()
Using: Eclipse SDK
使用:Eclipse SDK
Version: 3.8.1 Build id: M20120914-1540
版本:3.8.1 版本号:M20120914-1540
and Python 3.3
和 Python 3.3
采纳答案by David Robinson
You do
你做
from queue import *
This imports all the classes from the queuemodule already. Change that line to
这queue已经从模块中导入了所有类。将该行更改为
q = Queue(maxsize=0)
回答by Ashwini Chaudhary
That's because you're using : from queue import *
那是因为您正在使用: from queue import *
and then you're trying to use :
然后你尝试使用:
queue.Queue(maxsize=0)
remove the queuepart, because from queue import *imports all the attributes to the current namespace. :
删除该queue部分,因为将from queue import *所有属性导入到当前命名空间。:
Queue(maxsize=0)
or use import queueinstead of from queue import *.
或使用import queue代替from queue import *.
回答by Mark Francis
You Can install kombu with pip install kombu
您可以使用 pip install kombu 安装 kombu
and then Import queue Just like this
然后导入队列就像这样
from kombu import Queue
从昆布进口队列
回答by Vladyslav
If you import from queue import *this is mean that all classes and functions importing in you code fully. So you must not write name of the module, just q = Queue(maxsize=100). But if you want use classes with name of module: q = queue.Queue(maxsize=100)you mast write another import string: import queue, this is mean that you import all module with all functions not only all functions that in first case.
如果您导入,from queue import *这意味着所有类和函数都在您的代码中完全导入。所以你不能写模块的名字,只是q = Queue(maxsize=100). 但是,如果您想使用具有模块名称的类:q = queue.Queue(maxsize=100)您必须编写另一个导入字符串:import queue,这意味着您导入所有模块的所有功能,而不仅仅是第一种情况下的所有功能。

