python不知道错在哪里怎么办 您所在的位置:网站首页 如何知道代码错在哪里 python不知道错在哪里怎么办

python不知道错在哪里怎么办

2024-06-16 13:02| 来源: 网络整理| 查看: 265

[Python] 纯文本查看 复制代码from multiprocessing import Process, Semaphore, Lock, Queue

import time

from random import random

buffer = Queue(10)

empty = Semaphore(2)

full = Semaphore(0)

lock = Lock()

class Consumer(Process):

def run(self):

global buffer, empty, full, lock

while True:

full.acquire()

lock.acquire()

print(f'Consumer get {buffer.get()}')

time.sleep(1)

lock.release()

empty.release()

class Producer(Process):

def run(self):

global buffer, empty, full, lock

while True:

empty.acquire()

lock.acquire()

num = random()

print(f'Producer append an {num}')

buffer.put(num)

time.sleep(1)

lock.release()

full.release()

if __name__ == '__main__':

p = Producer()

c = Consumer()

p.daemon = True

c.daemon = True

p.start()

c.start()

p.join()

c.join()

print('Ended!')



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有