• Python中基本同步原语的使用


    Python中的同步原语--锁

    from atexit import register
    from random import randrange
    from threading import Thread, Lock, current_thread
    from time import ctime, sleep
    
    
    class CleanOutputSet(set):
        def __str__(self):
            return ', '.join(x for x in self)
    
    
    lock = Lock()
    loops = (randrange(2, 5) for x in range(randrange(3, 7)))
    remaining = CleanOutputSet()
    
    
    def loop(nsec):
        myname = current_thread().name
    
        lock.acquire()
        remaining.add(myname)
        print('{} started {}'.format(ctime(), myname))
        lock.release()
    
        sleep(nsec)
    
        lock.acquire()
        remaining.remove(myname)
        print('{} Completed {} ({} secs)'.format(ctime(), myname, nsec))
        print('    (remaining: {})'.format(remaining or None))
        lock.release()
    
    
    def main():
        print('starting at:', ctime())
        for pause in loops:
            Thread(target=loop, args=(pause,)).start()
    
    
    @register
    def _atexit():
        print('all done at:', ctime())
    
    
    if __name__ == '__main__':
        main()
    

    优化版使用上下文管理器:

    ...
    
    def loop(nsec):
        myname = current_thread().name
    
        with lock:
            remaining.add(myname)
            print('{} started {}'.format(ctime(), myname))
    
        sleep(nsec)
    
        with lock:
            remaining.remove(myname)
            print('{} Completed {} ({} secs)'.format(ctime(), myname, nsec))
            print('    (remaining: {})'.format(remaining or None))
    
    ...
    

    Python中的同步原语--信号量

    from atexit import register
    from random import randrange
    from threading import BoundedSemaphore, Lock, Thread
    from time import ctime, sleep
    
    
    lock = Lock()
    MAX = 5
    candytray = BoundedSemaphore(MAX)
    
    
    def refill():
        with lock:
            print('Refilling candy...')
            try:
                candytray.release()
            except ValueError:
                print('full, skipping')
            else:
                print('ok')
    
    
    def buy():
        with lock:
            print('Buying candy...')
            if candytray.acquire(False):
                print('OK')
            else:
                print('empty, skipping')
    
    
    def producer(loops):
        for x in range(loops):
            refill()
            sleep(randrange(3))
    
    
    def consumer(loops):
        for x in range(loops):
            buy()
            sleep(randrange(3))
    
    
    def _main():
        print('starting at:{}'.format(ctime()))
        nloops = randrange(2, 6)
        print('THE CANDY MACHINE (full with {} bars)!'.format(MAX))
        Thread(target=consumer, args=(randrange(nloops, nloops+MAX+2),)).start()
        Thread(target=producer, args=(randrange(nloops, nloops+MAX+2),)).start()
    
    
    @register
    def _atexit():
        print('all DONE at:', ctime())
    
    
    if __name__ == '__main__':
        _main()
    
  • 相关阅读:
    CSS3笔记!
    Charles与Jmeter结合编写接口测试
    Charles测试点集锦
    pom文件报错关于maven-compiler-plugin:3.1
    Jmeter获取数据库值并作为参数请求(转载)
    mysql基础操作语言
    JMeter之Ramp-up Period(in seconds)说明
    Jmeter查看QPS和响应时间随着时间的变化曲线(转载)
    Charles篡改后台数据
    接口测试之——Charles抓包及常见问题解决(转载自https://www.jianshu.com/p/831c0114179f)
  • 原文地址:https://www.cnblogs.com/BxScope/p/11000570.html
Copyright © 2020-2023  润新知