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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:01:01+00:00 2026-06-09T19:01:01+00:00

I have a list in my python program that gets new items on certain

  • 0

I have a list in my python program that gets new items on certain occasions (It’s a message-queue consumer). Then I have a thread that every few minutes checks to see if there’s anything in the list, and if there is then I want to do an action on each item and then empty the list.

Now my problem: should I use locks to ensure that the action in the subthread is atomic, and does this ensure that the main thread can’t alter the list while I’m going through the list?
Or should I instead use some kind of flag?

Pseudocode to make my problem clearer.

Subthread:

def run(self):
    while 1:
        if get_main_thread_list() is not empty:
            do_operations()
        empty_the_list()
        sleep(30)

Main thread:

list = []

def on_event(item):
    list.add(item)

def main():
    start_thread()
    start_listening_to_events()

I hope this makes my problem clearer, and any links to resources or comments are obviously welcome!

PS: I’m well aware that I just might not grasp threaded programming well enough for this question, if you believe so could you please take some time explaining whats wrong with my reasoning if you have the time.

  • 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-09T19:01:03+00:00Added an answer on June 9, 2026 at 7:01 pm

    should I use locks to ensure that the action in the subthread is atomic, and does this ensure that the main thread can’t alter the list while I’m going through the list?

    Yes. If you implement it correctly yes.

    Or should I instead use some kind of flag?

    “some kind of flag” == lock, so you’d better use threading locks.

    Important: It looks to me like you’re trying to reimplement the queue module from the stdlib, you might want to take a look at it.

    Other than having a bunch of interesting features is also thread safe:

    The queue module implements multi-producer, multi-consumer queues. 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.

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

Sidebar

Related Questions

I have a list in Python that I generate as part of the program.
I have a simple Python program that gets artwork from MP3 files. But when
I have a Python list that I'm supplying to the template: {'error_name':'Please enter a
I have a Python program that takes around 10 minutes to execute. So I
I have a program in python that includes a class that takes a function
I have a Python program that spawns many threads, runs 4 at a time,
I have a python program in an embedded system that needs to write to
I have a thread which produces some data (a python list) and which shall
I have a python program that produces an error: def update_ranges(phonemelist) : updating the
I have a python program that is running for many days. Memory usage does

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.