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
• 58,020 points
3,075 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
1,079 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,300 points
6,227 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
• 58,020 points
1,050 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, 2019 in Python by Anirudh
• 2,090 points
668 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
• 58,020 points
1,676 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
• 58,020 points
853 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
• 58,020 points
2,350 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
• 58,020 points
897 views
0 votes
2 answers

How to implement Stack in python

There are different method to implement stack ...READ MORE

answered Jun 21, 2020 in Python by pagarsach
• 160 points
1,349 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
15,277 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP