Make a small function thread safe

Lie Ryan lie.1296 at gmail.com
Fri Dec 16 17:08:04 EST 2011


On 12/17/2011 01:30 AM, Brad Tilley wrote:
> Or perhaps run should look like this instead:
>
>      def run(t):
>          lock.acquire()
>          shared_container.append(t.name <http://t.name>)
>          lock.release()
>
> That seems a bit barbaric to me, not sure.

change that to:

def run(t):
     with lock:
         shared_container.append(t.name <http://t.name>)


the `with-statement` will call lock.acquire() and lock.release().




More information about the Python-list mailing list