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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:38:09+00:00 2026-05-28T01:38:09+00:00

I’m trying to have lock’s for similar input strings. But the lock’s doesn’t work.

  • 0

I’m trying to have lock’s for similar input strings. But the lock’s doesn’t work. The second lock for the same string doesn’t wait but the first release will destroy the lock so the second release will raise an error.

test.py

import threading
import time

class TestThread(threading.Thread):
    def __init__(self, input):
        threading.Thread.__init__(self)
        self.input = input

        lock_wrap = "TestThread." + self.input + " = threading.Lock()"
        eval(compile(lock_wrap,'<string>','exec'))

    def run(self):
        acquire_wrap = "TestThread." + self.input + ".acquire()"
        exec(compile(acquire_wrap,'<string>','exec'))
        print("waste some time for %s" % self.input)
        time.sleep(30)
        print("%s done" % self.input)
        release_wrap = "TestThread." + self.input + ".release()"
        exec(compile(release_wrap,'<string>','exec'))

my_threads = [] 

while True:
    input = raw_input("> ") 
    if input == "end": 
        break 
    thread = TestThread(input)
    my_threads.append(thread)
    thread.start()

for t in my_threads:
    t.join()

result

$ python test.py 
> foo
> waste some time for foo
bar
waste some time for bar
 > foo
> waste some time for foo
foo done
bar done
foo done
Exception in thread Thread-3:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
    self.run()
  File "test.py", line 19, in run
    exec(compile(release_wrap,'<string>','exec'))
  File "<string>", line 1, in <module>
error: release unlocked lock
  • 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-28T01:38:10+00:00Added an answer on May 28, 2026 at 1:38 am

    Using eval for this application doesn’t make sense; why not just keep one lock per thread?

    class TestThread(threading.Thread):
        def __init__(self, input):
            threading.Thread.__init__(self)
            self.input = input
            self.lock = threading.Lock()
    
        def run(self):
            self.lock.acquire()
            print("waste some time for %s" % self.input)
            time.sleep(5)
            print("%s done" % self.input)
            self.lock.release()
    

    You mention wanting to have the same lock for identical strings, but in that case, of course when a lock for one string ends, so will the locks for other threads using the same string. Perhaps if you explained your motivation more I could suggest another solution.

    ETA: If you’re sure that, for your particular application, you want to have the same lock for the same string, this would be one way to do it:

    LOCK_DICT = {}
    LOCK_DICT_LOCK = threading.RLock()
    
    class TestThread(threading.Thread):
        def __init__(self, input):
            threading.Thread.__init__(self)
            self.input = input
    
            with LOCK_DICT_LOCK:
                if self.input not in LOCK_DICT:
                    LOCK_DICT[self.input] = threading.Lock()
    
        def run(self):
            with LOCK_DICT_LOCK:
                lock = LOCK_DICT[self.input]
    
            lock.acquire()
            print("waste some time for %s" % self.input)
            time.sleep(5)
            print("%s done" % self.input)        
            lock.release()
    

    Note that this particular version uses global variables, which isn’t ideal design, but it is much better than using eval as in your design above (which also kept the variables as class properties). Whatever code you use can of course put LOCK_DICT and LOCK_DICT_LOCK somewhere that isn’t global (say, a class you call ThreadManager).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.