I am experimenting with SimPy and I try to monitor the level of a Container resource.
For illustration purposes, please consider the simple example of a producer process that produces an item every 3 time steps. and puts them into the container, and a consumer process that takes a single item from the container at every time step. The initial container level is 3.
I implemented this as follows:
import simpy# define a "producer" that puts a new item in the container every 3 time unitsdef 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 unitdef 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 environmentenv = simpy.Environment()container = simpy.Container(env, init = 3)producer_process = env.process(producer(env, container))consumer_process = env.process(consumer(env, container))# run the simulationprint(f'Initial container level: {container.level}')env.run(until=7)This results in the following output:
Initial container level: 3Consumer at t = 0: Requesting item from container. Current container level (before taking item): 3Consumer at t = 0: Consumed item from container. New container level: 2Consumer at t = 2: Requesting item from container. Current container level (before taking item): 2Consumer at t = 2: Consumed item from container. New container level: 1Producer at t = 3: Produced item. Requesting to put it into container. Current container level (before putting): 1Producer at t = 3: Successfully put produced item into container. New container level: 2Consumer at t = 4: Requesting item from container. Current container level (before taking item): 2Consumer at t = 4: Consumed item from container. New container level: 1Producer at t = 6: Produced item. Requesting to put it into container. Current container level (before putting): 1Consumer at t = 6: Requesting item from container. Current container level (before taking item): 2Producer at t = 6: Successfully put produced item into container. New container level: 1Consumer at t = 6: Consumed item from container. New container level: 1I think this result is strange. For example, according to the output, the container level at time 6 increases from 1 to 2 even before the newly produced item is placed in the container.
My question is: How and when is the container level attribute updated in simpy and how can I obtain or create a correct log of all transient level changes?