How to implement Queue in python

0 votes

I am using Eclipse and getting this 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? 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 and Python 3.x

Nov 2, 2018 in Python by Priyaj
• 56,900 points
154 views

1 answer to this question.

+1 vote

You are missing this 

from queue import *

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

q = Queue(maxsize=0)
answered Nov 2, 2018 by Nabarupa

Related Questions In Python

0 votes
1 answer

How to implement Hashmaps in Python

Python dictionary is a built-in type that supports ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,720 points
143 views
0 votes
1 answer

How to use multiprocessing queue in Python?

This is a simple example of a ...READ MORE

answered Oct 25, 2018 in Python by SDeb
• 13,160 points
394 views
0 votes
1 answer

How to implement Linked List in Python?

You can use Deque that works better than linked list ...READ MORE

answered Oct 26, 2018 in Python by Priyaj
• 56,900 points
91 views
0 votes
0 answers

How to implement multiple try codes in a single block using Python?

Hi all, As per the title, I am ...READ MORE

Jan 14 in Python by Anirudh
• 2,070 points
44 views
0 votes
1 answer

Crawling after login in Python

You missed a few login data forms, ...READ MORE

answered Sep 7, 2018 in Python by Priyaj
• 56,900 points
387 views
0 votes
1 answer

Crawling after login in Python

You missed a few login data forms, ...READ MORE

answered Sep 14, 2018 in Python by Priyaj
• 56,900 points
38 views
0 votes
1 answer

“stub” __objclass__ in a Python class how to implement it?

You want to avoid interfering with this ...READ MORE

answered Sep 27, 2018 in Python by Priyaj
• 56,900 points
58 views
+1 vote
1 answer

How is raw_input() and input() in python3.x?

raw_input() was renamed to input() so now input() returns the exact string ...READ MORE

answered Oct 30, 2018 in Python by Priyaj
• 56,900 points
46 views
0 votes
1 answer

How to implement Stack in python

class Stack: def __init__(self): ...READ MORE

answered Nov 15, 2018 in Python by findingbugs
• 3,170 points
39 views
+3 votes
5 answers

How to read multiple data files in python

Firstly we will import pandas to read ...READ MORE

answered Apr 6, 2018 in Python by DeepCoder786
• 1,720 points
2,239 views