/usr/share/doc/python-simpy3-doc/html/topical_guides/simpy_basics.html is in python-simpy3-doc 3.0.7+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SimPy basics — SimPy 3.0.7 documentation</title>
<link rel="stylesheet" href="../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '3.0.7',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="shortcut icon" href="../_static/favicon.ico"/>
<link rel="top" title="SimPy 3.0.7 documentation" href="../index.html" />
<link rel="up" title="Topical Guides" href="index.html" />
<link rel="next" title="Environments" href="environments.html" />
<link rel="prev" title="Topical Guides" href="index.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="environments.html" title="Environments"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="index.html" title="Topical Guides"
accesskey="P">previous</a> |</li>
<li><a href="../contents.html">SimPy 3.0.7 documentation</a> »</li>
<li><a href="index.html" accesskey="U">Topical Guides</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="simpy-basics">
<h1>SimPy basics<a class="headerlink" href="#simpy-basics" title="Permalink to this headline">¶</a></h1>
<p>This guide describes the basic concepts of SimPy: How does it work? What are
processes, events and the environment? What can I do with them?</p>
<div class="section" id="how-simpy-works">
<h2>How SimPy works<a class="headerlink" href="#how-simpy-works" title="Permalink to this headline">¶</a></h2>
<p>If you break SimPy down, it is just an asynchronous event dispatcher. You
generate events and schedule them at a given simulation time. Events are sorted
by priority, simulation time, and an increasing event id. An event also has
a list of callbacks, which are executed when the event is triggered and
processed by the event loop. Events may also have a return value.</p>
<p>The components involved in this are the <a class="reference internal" href="../api_reference/simpy.core.html#simpy.core.Environment" title="simpy.core.Environment"><tt class="xref py py-class docutils literal"><span class="pre">Environment</span></tt></a>,
<a class="reference internal" href="../api_reference/simpy.events.html#module-simpy.events" title="simpy.events"><tt class="xref py py-mod docutils literal"><span class="pre">events</span></tt></a> and the process functions that you write.</p>
<p>Process functions implement your simulation model, that is, they define the
behavior of your simulation. They are plain Python generator functions that
yield instances of <a class="reference internal" href="../api_reference/simpy.events.html#simpy.events.Event" title="simpy.events.Event"><tt class="xref py py-class docutils literal"><span class="pre">Event</span></tt></a>.</p>
<p>The environment stores these events in its event list and keeps track of the
current simulation time.</p>
<p>If a process function yields an event, SimPy adds the process to the event’s
callbacks and suspends the process until the event is triggered and processed.
When a process waiting for an event is resumed, it will also receive the
event’s value.</p>
<p>Here is a very simple example that illustrates all this; the code is more
verbose than it needs to be to make things extra clear. You find a compact
version of it at the end of this section:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">simpy</span>
<span class="go">>>></span>
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">example</span><span class="p">(</span><span class="n">env</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">event</span> <span class="o">=</span> <span class="n">simpy</span><span class="o">.</span><span class="n">events</span><span class="o">.</span><span class="n">Timeout</span><span class="p">(</span><span class="n">env</span><span class="p">,</span> <span class="n">delay</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">value</span> <span class="o">=</span> <span class="k">yield</span> <span class="n">event</span>
<span class="gp">... </span> <span class="k">print</span><span class="p">(</span><span class="s">'now=</span><span class="si">%d</span><span class="s">, value=</span><span class="si">%d</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">env</span><span class="o">.</span><span class="n">now</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
<span class="go">>>></span>
<span class="gp">>>> </span><span class="n">env</span> <span class="o">=</span> <span class="n">simpy</span><span class="o">.</span><span class="n">Environment</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">example_gen</span> <span class="o">=</span> <span class="n">example</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">p</span> <span class="o">=</span> <span class="n">simpy</span><span class="o">.</span><span class="n">events</span><span class="o">.</span><span class="n">Process</span><span class="p">(</span><span class="n">env</span><span class="p">,</span> <span class="n">example_gen</span><span class="p">)</span>
<span class="go">>>></span>
<span class="gp">>>> </span><span class="n">env</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
<span class="go">now=1, value=42</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">example()</span></tt> process function above first creates
a <a class="reference internal" href="../api_reference/simpy.events.html#simpy.events.Timeout" title="simpy.events.Timeout"><tt class="xref py py-class docutils literal"><span class="pre">Timeout</span></tt></a> event. It passes the environment, a delay, and
a value to it. The Timeout schedules itself at <tt class="docutils literal"><span class="pre">now</span> <span class="pre">+</span> <span class="pre">delay</span></tt> (that’s why the
environment is required); other event types usually schedule themselves at the
current simulation time.</p>
<p>The process function then yields the event and thus gets suspended. It is
resumed, when SimPy processes the Timeout event. The process function also
receives the event’s value (42) – this is, however, optional, so <tt class="docutils literal"><span class="pre">yield</span>
<span class="pre">event</span></tt> would have been okay if the you were not interested in the value or if
the event had no value at all.</p>
<p>Finally, the process function prints the current simulation time (that is
accessible via the environment’s <a class="reference internal" href="../api_reference/simpy.core.html#simpy.core.Environment.now" title="simpy.core.Environment.now"><tt class="xref py py-attr docutils literal"><span class="pre">now</span></tt></a> attribute)
and the Timeout’s value.</p>
<p>If all required process functions are defined, you can instantiate all objects
for your simulation. In most cases, you start by creating an instance of
<a class="reference internal" href="../api_reference/simpy.core.html#simpy.core.Environment" title="simpy.core.Environment"><tt class="xref py py-class docutils literal"><span class="pre">Environment</span></tt></a>, because you’ll need to pass it around a lot
when creating everything else.</p>
<p>Starting a process function involves two things:</p>
<ol class="arabic simple">
<li>You have to call the process function to create a generator object. (This
will not execute any code of that function yet. Please read <a class="reference external" href="http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained/231855#231855">The Python
yield keyword explained</a>,
to understand why this is the case.)</li>
<li>You then create an instance of <a class="reference internal" href="../api_reference/simpy.events.html#simpy.events.Process" title="simpy.events.Process"><tt class="xref py py-class docutils literal"><span class="pre">Process</span></tt></a> and pass the
environment and the generator object to it. This will schedule an
<a class="reference internal" href="../api_reference/simpy.events.html#simpy.events.Initialize" title="simpy.events.Initialize"><tt class="xref py py-class docutils literal"><span class="pre">Initialize</span></tt></a> event at the current simulation time which
starts the execution of the process function. The process instance is also
an event that is triggered when the process function returns. The
<a class="reference internal" href="events.html"><em>guide to events</em></a> explains why this is handy.</li>
</ol>
<p>Finally, you can start SimPy’s event loop. By default, it will run as long as
there are events in the event list, but you can also let it stop earlier by
providing an <tt class="docutils literal"><span class="pre">until</span></tt> argument (see <a class="reference internal" href="environments.html#simulation-control"><em>Simulation control</em></a>).</p>
<p>The following guides describe the environment and its interactions with events
and process functions in more detail.</p>
</div>
<div class="section" id="best-practice-version-of-the-example-above">
<h2>“Best practice” version of the example above<a class="headerlink" href="#best-practice-version-of-the-example-above" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">simpy</span>
<span class="go">>>></span>
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">example</span><span class="p">(</span><span class="n">env</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">value</span> <span class="o">=</span> <span class="k">yield</span> <span class="n">env</span><span class="o">.</span><span class="n">timeout</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">print</span><span class="p">(</span><span class="s">'now=</span><span class="si">%d</span><span class="s">, value=</span><span class="si">%d</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">env</span><span class="o">.</span><span class="n">now</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
<span class="go">>>></span>
<span class="gp">>>> </span><span class="n">env</span> <span class="o">=</span> <span class="n">simpy</span><span class="o">.</span><span class="n">Environment</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">p</span> <span class="o">=</span> <span class="n">env</span><span class="o">.</span><span class="n">process</span><span class="p">(</span><span class="n">example</span><span class="p">(</span><span class="n">env</span><span class="p">))</span>
<span class="gp">>>> </span><span class="n">env</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
<span class="go">now=1, value=42</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="../contents.html">
<img class="logo" src="../_static/simpy-logo-small.png" alt="Logo"/>
</a></p>
<h3><a href="../contents.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">SimPy basics</a><ul>
<li><a class="reference internal" href="#how-simpy-works">How SimPy works</a></li>
<li><a class="reference internal" href="#best-practice-version-of-the-example-above">“Best practice” version of the example above</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="index.html"
title="previous chapter">Topical Guides</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="environments.html"
title="next chapter">Environments</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/topical_guides/simpy_basics.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="environments.html" title="Environments"
>next</a> |</li>
<li class="right" >
<a href="index.html" title="Topical Guides"
>previous</a> |</li>
<li><a href="../contents.html">SimPy 3.0.7 documentation</a> »</li>
<li><a href="index.html" >Topical Guides</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2002–2015, Team SimPy.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
</div>
</body>
</html>
|