Tuesday, February 12, 2013

locks in python thread

When doing multi threading program in any language, it is a big problem when having shared variables or some shared objects. In this kind of situation, it is required to lock some variables until finish the some process of one thread. In order to do this we can use locks. In python there is a library for import in order to do this.

ex:

from threading import  Lock

......
....

#initialize lock
lock=Lock()

.....

#when needed to add lock
lock.acquire()
#do the stuff
#remove lock
lock.release()

0 comments:

Post a Comment