python 中的互斥装饰器

发布于 2024-10-16 15:37:54 字数 477 浏览 2 评论 0原文

我看到了这个: http://yukelzon.blogspot.com/2005/07/python -locks.html 在寻找解决方案以向现有代码添加一些锁定以写入文件时。

满足我需要的简化版本如下所示:

  def mlock(orig):
    def inner(*args, **kwargs):
      Coloring.lock.acquire()
      try:
        ret = orig(*args, **kwargs)
        return ret
      finally:
        Coloring.lock.release()
    return inner

锁是一个类变量。任何人都可以想到改进或更好的方法吗?

I saw this: http://yukelzon.blogspot.com/2005/07/python-locks.html when looking for a solution to add some locking to existing code around writing to a file.

The simplified version for my needs looks like this:

  def mlock(orig):
    def inner(*args, **kwargs):
      Coloring.lock.acquire()
      try:
        ret = orig(*args, **kwargs)
        return ret
      finally:
        Coloring.lock.release()
    return inner

The lock is a class variable. Can any one think of improvements or better ways?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心作怪 2024-10-23 15:37:54

如果您使用的是 Python 2.6+(我认为),互斥对象是上下文管理器,因此:

def mlock(f):
    def inner(*args, **kwargs):
        with Coloring.lock:
            return f(*args, **kwargs)
    return inner

If you're using Python 2.6+ (I think), mutex objects are context managers, so:

def mlock(f):
    def inner(*args, **kwargs):
        with Coloring.lock:
            return f(*args, **kwargs)
    return inner
时光沙漏 2024-10-23 15:37:54

我不喜欢自由变量。我可能会让锁成为装饰器的显式变量,如下所示:

def mlock(Coloring):
    def mlock_decorator(orig):
        def inner(*args, **kwargs):
          Coloring.lock.acquire()
          try:
            ret = orig(*args, **kwargs)
            return ret
          finally:
            Coloring.lock.release()
        return inner
    return mlock_decorator

好吧,因为 Coloring 是一个类变量,如果您希望它非常特定于该类,请显式访问它:

def mlock(orig):
    def inner(self, *args, **kwargs):
      self.Coloring.lock.acquire()
      try:
        ret = orig(self, *args, **kwargs)
        return ret
      finally:
        self.Coloring.lock.release()
    return inner

或者,至少声明该锁是全局的

def mlock(orig):
    global Coloring
    def inner(*args, **kwargs):
      Coloring.lock.acquire()
      try:
        ret = orig(*args, **kwargs)
        return ret
      finally:
        Coloring.lock.release()
    return inner

I'm not a fan of free variables. I would probably make the lock an explicit variable of the decorator, like so:

def mlock(Coloring):
    def mlock_decorator(orig):
        def inner(*args, **kwargs):
          Coloring.lock.acquire()
          try:
            ret = orig(*args, **kwargs)
            return ret
          finally:
            Coloring.lock.release()
        return inner
    return mlock_decorator

Well, since Coloring is a class variable, and if you expect this to be pretty specific to that class, access it explicitly:

def mlock(orig):
    def inner(self, *args, **kwargs):
      self.Coloring.lock.acquire()
      try:
        ret = orig(self, *args, **kwargs)
        return ret
      finally:
        self.Coloring.lock.release()
    return inner

Or, at very least, declare that the lock is a global

def mlock(orig):
    global Coloring
    def inner(*args, **kwargs):
      Coloring.lock.acquire()
      try:
        ret = orig(*args, **kwargs)
        return ret
      finally:
        Coloring.lock.release()
    return inner
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文