Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python, does lock get automatically released when an exception happens?

Tags:

python

locking

for example:

import threading

lock = threading.Lock()

with lock:
    some code that throws an exception

This is assuming that code that throws an exception isn't wrapped in a try except block.

like image 421
DiderDrogba344 Avatar asked Aug 20 '16 03:08

DiderDrogba344


1 Answers

The whole point of using your lock as a context manager (with lock:) is for Python to notify that lock object when an exception occurs.

So yes, the lock will unlock itself when an exception occurs because the with statement ensures it is notified of an exception.

like image 170
Martijn Pieters Avatar answered Oct 21 '22 04:10

Martijn Pieters