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

  • Home
  • SEARCH
  • 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 4627300
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:32:53+00:00 2026-05-22T03:32:53+00:00

I have a queue that always needs to be ready to process items when

  • 0

I have a queue that always needs to be ready to process items when they are added to it. The function that runs on each item in the queue creates and starts thread to execute the operation in the background so the program can go do other things.

However, the function I am calling on each item in the queue simply starts the thread and then completes execution, regardless of whether or not the thread it started completed. Because of this, the loop will move on to the next item in the queue before the program is done processing the last item.

Here is code to better demonstrate what I am trying to do:

queue = Queue.Queue()
t = threading.Thread(target=worker)
t.start()

def addTask():
    queue.put(SomeObject())

def worker():
    while True:
        try:
            # If an item is put onto the queue, immediately execute it (unless 
            # an item on the queue is still being processed, in which case wait 
            # for it to complete before moving on to the next item in the queue)
            item = queue.get()
            runTests(item)
            # I want to wait for 'runTests' to complete before moving past this point
        except Queue.Empty, err:
            # If the queue is empty, just keep running the loop until something 
            # is put on top of it.
            pass

def runTests(args):
    op_thread = SomeThread(args)
    op_thread.start()
    # My problem is once this last line 't.start()' starts the thread, 
    # the 'runTests' function completes operation, but the operation executed
    # by some thread is not yet done executing because it is still running in
    # the background. I do not want the 'runTests' function to actually complete
    # execution until the operation in thread t is done executing.
    """t.join()"""
    # I tried putting this line after 't.start()', but that did not solve anything.
    # I have commented it out because it is not necessary to demonstrate what 
    # I am trying to do, but I just wanted to show that I tried it.

Some notes:

This is all running in a PyGTK application. Once the ‘SomeThread’ operation is complete, it sends a callback to the GUI to display the results of the operation.

I do not know how much this affects the issue I am having, but I thought it might be important.

  • 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-05-22T03:32:54+00:00Added an answer on May 22, 2026 at 3:32 am

    A fundamental issue with Python threads is that you can’t just kill them – they have to agree to die.

    What you should do is:

    1. Implement the thread as a class
    2. Add a threading.Event member which the join method clears and the thread’s main loop occasionally checks. If it sees it’s cleared, it returns. For this override threading.Thread.join to check the event and then call Thread.join on itself
    3. To allow (2), make the read from Queue block with some small timeout. This way your thread’s “response time” to the kill request will be the timeout, and OTOH no CPU choking is done

    Here’s some code from a socket client thread I have that has the same issue with blocking on a queue:

    class SocketClientThread(threading.Thread):
        """ Implements the threading.Thread interface (start, join, etc.) and
            can be controlled via the cmd_q Queue attribute. Replies are placed in
            the reply_q Queue attribute.
        """
        def __init__(self, cmd_q=Queue.Queue(), reply_q=Queue.Queue()):
            super(SocketClientThread, self).__init__()
            self.cmd_q = cmd_q
            self.reply_q = reply_q
            self.alive = threading.Event()
            self.alive.set()
            self.socket = None
    
            self.handlers = {
                ClientCommand.CONNECT: self._handle_CONNECT,
                ClientCommand.CLOSE: self._handle_CLOSE,
                ClientCommand.SEND: self._handle_SEND,
                ClientCommand.RECEIVE: self._handle_RECEIVE,
            }
    
        def run(self):
            while self.alive.isSet():
                try:
                    # Queue.get with timeout to allow checking self.alive
                    cmd = self.cmd_q.get(True, 0.1)
                    self.handlers[cmd.type](cmd)
                except Queue.Empty as e:
                    continue
    
        def join(self, timeout=None):
            self.alive.clear()
            threading.Thread.join(self, timeout)
    

    Note self.alive and the loop in run.

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

Sidebar

Related Questions

I have a Queue object that I need to ensure is thread-safe. Would it
I have a Queue<T> object that I have initialised to a capacity of 2,
I have a priority queue implementation in C# that I want to add a
I have a list/queue of 200 commands that I need to run in a
I have one thread that writes results into a Queue. In another thread (GUI),
As part of a Linux benchmark application, I have a parent process that forks
I have been trying to access a queue that is on the cloud while
So I have a program that runs a simulation When you select the model,
I'm trying to create simply connect with ActiveMQ using JNDI. I have: Queue named
I have multiple processes monitoring an MSMQ queue. I want to do multi-step operations

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.