Bain Bain - 4 months ago 24
Python Question

Using Queue in python

I'm trying to run the following in Eclipse (using PyDev) and I keep getting error :

q = queue.Queue(maxsize=0)
NameError: global name 'queue' is not defined


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.

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

Version: 3.8.1
Build id: M20120914-1540

and Python 3.3

Answer

You do

from queue import *

This imports all the classes from the queue module already. Change that line to

q = Queue(maxsize=0)
Comments