哈哈,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,新鲜啊的更多相关文章

  1. python中threading的用法

    摘自:http://blog.chinaunix.net/uid-27571599-id-3484048.html 以及:http://blog.chinaunix.net/uid-11131943- ...

  2. python中threading模块详解(一)

    python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...

  3. python多线程threading.Lock锁用法实例

    本文实例讲述了python多线程threading.Lock锁的用法实例,分享给大家供大家参考.具体分析如下: python的锁可以独立提取出来 mutex = threading.Lock() #锁 ...

  4. Python 线程(threading) 进程(multiprocessing)

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  5. Python 线程(threading)

    Python 的thread模块是比较底层的模块,Python的threading模块是对thread做了一些包装,可以更加方便的 被使用; 1. 使用threading 模块 # 示例一: 单线程执 ...

  6. Python之threading多线程,多进程

    1.threading模块是Python里面常用的线程模块,多线程处理任务对于提升效率非常重要,先说一下线程和进程的各种区别,如图 概括起来就是 IO密集型(不用CPU) 多线程计算密集型(用CPU) ...

  7. Python的threading和multiprocessing

    Python的threading 基础用法, 通过 threading.Thread() 创建线程, 然后 start() 和 join() import time import threading ...

  8. python使用threading获取线程函数返回值的实现方法

    python使用threading获取线程函数返回值的实现方法 这篇文章主要介绍了python使用threading获取线程函数返回值的实现方法,需要的朋友可以参考下 threading用于提供线程相 ...

  9. Python 之 threading

    创建多线程常用的三种方法: 创建Thread的实例,传给它一个函数 创建Thread的实例,传给它一个可调用的类实例(不推荐) 派生Thread的子类,并创建子类的实例(推荐) 创建Thread的实例 ...

随机推荐

  1. div 加滚动条

    div 加滚动条的方法: <div style="position:absolute; height:400px; overflow:auto"></div> ...

  2. fzu2172 字符串dp

    F - 巡了南山我巡北山 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  3. Google java style

    这里主要是记录关于java style的笔记. 1,Google的. 博客链接(中文):http://www.cnblogs.com/lanxuezaipiao/p/3534447.html. 官方链 ...

  4. 2017/1/7 学习笔记 jar包,maven

    ① 关于tar,jar,war文件 tar是通用的另一种打包格式,为了部署到服务器时方便. jar是java app server识别的java部署格式,其实是Zip文件,只是内部的文件有规范. wa ...

  5. LINQ查询字符串判断是否大写

    #region Linq to 字符串char.IsUpper意思是判断是否大写            //string strDemo = "HelloWord!";       ...

  6. struts2 复杂参数封装

    1.1.1    Struts2中封装复杂类型的数据: 封装到List集合: 页面: 商品名称:<input type="text" name="products[ ...

  7. javax.imageio.IIOException: Can't create cache file!

    javax.imageio.IIOException: Can't create cache file! at javax.imageio.ImageIO.createImageInputStream ...

  8. 自动编译和提交脚本(结合svn和visual studio)

    @echo 更新代码开始----------------- TortoiseProc.exe /command:update /path:"D:\work\mmsanguo_publish_ ...

  9. 请不要用SECONDS_BEHIND_MASTER来衡量MYSQL主备的延迟时间【转】

    本文来自:http://www.woqutech.com/?p=1116 MySQL 本身通过 show slave status 提供了 Seconds_Behind_Master ,用于衡量主备之 ...

  10. 1.SpringMVC的简介和环境搭建

    SpringMVC的简介: SpringMVC 和 Struts一样是一个MVC框架,和Spring无缝连接,和struts2类似, Spring MVC属于SpringFrameWork的后续产品, ...