SimPy:Container资源奇怪的瞬态级别属性值

问题描述 投票:0回答:1

我正在尝试使用 SimPy,并尝试监控容器资源的级别。

出于说明目的,请考虑“生产者流程”的简单示例,该流程每 3 个时间步生产一个项目。并将它们放入容器中,以及一个在每个时间步骤从容器中获取单个项目的“消费者进程”。初始容器级别为 3。 我的实现如下: import simpy # define a "producer" that puts a new item in the container every 3 time units def producer(env, container): while(True): yield env.timeout(3) print(f'Producer at t = {env.now}: Produced item. ' f'Requesting to put it into container. ' f'Current container level (before putting): {container.level}') yield container.put(1) print(f'Producer at t = {env.now}: Successfully put produced item into container. ' f'New container level: {container.level}') # define a "consumer" that attempts to take one item from the container at every time unit def consumer(env, container): while(True): print(f'Consumer at t = {env.now}: Requesting item from container. ' f'Current container level (before taking item): {container.level}') yield container.get(1) print(f'Consumer at t = {env.now}: Consumed item from container. ' f'New container level: {container.level}') yield env.timeout(2) # setup the simpy environment env = simpy.Environment() container = simpy.Container(env, init = 3) producer_process = env.process(producer(env, container)) consumer_process = env.process(consumer(env, container)) # run the simulation print(f'Initial container level: {container.level}') env.run(until=7)

这会产生以下输出:

Initial container level: 3
Consumer at t = 0: Requesting item from container. Current container level (before taking item): 3
Consumer at t = 0: Consumed item from container. New container level: 2
Consumer at t = 2: Requesting item from container. Current container level (before taking item): 2
Consumer at t = 2: Consumed item from container. New container level: 1
Producer at t = 3: Produced item. Requesting to put it into container. Current container level (before putting): 1
Producer at t = 3: Successfully put produced item into container. New container level: 2
Consumer at t = 4: Requesting item from container. Current container level (before taking item): 2
Consumer at t = 4: Consumed item from container. New container level: 1
Producer at t = 6: Produced item. Requesting to put it into container. Current container level (before putting): 1
Consumer at t = 6: Requesting item from container. Current container level (before taking item): 2
Producer at t = 6: Successfully put produced item into container. New container level: 1
Consumer at t = 6: Consumed item from container. New container level: 1

我觉得这个结果很奇怪。例如,根据输出,即使在新生产的物品放入容器之前,时间 6 的容器级别也从 1 增加到 2。

我的问题是:在 simpy 中如何以及何时更新容器级别属性以及如何获取或创建所有瞬态级别更改的正确日志?

请记住,每次过程执行yield时,它都会放弃对线程的控制,从而允许另一个过程运行。 最终,屈服过程将恢复并完成。

python simpy
1个回答
0
投票

然后生产者重新启动并进行最终打印,然后结束 消费者开始备份并完成。

也不要指望接下来运行的内容有任何顺序。 计划在时间 x 可以随机运行的顺序。

关键是您的程序可能不会从头到尾运行,它可以在一定时间休息一下,在休息期间可以运行另一个程序。

© www.soinside.com 2019 - 2024. All rights reserved.