Python使用functools实现注解同步方法

在 Python 中没有类似 Java 中使用的 synchronized 关键字来同步方法,因此在 Python 中要实现同步方法,通常我们是使用 threading.Lock() 来实现。在进入函数的地方获取锁,出函数的时候释放锁,这样实现代码看起好非常不好看。另外网上也有人给出了其它几种实现方式,但看起来都不美气。

今天我在做项目的时候突然想到是不是可以通过 functools 来实现通过注解来标注方法为同步方法。

首先要求自己的类中有一个锁对象并且在类初始化的时候初始化这个锁对象,比如:

class MyWorker(object):
  def __init__(self):
    self.lock = threading.Lock()
    ...
  ...

然后创建一个 synchronized 函数,这个函数装饰具体对象的具体方法,将方法放到获取/释放锁之间来运行,如下

def synchronized(func):
  @functools.wraps(func)
  def wrapper(self,*args,**kwargs):
    with self.lock:
      return func(self,**kwargs)
  return wrapper

最后在需要使用同步的方法上使用 @synchronized 来标准方法是同步方法,比如:

@synchronized
def test(self):
  ...

下面是一个完整例子,仅供参考:

import threading
import functools
import time
def synchronized(func):
  @functools.wraps(func)
  def wrapper(self,**kwargs)
  return wrapper
class MyWorker(object):
  def __init__(self):
    self.lock = threading.Lock()
    self.idx = 0
  @synchronized
  def test1(self):
    for i in range(1,11):
      self.idx = self.idx + 1
      print "Test1: " + str(self.idx)
      time.sleep(1)
  @synchronized
  def test2(self):
    for i in range(1,11):
      self.idx = self.idx + 1
      print "Test2: " + str(self.idx)
      time.sleep(1)
  @synchronized
  def test3(self):
    for i in range(1,11):
      self.idx = self.idx + 1
      print "Test3: " + str(self.idx)
      time.sleep(1)
worker = MyWorker()
threading.Thread(target=worker.test1).start()
threading.Thread(target=worker.test2).start()
threading.Thread(target=worker.test3).start()

总结

以上所述是小编给大家介绍的Python使用functools实现注解同步方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

相关文章

Python中的函数(二) 在上一篇文章中提到了Python中函数的定...
Python中的字符串 可能大多数人在学习C语言的时候,最先接触...
Python 面向对象编程(一) 虽然Python是解释性语言,但是它...
Python面向对象编程(二) 在前面一篇文章中谈到了类的基本定...
Python中的函数(一) 接触过C语言的朋友对函数这个词肯定非...
在windows下如何快速搭建web.py开发框架 用Python进行web开发...