用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的实例 ...
随机推荐
- cacti错误
cacti 错误:CMDPHP: Poller[0] ERROR 解决方案: 找到错误表 desc 表名: 修复此表 mysqlcheck -A -o -r -p -u用户名
- super用法和继承中的构造方法
当new出来一个对象的时候, this是只想对象本身. 在存在继承关系时, 在子类中用super表示引用父类中的东西. 子类的构造过程必须调用父类的构造方法. 子类中包含父类,所以子类中一定要先调用 ...
- 【Eclipse】在Eclipse工具中自定义类注释
直接上图:这个公司基本都已经定制好了,自己写demo的时候可以适当定制自己的注释 package com.zlg.controller; zlg : 此处输入zlg(对应模版的名称) 然后ALT+/ ...
- js中val()和value的区别
val()是在有jQuery插件的时候才能用,value是在没有jQuery插件的情况下也能用.val()是jQuery根据原生JS里面的value写出来的函数 $(this).val(); 有四个重 ...
- mybatis Result Maps collection already contains value for com.ebways.dictionary.dao.impl.PtInfoDaoImpl.beanMap
java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...
- 如何让您的php也支持pthreads多线程
我们常常会碰到这样一种情况,开发环境在windows下开发,而生产环境确是linux.windows下能正常运行,上传到linux后却无法好好地玩耍了.然后开始了一轮尼玛式的疯狂的查找原因,最后发现是 ...
- cxGrid 增加序号 (非数据库绑定模式) (测试通过)
cxGrid 增加序号 (非数据库绑定模式) ----------------------------------- 1. 选在 adoQuery 控件 , 鼠标右键菜单中 选择 Fields Edi ...
- MySQL 利用SQL线程对Binlog操作
背景: 对于MySQL的binlog的查看都是用其自带的工具mysqlbinlog进行操作的,其实还有另一个方法来操作binlog,就是Replication中的SQL线程去操作binlog,其实bi ...
- ffmpeg-20160522-git-bin
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...
- SAP SMARTFORMS-基于内存方式在report程序中传递数据到smartforms显示
一.准备工作 1.新建include程序 1> include程序名字:ZDD_INC_0104 2> ZDD_INC_0104 程序中的内容为 2.使用T-CODE :SE11新建两个 ...