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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:09:24+00:00 2026-05-16T23:09:24+00:00

I have a class that is only ever accessed externally through static methods. Those

  • 0

I have a class that is only ever accessed externally through static methods. Those static methods then create an object of the class to use within the method, then they return and the object is presumably destroyed. The class is a getter/setter for a couple config files and now I need to place thread locks on the access to the config files.

Since I have several different static methods that all need read/write access to the config files that all create objects in the scope of the method, I was thinking of having my lock acquires done inside of the object constructor, and then releasing in the destructor.

My coworker expressed concern that it seems like that could potentially leave the class locked forever if something happened. And he also mentioned something about how the destructor in python was called in regards to the garbage collector, but we’re both relatively new to python so that’s an unknown.

Is this a reasonable solution or should I just lock/unlock in each of the methods themselves?


Class A():
    rateLock = threading.RLock()
    chargeLock = threading.RLock()

    @staticmethod
    def doZStuff():
        a = A()
        a.doStuff('Z')

    @staticmethod
    def doYStuff():
        a = A()
        a.doStuff('Y')

    @synchronized(lock)
    def doStuff(self, type):
        if type == 'Z':
            otherstuff()
        elif type == 'B':
            evenmorestuff()

Is it even possible to get it to work that way with the decorator on doStuff() instead of doZStuff()

Update

Thanks for the answers everyone. The problem I’m facing is mostly due to the fact that it doesn’t really make sense to access my module asynchronously, but this is just part of an API. And the team accessing our stuff through the API was complaining about concurrency issues. So I don’t need the perfect solution, I’m just trying to make it so they can’t crash our side or get garbage data back

  • 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-16T23:09:25+00:00Added an answer on May 16, 2026 at 11:09 pm

    You are right with the garbage collection, so it is not a good idea.
    Look into decorators, for writing synchronized functions.

    Example: http://code.activestate.com/recipes/465057-basic-synchronization-decorator/

    edit
    I’m still not 100% sure what you have in mind, so my suggestion may be wrong:

    class A():
        lockZ = threading.RLock()
        lockY = threading.RLock()
    
        @staticmethod
        @synchroized(lockZ)
        def doZStuff():
            a = A()
            a.doStuff('Z')
    
        @staticmethod
        @synchroized(lockY)
        def doYStuff():
            a = A()
            a.doStuff('Y')
    
        def doStuff(self, type):
            if type == 'Z':
                otherstuff()
            elif type == 'B':
                evenmorestuff()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a helper class that should only ever run in a background thread.
I have these two methods on a class that differ only in one method
I have a class that only really ever needed by classes in a certain
Writing a PHP app and have several classes that only have static methods (no
I have a timer in my class that is I only ever want one
I have a CORE class that pertains only to my specific site, ie, it
Given: I have an interface. I have only class that implements that interface. Question:
I have a class called Property that I only need to display a few
I have a custom class that uses boost mutexes and locks like this (only
I have method in a class that I need to make sure is only

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.