SimPy: странные значения атрибутов временного уровня ресурса ContainerPython

Программы на Python
Anonymous
SimPy: странные значения атрибутов временного уровня ресурса Container

Сообщение Anonymous »

Я экспериментирую с 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, и как я могу получить или создать правильный журнал всех изменений переходного уровня?

Подробнее здесь: https://stackoverflow.com/questions/782 ... r-resource

Вернуться в «Python»