Lock is just syntactic sugar for doing Monitor.Enter/Monitor.Exit calls on the 
object and setting up the try/finally.  Here's the IronPython equivalent:

from System.Threading import Monitor

class Locker(object):
    def __init__(self, obj):
            self.obj = obj

    def __enter__(self):
            Monitor.Enter(self.obj)

    def __exit__(self, exc_type, exc_value, exc_tb):
            Monitor.Exit(self.obj)


with Locker(object()):
    print 'hello'


(I thought we had considered putting something like this in the clr module at 
one point in time so you wouldn't have to define the Locker class but it looks 
like that never made it in but it'd be an easy thing to add if someone wanted 
to give it a try).


From: users-boun...@lists.ironpython.com 
[mailto:users-boun...@lists.ironpython.com] On Behalf Of Sam Dozor
Sent: Monday, December 13, 2010 9:18 AM
To: Discussion of IronPython
Subject: [IronPython] Thread Locking

Hello All,

I'm trying to use IronPython to lock a thread and have not been able to import 
the correct modules.  I have IronPython.modules.dll in the same directory as 
IronPython.dll, and yet when I try the line "import threading" I get something 
like "threading module does not exist".

I'm basically trying to use the lock() keyword from .NET, but if I can use 
Python's locking capabilities I'd do that too.  Any ideas?

Thanks,
Sam
_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to