Newbie Alert: Discrete Event Simulator

Bryan Morris & Mary Gauvreau bryan.mary at cyberus.ca
Thu Oct 28 21:08:48 EDT 1999


Hi:

After lurking on this newsgroup for quite a few months... I think I need
some help.

Backgrounder:
I've been doing some quick 'n dirty scripts with Python mostly just to learn
it.  I was looking
for that 'big' project to really sink my teeth into this language.   Anyway,
a couple of weeks
ago someone posted to the newsgroup a question if anyone had written a
Discrete
Event Simulator (DES) module in Python.  Since I've done a couple of DES
(both event driven and
logical process) in C++ I thought that this would be an ideal "real" first
project.  (I thought about
just extending my C++ DES libraries, but decided to try a pure-Python
approach as a learning exercise).

Brief Design Discussion:

I decided to use an event-driven model instead of using the threads module
(again, as a learning
exercise).  So I have a Scheduler object that handles the scheduling for all
Event objects passed to it.
Each Event object has a simulation time that it must be scheduled at and a
recipient Event_Handler
object.  The Scheduler's event list is simply a list sorted in increasing
simulation time. The Scheduler grabs
the next Event object off the list, sets the global simulation time to that
event's time then calls the Event_Handler
object's Process_Event function.

Another object of note is what I call an Object_Manager.  It holds a
dictionary with every Event_Handler and
the key is the Event_Handler's unique name.  This is used when scheduling
events.  So in a simulation with
two Event_Handlers named "Ping" and "Pong", the "Ping" event handler needs
to send a message to "Pong"
with:
    # Create an event with event_data, and send it to "Pong"
    # NOTE: Here myEvent holds the event_data and the instance of the
Event_Handler called "Pong".
    myEvent = Event( event_data, Object_Manager.Get_Event_Handler("Pong"))
    # Schedule the event to be sent in the next cycle.
    Scheduler.schedule_in( myEvent, nextSimulationCycle )

When an Event_Handler is created it's __init__ function passes it's name and
it's "self" to the Object_Manager
e.g.,
def __init__(self):
    """ blah """
    Object_Manager.Register(name, self)

If you're still with me... here's the
Problem:
The simulation seems to run fine, except after the Scheduler handles a few
events I get a Traceback
error saying the Scheduler can't find the proper Event_Handler instance.

What I've tried:
When I step through the debugger and the Object_Manager stores the name and
object instance address
(e.g., "Pong instance at AAAA").  The first event to be scheduled for "Pong"
tries to get the associated
object instance from the Object_Manager but instead it gets back something
else (e.g.,
"Pong instance at BBBB").  Which causes the traceback when I call this
instance's Process_Event function.

My questions:
a) When I Register an object instance and store it in the dictionary, am I
guaranteed that that object instance
will not move?  He says with only a "dangerous" (i.e., little) understanding
of what the garbage collection does.
As you can imagine, this is kind of critical to my design as it stands.
b) If not, any other ideas?
c) Have I been vague enough, or do you need more info?

I don't really want to send the code for open view until at least I bang my
head against the wall a few more times...

lurking-newbie-that-is-finally-using-Python-thanks-you-much'ly
Bryan








More information about the Python-list mailing list