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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:47:44+00:00 2026-06-06T15:47:44+00:00

I have the following code: def query(self,query): lock = QMutexLocker(self.mutex) reply = self.conn.query(query) if

  • 0

I have the following code:

def query(self,query):
  lock = QMutexLocker(self.mutex)
  reply = self.conn.query(query)
  if (re.search("error", reply) != None):
    raise GeneralError("Query error")

  #more code...  
  return reply

Now, if the exception is thrown lock doesnt seem to be deleted, cause the mutex is not released. I can ofcourse do “del lock” everywhere, but that takes away the whole point of qmutexlocker. Does this have to do with Python garbage-collection? If so, that must mean QMutexLocker is not usable at all in Python?

  • 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-06T15:47:46+00:00Added an answer on June 6, 2026 at 3:47 pm

    If you want the mutex released prior to raising an exception, then release it:

    def query(self, query):
        lock = QMutexLocker(self.mutex)
        reply = self.conn.query(query)
        if re.search("error", reply):
            lock.unlock()
            raise GeneralError("Query error")
    

    If you are hoping that when lock goes out of scope that it will be instantly released, you are expecting too much of the interpreter. Since you know exactly when and why the lock should be released, do it.

    As a general rule — Python or elsewhere — you should always have a mutex bound the smallest possible action. I’ll assume that you know that whatever query is doing actually requires protection and it will still need it after the call to self.conn.query.

    added in response to comment:

    That’s a fair point that “must mean QMutexLocker is not usable at all” which I did miss. I assume you are referring to PySide.QtCore.QMutexLocker which makes the unlikely claim:

    Now, the mutex will always be unlocked when the
    PySide.QtCore.QMutexLocker object is destroyed (when the function
    returns since locker is an auto variable).

    It is unlikely because there is no such thing as an auto variable storage class in Python. I suspect this will, upon further investigation, prove to be a “let’s just wrap a C++ library and assume the scoping semantics work”. If this guess is correct you might feasibly use the with statement to better guarantee reliable unlocking.

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

Sidebar

Related Questions

i have the following code def get(self): date = datetime.date.today() loc_query = Location.all() last_cursor
I have following code: def whatever(url, data=None): req = urllib2.Request(url) res = urllib2.urlopen(req, data)
I have the following code: def maturities InfoItem.find_all_by_work_order(self.work_order).map(&:maturity) end I was thinking about changing
I have the following code: def add_record(self,values): self.sql.execute(INSERT INTO TEST VALUES (?,?) % values)
I have the following code: #!/usr/bin/ruby class Person def self.speak p = self.new puts
Let's say I have the following code: class MyClass(object): def __init__(self, param): self.param =
I am using Tornado and I have the following code: class UserHandler(RequestHandler): def get(self):
I have the following code in a collection: class Author(Agent): def foo(self): self.find_another_document_and_update_it(ids) self.processed
I have the following Ruby code: 94 def open_connection(info) 95 self.log opening #{info.inspect} 96
I have the following code: def commandType(self): import re print self.cmds[self.counter] if re.match(@,self.cmds[self.counter]): return

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.