[IronPython] Thread Locking

Dino Viehland dinov at microsoft.com
Mon Dec 13 19:09:49 CET 2010


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-bounces at lists.ironpython.com [mailto:users-bounces at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20101213/b2b962a3/attachment.html>


More information about the Ironpython-users mailing list