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 4078014
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:37:20+00:00 2026-05-20T17:37:20+00:00

Since I’m not an expert for Python and multi-threaded programming I’d like to ask

  • 0

Since I’m not an expert for Python and multi-threaded programming I’d like to ask you if my implementation is correct.

My aim was to extend the Queue class so it can be cleared. And the removed items should be returned. That’s all. My implementation is:

import Queue

class ClearableQueue(Queue.Queue):

    def __init__(self, maxsize):
        Queue.Queue.__init__(self, maxsize)

    def clear(self):
        self.mutex.acquire()

        copyOfRemovedEntries = list(self.queue)
        self.queue.clear()
        self.unfinished_tasks = 0
        self.all_tasks_done.notifyAll()
        self.not_full.notifyAll()

        self.mutex.release()

        return copyOfRemovedEntries

Is it correct?
Thank you.

Update: Unfortunately, this implementation is still insufficient since task_done could throw the ValueError exception after clear() is called.

More precisely: The queue is thought to be used in a multi-threaded environment. So assume one producer and one worker thread (but you can consider more threads as well). Normally, if a worker thread calls get() a task_done() should be called after the worker has done its work. If this happens this way then it could happen that the producer thread call clear() for some reason, right after the worker thread has called get() and before task_done() has been called. This works so far, however, if the worker thread would like to call task_done() then the exception will be thrown. This is because task_done() checks the number of unfinished tasks by checking unfinished_tasks of the Queue class.

It would be interesting if this issue could be handled solely by the ClearableQueue class so the clear() method can be called without worries. Or if there has to be something different that controlls the method calls somehow.

Actually, in my concrete case I don’t use the join() method so I don’t need to call task_done(). However, I’d like to make this feature complete. Could be useful for other people as well.

  • 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-20T17:37:20+00:00Added an answer on May 20, 2026 at 5:37 pm

    You appear to be suffering some sort of race condition, and if I understand it, the current situation is that you sometimes get:

    T1: |----->|------------->|-------------->|
        | get  |    some_opp  | task_done     |
    T2: |---------->|------>|---------------->|
        | other_opp | clear | yet_another_opp |
    

    Where clear is performed within get and task_done. This causes a crash. As I understand it you need some way do do this:

    T1: |----->|------------->|-------------->|
        | get  |    some_opp  | task_done     |
    T2: |---------->|------------------------>|------>|
        | other_opp | wait_for_task_done      | clear |
    

    If this is correct, you may need a second lock, set by get and released by task_done, which says ‘this queue can’t be cleared’. You might then need to have a version of get and task_done that does not do this for special cases where you really know that you’re doing.

    An alternative to this is to have a more atomic lock which allows you to do this:

    T1: |----->|------------------->|-------------->|------------->|
        | get  |    some_opp        | task_done     | finish_clear |
    T2: |---------->|-------------->|---------------->|
        | other_opp | partial_clear | yet_another_opp |
    

    Where you say ‘I’m not done with this task but you can clear the rest, then tells task_done that the task had an attempt at being cleared, so it should do something after. This is starting to get fairly complex though.

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

Sidebar

Related Questions

Since Rails is not multithreaded (yet), it seems like a threaded web framework would
Since I am not getting anywhere with my previous question , I would like
Since I started studying object-oriented programming, I frequently read articles/blogs saying functions are better,
Since I'm using Java 1.4.2, this means that I cannot use Java's implementation of
Since Xcode 4, using BWToolkit isn't really easy. In fact, it's not even possible
Since I have to multithread which I can not eloquently solve in PHP I
Since CS3 doesn't have a web service component, as previous versions had, is there
Since both a Table Scan and a Clustered Index Scan essentially scan all records
Since Graduating from a very small school in 2006 with a badly shaped &
Since the WMI class Win32_OperatingSystem only includes OSArchitecture in Windows Vista, I quickly wrote

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.