用with实现python的threading,新鲜啊
哈哈,2.5以后可用。自动加锁释放,如同操作文件打开关闭一样。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import threading
import logging
logging.basicConfig(level=logging.DEBUG,
format='(%(threadName)-10s)%(message)s', )
def threading_with(statement):
with statement:
logging.debug('%s acquired via with' %statement)
def threading_not_with(statement):
statement.acquire()
try:
logging.debug('%s acquired directly' % statement)
finally:
statement.release()
if __name__ == "__main__":
lock = threading.Lock()
rlock = threading.RLock()
condition = threading.Condition()
mutex = threading.Semaphore(1)
threading_synchronization_list = \
[lock, rlock, condition, mutex]
for statement in threading_synchronization_list :
t1 = threading.Thread(target=threading_with,
args=(statement,))
t2 = threading.Thread(target=threading_not_with,
args=(statement,))
t1.start()
t2.start()
t1.join()
t2.join()

用with实现python的threading,新鲜啊的更多相关文章
- python中threading的用法
摘自:http://blog.chinaunix.net/uid-27571599-id-3484048.html 以及:http://blog.chinaunix.net/uid-11131943- ...
- python中threading模块详解(一)
python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...
- python多线程threading.Lock锁用法实例
本文实例讲述了python多线程threading.Lock锁的用法实例,分享给大家供大家参考.具体分析如下: python的锁可以独立提取出来 mutex = threading.Lock() #锁 ...
- Python 线程(threading) 进程(multiprocessing)
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- Python 线程(threading)
Python 的thread模块是比较底层的模块,Python的threading模块是对thread做了一些包装,可以更加方便的 被使用; 1. 使用threading 模块 # 示例一: 单线程执 ...
- Python之threading多线程,多进程
1.threading模块是Python里面常用的线程模块,多线程处理任务对于提升效率非常重要,先说一下线程和进程的各种区别,如图 概括起来就是 IO密集型(不用CPU) 多线程计算密集型(用CPU) ...
- Python的threading和multiprocessing
Python的threading 基础用法, 通过 threading.Thread() 创建线程, 然后 start() 和 join() import time import threading ...
- python使用threading获取线程函数返回值的实现方法
python使用threading获取线程函数返回值的实现方法 这篇文章主要介绍了python使用threading获取线程函数返回值的实现方法,需要的朋友可以参考下 threading用于提供线程相 ...
- Python 之 threading
创建多线程常用的三种方法: 创建Thread的实例,传给它一个函数 创建Thread的实例,传给它一个可调用的类实例(不推荐) 派生Thread的子类,并创建子类的实例(推荐) 创建Thread的实例 ...
随机推荐
- Delphi结构体的扩展,可以自动初始化,反初始化,自定义拷贝函数.
转载:http://www.raysoftware.cn/?p=518&utm_source=tuicool 恭贺Delphi XE7诞生,Delphi XE7在编译器内部集成了我之前所实现的 ...
- BZOJ 2685: Sgu385 highlander
Sol 期望DP. \(f[i][j][k]\) 表示已经确定了 \(i\) 个点, 最大环大小为 \(j\) ,个数为 \(k\) 的方案数. 转移非常复杂,因为细节特别多. \(f[i][j][1 ...
- Python自动化之sqlalchemy关联查询
外键关联 from sqlalchemy import ForeignKey from sqlalchemy.orm import relationship class Address(Base): ...
- Arp攻击实战
hyddd原创,转载请说明出处. 现在网上很多关于ARP攻击的文章,但多数是描述其原理,少数会以某个工具作为例子展开说明,但感觉说的还是不够详细,今天写个文章以实战介绍一个叫"WinArpA ...
- Routing in ASP.NET Web API和配置文件的设定读取
Routing Tables In ASP.NET Web API, a controller is a class that handles HTTP requests. The public me ...
- ios 在中国地区,24小时时间格式 系统设定下 获得12小时制时间的方法
如题,在中国地区,24小时时间格式 系统设定下,如果单单使用 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 之后,无论用hh ...
- Greedy:Jessica's Reading Problem(POJ 3320)
Jessica's Reading Problem 题目大意:Jessica期末考试临时抱佛脚想读一本书把知识点掌握,但是知识点很多,而且很多都是重复的,她想读最少的连续的页数把知识点全部掌握(知识点 ...
- 20145213《Java程序设计》第二周学习总结
20145213<Java程序设计>第二周学习总结 教材学习内容总结 本周娄老师给的任务是学习教材的第三章--基础语法.其实我觉得还蛮轻松的,因为在翻开厚重的书本,一股熟悉的气息扑面而来, ...
- java课后作业
课后作业之字串加密: 设计思想: 1.输入要加密的英文子串str 2.定义num=str的字符串长度 3.将字符串转化为单个字符 4.每个字符+3,向后移3个 5.定义str1,将新得到的每个字符加到 ...
- C# 类中索引器的使用二
索引器(Indexer)是C#引入的一个新型的类成员,它使得类中的对象可以像数组那样方便.直观的被引用.索引器非常类似于属性,但索引器可以有参数列表,且只能作用在实例对象上,而不能在类上直接作用.定义 ...