zoukankan      html  css  js  c++  java
  • Python并行编程(八):with语法

    1、基本概念

          当有两个相关的操作需要在一部分代码块前后分别执行的时候,可以使用with语法自动完成。同时,使用with语法可以在特定的地方分配和释放资源,因此,with语法也叫作"上下文管理器"。在threading模快中,所有带有acquire()方法和release()方法的对象都可以使用上下文管理器。主要用于代码块的收尾工作。

          也就是说,下面的对象可以使用with语法:

                Lock、RLock、Condition、Semaphore

    2、测试用例

    # 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()
  • 相关阅读:
    C# 从服务器下载文件
    不能使用联机NuGet 程序包
    NPOI之Excel——合并单元格、设置样式、输入公式
    jquery hover事件中 fadeIn和fadeOut 效果不能及时停止
    UVA 10519 !! Really Strange !!
    UVA 10359 Tiling
    UVA 10940 Throwing cards away II
    UVA 10079 Pizze Cutting
    UVA 763 Fibinary Numbers
    UVA 10229 Modular Fibonacci
  • 原文地址:https://www.cnblogs.com/dukuan/p/9798036.html
Copyright © 2011-2022 走看看