• python_并发编程——锁


    多进程模拟买票~

    import time
    import json
    from multiprocessing import Process
    
    class Show(Process):    #
        def run(self):
            with open('ticket') as f:
                dic = json.load(f)
                print("余票:{}".format(dic['ticket']))
    
    class Buy_ticket(Process):  #
        def __init__(self,name):
            super().__init__()
            self.name = name
        def run(self):
            with open('ticket') as f:
                dic = json.load(f)
                time.sleep(0.1) #模拟网络延迟
            if dic['ticket'] > 0:
                dic['ticket'] -=1
                print('{}买到票了~~~'.format(self.name))
                time.sleep(0.1) #模拟网络延迟
                with open('ticket','w') as f:
                    json.dump(dic,f)
            else:
                print('{}没买到票!!!'.format(self.name))
    if __name__ == '__main__':
        for i in range(10):
            q1 = Show()
            q1.start()
        for i in range(10):
            i = str(i)
            q2 = Buy_ticket(i)
            q2.start()

    json文件:结果:余票为1却有两个人买到票了~。

    加上锁之后

    import time
    import json
    from multiprocessing import Process
    from multiprocessing import Lock
    
    class Show(Process):    #
        def run(self):
            with open('ticket') as f:
                dic = json.load(f)
                print("余票:{}".format(dic['ticket']))
    
    class Buy_ticket(Process):  #
        def __init__(self,name,lock):   #接收锁对象
            super().__init__()
            self.name = name
            self.lock = lock
        def run(self):
            self.lock.acquire()     #拿钥匙进门
            with open('ticket') as f:
                dic = json.load(f)
                time.sleep(0.1) #模拟网络延迟
            if dic['ticket'] > 0:
                dic['ticket'] -=1
                print('{}买到票了~~~'.format(self.name))
                time.sleep(0.1) #模拟网络延迟
                with open('ticket','w') as f:
                    json.dump(dic,f)
            else:
                print('{}没买到票!!!'.format(self.name))
            self.lock.release()     #还钥匙
    if __name__ == '__main__':
        for i in range(10):
            q1 = Show()
            q1.start()
        lock = Lock()   #实例化一个锁对象
        for i in range(10):
            i = str(i)
            q2 = Buy_ticket(i,lock) #将锁对象传入子进程
            q2.start()

    json文件:结果:就只有三个人买到了~~

    信息量:实现限定进程(大于1)数量访问代码。

    import time
    import random
    from multiprocessing import Process
    from multiprocessing import Semaphore
    
    class Ktv(Process):
        def __init__(self,name,sem):
            super().__init__()
            self.name = name
            self.sem = sem
    
        def run(self):
            self.sem.acquire()      #获取钥匙
            print('{}走进了KTV'.format(self.name))
            time.sleep(random.randint(10,20))  #随机暂停10-20秒
            print('{}走出了KTV'.format(self.name))
            self.sem.release()    #还钥匙
    
    if __name__ == '__main__':
        sem = Semaphore(4)  #实例化,实例化的同时设置信息量
        for i in range(20):
            name = str(i)   #将信息量的对象传递进子进程
            q = Ktv(name,sem)
            q.start()

    结果:开始进去4个后,只能出了一个再进去一个。

  • 相关阅读:
    Maven相关知识片
    fastjson使用
    Block pool ID needed, but service not yet registered with NN java.lang.Exception: trace 异常解决
    JVM之内存结构详解
    ContextCleaner ——Spark 应用程序的垃圾回收器
    重新认识Java 8的HashMap
    HDFS NameNode内存全景
    HDFS的NameNode与SecondaryNameNode的工作原理
    大数据——基础知识
    HDFS数据定期清理
  • 原文地址:https://www.cnblogs.com/wangdianchao/p/12044316.html
Copyright © 2020-2023  润新知