Queue size appears to be the ENV size

115 views
Skip to first unread message

Jeremy Lewallen

unread,
Aug 28, 2016, 4:11:32 PM8/28/16
to python-simpy

I am just starting to work on an event simulation, and I am having some issues with monitoring the queue.

It appears that every time I check the queue, it is actually showing the Env.now. Any advice?


** BTW I am new to the group and SimPy.  If this isn't proper to post this in the group please let me know and I will remove it. ***



import simpy

num_of_machines = 2

env = simpy.Environment()
bcs = simpy.Resource(env, capacity=num_of_machines)

def monitor(resource):
     """This is our monitoring callback."""

     print('Queue size: %s' % len(resource.queue))

def process_client(env, name):

    with bcs.request() as req:
        yield req
        print('%s starting to charge at %s' % (name, env.now))
        yield env.timeout(90)
        print('%s ending charge at %s' % (name, env.now))
        monitor(bcs)



def setup(env):
    i = 0

    while True:
        i += 1
        yield env.timeout(1)

        env.process(process_client(env, ('Car %s' % i)))

env.process(setup(env))

env.run(until=300)

Results:

Car 1 starting to charge at 1
Car 2 starting to charge at 2
Car 1 ending charge at 91
Queue size: 88
Car 3 starting to charge at 91
Car 2 ending charge at 92
Queue size: 88
Car 4 starting to charge at 92
Car 3 ending charge at 181
Queue size: 176
Car 5 starting to charge at 181
Car 4 ending charge at 182
Queue size: 176
Car 6 starting to charge at 182
Car 5 ending charge at 271
Queue size: 264
Car 7 starting to charge at 271
Car 6 ending charge at 272
Queue size: 264
Car 8 starting to charge at 272

jpgr...@gmail.com

unread,
Aug 29, 2016, 12:49:21 PM8/29/16
to python-simpy
Hi Jeremy,

The key issue I'm seeing is in setup() where client processes are being launched at a rate of 1 per unit of time. This is leading to a huge number of these client processes contending for the `bcs` resource. This manifests as bcs' queue (aka put_queue), which has an item for each process waiting on the resource, growing quite large.

Since clients are being added at a rate of 1/1, but only being serviced at a rate of 2/90 (a much slower rate), the "Queue size" reported by monitor() will end up being pretty close to `env.now`, as you are observing.

I wonder if perhaps you intended for setup() to block on each client process? This would be accomplished by adding a yield:

yield env.process(process_client(env, 'Car %s' % i))

Alternatively, you may want to have some other mechanism to bound the number of in-flight client processes. I don't know enough about your simulation goals to be able to offer more concrete suggestions.

Hope this helps.

Pete
Reply all
Reply to author
Forward
0 new messages