import threading

def tryfinally(finallyf):

  u"returns a decorator that adds try/finally behavior with given no-argument call in the finally"

  print "tryfinally"

  def decorator(callable):

    print "decorator"

    def execute(*args, **kwargs):

      print "execute1"

      try: result = callable(*args, **kwargs)

      finally: finallyf()

      return result

    return execute

  return decorator



def usinglock(lock):

  u"returns a decorator whose argument will acquire the given lock while executing"

  print "usinglock"

  def decorator(function):

    print "decorator"

    body = tryfinally(lock.release)(function)

    def execute(*args, **kwargs):

      print "execute"

      lock.acquire()

      return body(*args, **kwargs)

    return execute

  return decorator



def synchronized(function):

  u"decorator; only one thread can enter the decorated function at a time; recursion is OK"

  print "synchronized"

  return usinglock(threading.RLock())(function)









@synchronized

def foo(*args):

  print "Only one thread can enter this function at a time"





if __name__=="__main__":

  foo(123)

版权声明:本文博主原创文章。博客,未经同意不得转载。

python装饰实现线程同步的更多相关文章

  1. Python并发编程-线程同步(线程安全)

    Python并发编程-线程同步(线程安全) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 线程同步,线程间协调,通过某种技术,让一个线程访问某些数据时,其它线程不能访问这些数据,直 ...

  2. A Basic Example of Threads Synchronization in Python, python中的线程同步示例

    http://zulko.github.io/blog/2013/09/19/a-basic-example-of-threads-synchronization-in-python/ We will ...

  3. Python多线程(2)——线程同步机制

    本文介绍Python中的线程同步对象,主要涉及 thread 和 threading 模块. threading 模块提供的线程同步原语包括:Lock.RLock.Condition.Event.Se ...

  4. python lock, semaphore, event实现线程同步

    lock 机制不管你是java, C#, 还是python都是常用的线程同步机制, 相比较C# 的锁机制, python的加锁显得比较简单, 直接调用threading 标准库的lock 就可以了. ...

  5. python线程同步原语--源码阅读

    前面两篇文章,写了python线程同步原语的基本应用.下面这篇文章主要是通过阅读源码来了解这几个类的内部原理和是怎么协同一起工作来实现python多线程的. 相关文章链接:python同步原语--线程 ...

  6. Python线程同步

    线程执行 join与setDaemon 子线程在主线程运行结束后,会继续执行完,如果给子线程设置为守护线程(setDaemon=True),主线程运行结束子线程即结束: 如果join()线程,那么主线 ...

  7. python笔记10-多线程之线程同步(锁lock)

    前言 关于吃火锅的场景,小伙伴并不陌生,吃火锅的时候a同学往锅里下鱼丸,b同学同时去吃掉鱼丸,有可能会导致吃到生的鱼丸. 为了避免这种情况,在下鱼丸的过程中,先锁定操作,让吃火锅的小伙伴停一会,等鱼丸 ...

  8. 孤荷凌寒自学python第四十三天python 的线程同步之Queue对象

     孤荷凌寒自学python第四十三天python的线程同步之Queue对象 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) Queue对象是直接操作队列池的对象,队列中可以存放多种对象,当然也 ...

  9. 孤荷凌寒自学python第四十一天python的线程同步之Event对象

     孤荷凌寒自学python第四十一天python的线程同步之Event对象 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 鉴于Lock锁与RLock锁均宣告没有完全完成同步文件操作的问题,于 ...

随机推荐

  1. php练习——打印半金字塔、金字塔、空心金字塔、菱形、空心菱形

    半金字塔 金字塔 空心金字塔 菱形     空心菱形

  2. mysql备份,还原命令

    mysql导出数据1.导出整个数据库mysqldump -u用户名 -p 数据库名 >备份文件2.导出一个表mysqldump -u用户名 -p databaseName tablename & ...

  3. thinkphp路径引用问题

    查看ThinkPHP\Library\Behavior\ContentReplaceBehavior.class文件,常量定义如下定义: '__ROOT__'      =>  __ROOT__ ...

  4. smali 语法基础

    dalvik字节码有两种类型,原始类型和引用类型.对象和数组是引用类型,其它都是原始类型. V  void Z  boolean B  byte S  short C  char I  int F   ...

  5. 各浏览器Cookie大小、个数限制

    一.浏览器允许每个域名所包含的cookie数: Microsoft指出InternetExplorer8增加cookie限制为每个域名50个,但IE7似乎也允许每个域名50个cookie. Firef ...

  6. hexdump——Linux系统的二进制文件查看工具

    hexdump是Linux下的一个二进制文件查看工具,可以将二进制文件转换为ASCII.10进制.16进制或8进制进行查看. 首先我们准备一个测试用的文件test,十六进制如下: 00 01 02 0 ...

  7. android小知识

    string 与 []byte 互转: public String BytesToString(byte[] data) { return new String(data); } public byt ...

  8. 【POJ2773】Happy 2006 欧几里德

    题目描述: 分析: 根据欧几里德,我们有gcd(b×t+a,b)=gcd(a,b) 则如果a与b互质,则b×t+a与b也一定互质,如果a与b不互质,则b×t+a与b也一定不互质. 所以与m互质的数对m ...

  9. js页面传参数时,参数值包含特殊字符的处理

    js页面传参数时,参数值包含特殊字符应该怎么处理,解决方法就是利用js的escape函数,这个函数在解决中文乱码等方面应用的比较广泛.推荐使用. 工作中遇到的小问题,一个页面中通过window.sho ...

  10. 【HDOJ】1908 Double Queue

    双端队列+二分. #include <cstdio> #define MAXN 1000005 typedef struct { int id; int p; } node_st; nod ...