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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:34:59+00:00 2026-05-27T20:34:59+00:00

I am working with locks within a class to avoid multiple threads adding the

  • 0

I am working with locks within a class to avoid multiple threads adding the same elements to a list twice. However, my class gets stuck when I want to access a method protected by a lock from within the class itself.

The class looks like this (simplified, so it does not make much sense anymore):

import threading

class List(object):
    def __init__(self):
        self.list = []
        self.lock = threading.Lock()

    def add(self, element):
        print("adding")
        with self.lock:
            if not element in self.list:
                self.list.append(element)

    def get(self):
        print("getting")
        with self.lock:
            # Try to get more elements, might fail
            if len(self.list) == 0:
                self.getFromDb()

            if len(self.list) > 0:
                el = self.list.pop()
                return el
            else:
                return None

    def getFromDb(self):
        print("getting from db")
        # Emulate an hardcoded database ;)
        self.add("i am new")
        # Here we will enter a block that is already locked by
        # the thread who called get() so my method waits
        # infinitely

So the following test file does not complete as it gets stuck when it wants to add new elements to the list:

list = List()
print("= 1 =")
list.add("hi")
print("= 2 =")
print(list.get())
print("= 3 =")
print(list.get())
print("= 4 =")

Output is:

= 1 =
adding
adding
= 2 =
getting
hi
= 3 =
getting
getting from db
adding

There you can see that all methods are working, but when a new element shall be added from getFromDb (called from the locked block inside add) who itself calls add again, the application cannot continue.

Are there other methods than just filling the list directly in getFromDb without using add? This would not be that nice, as add does checking for duplicates (already in this very basic example).

  • 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-27T20:35:00+00:00Added an answer on May 27, 2026 at 8:35 pm

    You will need a recursive lock.
    There is one in python’s threading – see here.

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

Sidebar

Related Questions

I've recently been working with code that looks like this: using namespace std; class
I'm currently working on Mangler's Android implementation. I have a java class that looks
In the project I'm working on now, we have base Entity class that looks
I'm working on a Java project where I have created a class that looks
I'm working in a CakePHP Model class, writing a function that is supposed to
I have a collection of TwitterCollection objects held within a List. I am populating
It looks like there a few working solutions for using custom true type fonts
It looks like SVN properties are stored in the local Working Copy folder, not
I am working with a set of data that looks something like the following.
I'm working with a GridView that uses a SqlDataSource element that looks like this:

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.