python - How to avoid 'cannot release an unacquired lock' error? -
i have piece of code, in single thread repeatedly acquires , releases lock. many times, hits runtimeerror "cannot release unacquired lock". earlier, lock being used follows :
try : lock.acquire() <do-something> finally: lock.release()
which after consulting pep 343, realized wrong way of doing it, i.e. lock.acquire()
should outside of try...except
block. changed code :
lock = threading.lock() try: lock: <do-something> except exception e: logger.exception("failed acquire lock. %s" % str(e)) raise e
so questions have :
1) correct way of using lock? correct, mean, lock gets acquired safely , released after use. times when lock doesn't acquired, error logged during such events, instead of getting runtimeerror, that's why kept with
inside try..except
block.
2) couldn't find handling "cannot release unacquired lock" error specifically. or even, why or when occur? far know, acquire()
doesn't throw exception either. it's blocking call , supposed wait till lock ready acquired. insights ?
thanks in advance!
wiki
Comments
Post a Comment