Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7998867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:20:12+00:00 2026-06-04T15:20:12+00:00

I tried to put together a very simple multithreading model, and so far it

  • 0

I tried to put together a very simple multithreading model, and so far it seems to work. My question is how am I sure that two threads will not grab the same value from the queue simultaneously and give me repeats? Is there just some built in method that prevents this? I added a delay to try to put time between when each thread will get a value from the queue, is this necessary?

from Queue import Queue
from threading import Thread
import time

class myThread(Thread):
    def __init__(self,queue):
        Thread.__init__(self)
        self.queue = queue
    def run(self):       
        while True:                      
            time.sleep(0.0001)   #not sure if this is good enough to compensate for overlap in pulling the same value out of a queue         
            task = self.queue.get() #pull a number from the queue,
            if task != None:          
                out.append(task) #This will be where you        
            else:               
                break


queue = Queue()
out = []


for i in range(10):
    t = myThread(queue)
    t.start()
for i in range(100):
    queue.put(i)
print out
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:20:13+00:00Added an answer on June 4, 2026 at 3:20 pm

    The Queue class implements locking to prevent this happening, see http://docs.python.org/library/queue.html, in particular:

    It is especially useful in threaded programming when information must
    be exchanged safely between multiple threads. The Queue class in this
    module implements all the required locking semantics.

    So you don’t need any delay, the queue should work exactly as you want. That’s what it was written for (basically) 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on the little that i know, I tried to put together the relationships
I've tried to put together a solution from similar questions but have failed miserably.
I'm just starting to work with JSF and Facelets and I've put together a
Today, as I tried to put together a script in Octave, I thought, this
I've put together a little script that works fine in general, it just needs
For example, if I created a framework that put together Ogre3D with RakNet and
I just put together a small script for a team of users that collects
I currently have a very simple model Coupon. I suddenly am interested to add
I have just put together a Go package that is going to be a
I've got some code that uses the regular Forms API to put together a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.