python装饰实现线程同步
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装饰实现线程同步的更多相关文章
- Python并发编程-线程同步(线程安全)
Python并发编程-线程同步(线程安全) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 线程同步,线程间协调,通过某种技术,让一个线程访问某些数据时,其它线程不能访问这些数据,直 ...
- 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 ...
- Python多线程(2)——线程同步机制
本文介绍Python中的线程同步对象,主要涉及 thread 和 threading 模块. threading 模块提供的线程同步原语包括:Lock.RLock.Condition.Event.Se ...
- python lock, semaphore, event实现线程同步
lock 机制不管你是java, C#, 还是python都是常用的线程同步机制, 相比较C# 的锁机制, python的加锁显得比较简单, 直接调用threading 标准库的lock 就可以了. ...
- python线程同步原语--源码阅读
前面两篇文章,写了python线程同步原语的基本应用.下面这篇文章主要是通过阅读源码来了解这几个类的内部原理和是怎么协同一起工作来实现python多线程的. 相关文章链接:python同步原语--线程 ...
- Python线程同步
线程执行 join与setDaemon 子线程在主线程运行结束后,会继续执行完,如果给子线程设置为守护线程(setDaemon=True),主线程运行结束子线程即结束: 如果join()线程,那么主线 ...
- python笔记10-多线程之线程同步(锁lock)
前言 关于吃火锅的场景,小伙伴并不陌生,吃火锅的时候a同学往锅里下鱼丸,b同学同时去吃掉鱼丸,有可能会导致吃到生的鱼丸. 为了避免这种情况,在下鱼丸的过程中,先锁定操作,让吃火锅的小伙伴停一会,等鱼丸 ...
- 孤荷凌寒自学python第四十三天python 的线程同步之Queue对象
孤荷凌寒自学python第四十三天python的线程同步之Queue对象 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) Queue对象是直接操作队列池的对象,队列中可以存放多种对象,当然也 ...
- 孤荷凌寒自学python第四十一天python的线程同步之Event对象
孤荷凌寒自学python第四十一天python的线程同步之Event对象 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 鉴于Lock锁与RLock锁均宣告没有完全完成同步文件操作的问题,于 ...
随机推荐
- node 后台ajax文件(同时支持http、https)
var http = require("http"), Url = require("url"), querystring = require('queryst ...
- C#中标准Dispose模式的实现
http://www.cnblogs.com/luminji/archive/2011/03/29/1997812.html 需要明确一下C#程序(或者说.NET)中的资源.简单的说来,C#中的每一个 ...
- Centos6.4配置Fedora EPEL源附配置hop5.in源
查看系统版本 cat /etc/redhat-release 下载CentOS 版本所对应的EPEL 的版本 wget http://download.fedoraproject.org/pub/ep ...
- SecurityCRT输出日志重定向
使用CRT进行抓取log,因为工具本省缓冲区有限,导致,刷屏特别快,可能会错过一些log,可以对CRT的log进行增加输出源,或者说将输出到控制台的log再输出到本地文件中: 文件->点击(勾选 ...
- C#转义字符总结
转义字符 \·一种特殊的字符常量:·以反斜线"\"开头,后跟一个或几个字符.·具有特定的含义,不同于字符原有的意义,故称“转义”字符.·主要用来表示那些用一般字符不便于表示的控制代 ...
- IndexedDB
http://www.tfan.org/indexeddb/ http://fnvfox.appspot.com/thankyou.html http://www.tfan.org/wechat-on ...
- 如何解决 Java 安全问题?
如何解决 Java 安全问题,目前的应对策略都十分笨拙,往往适得其反.幸运的是,有一种新的方法可以将安全机制嵌入 Java 执行平台--或者更具体地说,嵌入 Java 虚拟机中,进而规避一些「Big ...
- Asterix and Obelix
uva10246:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&am ...
- 学习VI的强文,新工作需要呀
http://www.gentoo.org/doc/zh_cn/vi-guide.xml :set nu//用于给文本加行号的. :set nocompatible //启用 vi 兼容模式,一般是给 ...
- python的相对路径导入问题
用python做项目,如果项目大了,或者想更好的管理程序,总是要使用包.包解决了命名冲突的问题. 今天在使用python的相对路径导入的时候,遇到了不少的问题. 包导入情形: src/ __in ...