<pclass="last">You are reading an old version of the Evennia documentation. <ahref="https://www.evennia.com/docs/latest/index.html">The latest version is here</a></p>.
<h1>Async Process<aclass="headerlink"href="#async-process"title="Permalink to this headline">¶</a></h1>
<divclass="admonition important">
<pclass="admonition-title">Important</p>
<p>This is considered an advanced topic.</p>
</div>
<sectionid="synchronous-versus-asynchronous">
<h2>Synchronous versus Asynchronous<aclass="headerlink"href="#synchronous-versus-asynchronous"title="Permalink to this headline">¶</a></h2>
<asideclass="sidebar">
<p>This is also explored in the <aclass="reference internal"href="../Howtos/Howto-Command-Duration.html"><spanclass="doc std std-doc">Command Duration Tutorial</span></a>.</p>
</aside>
<p>Most program code operates <em>synchronously</em>. This means that each statement in your code gets processed and finishes before the next can begin. This makes for easy-to-understand code. It is also a <em>requirement</em> in many cases - a subsequent piece of code often depend on something calculated or defined in a previous statement.</p>
<p>Consider this piece of code in a traditional Python program:</p>
<p>When run, this will print <codeclass="docutils literal notranslate"><spanclass="pre">"before</span><spanclass="pre">call</span><spanclass="pre">..."</span></code>, after which the <codeclass="docutils literal notranslate"><spanclass="pre">long_running_function</span></code> gets to work
for however long time. Only once that is done, the system prints <codeclass="docutils literal notranslate"><spanclass="pre">"after</span><spanclass="pre">call</span><spanclass="pre">..."</span></code>. Easy and logical to follow. Most of Evennia work in this way and often it’s important that commands get
executed in the same strict order they were coded.</p>
<p>Evennia, via Twisted, is a single-process multi-user server. In simple terms this means that it swiftly switches between dealing with player input so quickly that each player feels like they do things at the same time. This is a clever illusion however: If one user, say, runs a command containing that <codeclass="docutils literal notranslate"><spanclass="pre">long_running_function</span></code>, <em>all</em> other players are effectively forced to wait until it finishes.</p>
<p>Now, it should be said that on a modern computer system this is rarely an issue. Very few commands run so long that other users notice it. And as mentioned, most of the time you <em>want</em> to enforce all commands to occur in strict sequence.</p>
</section>
<sectionid="utils-delay">
<h2><codeclass="docutils literal notranslate"><spanclass="pre">utils.delay</span></code><aclass="headerlink"href="#utils-delay"title="Permalink to this headline">¶</a></h2>
<asideclass="sidebar">
<pclass="sidebar-title">delay() vs time.sleep()</p>
<p>This is equivalent to something like <codeclass="docutils literal notranslate"><spanclass="pre">time.sleep()</span></code> except <codeclass="docutils literal notranslate"><spanclass="pre">delay</span></code> is asynchronous while <codeclass="docutils literal notranslate"><spanclass="pre">sleep</span></code> would lock the entire server for the duration of the sleep.</p>
</aside>
<p>The <codeclass="docutils literal notranslate"><spanclass="pre">delay</span></code> function is a much simpler sibling to <codeclass="docutils literal notranslate"><spanclass="pre">run_async</span></code>. It is in fact just a way to delay the execution of a command until a future time.</p>
<p>This will delay the execution of the callback for 10 seconds. Provide <codeclass="docutils literal notranslate"><spanclass="pre">persistent=True</span></code> to make the delay survive a server <codeclass="docutils literal notranslate"><spanclass="pre">reload</span></code>. While waiting, you can input commands normally.</p>
<p>You can also try the following snippet just see how it works:</p>
<p>Wait 10 seconds and ‘Test!’ should be echoed back to you.</p>
</section>
<sectionid="utils-interactive-decorator">
<h2><codeclass="docutils literal notranslate"><spanclass="pre">@utils.interactive</span></code> decorator<aclass="headerlink"href="#utils-interactive-decorator"title="Permalink to this headline">¶</a></h2>
<p>The <codeclass="docutils literal notranslate"><spanclass="pre">@interactive</span></code> [decorator](<aclass="reference external"href="https://realpython.com/primer-on-python-">https://realpython.com/primer-on-python-</a> decorators/) makes any function or method possible to ‘pause’ and/or await player input in an interactive way.</p>
<spanclass="n">caller</span><spanclass="o">.</span><spanclass="n">msg</span><spanclass="p">(</span><spanclass="s2">"Getting ready to wait ..."</span><spanclass="p">)</span>
<spanclass="n">caller</span><spanclass="o">.</span><spanclass="n">msg</span><spanclass="p">(</span><spanclass="s2">"Now 5 seconds have passed."</span><spanclass="p">)</span>
<spanclass="n">response</span><spanclass="o">=</span><spanclass="k">yield</span><spanclass="p">(</span><spanclass="s2">"Do you want to wait another 5 secs?"</span><spanclass="p">)</span>
<p>The <codeclass="docutils literal notranslate"><spanclass="pre">@interactive</span></code> decorator gives the function the ability to pause. The use of <codeclass="docutils literal notranslate"><spanclass="pre">yield(seconds)</span></code> will do just that - it will asynchronously pause for the number of seconds given before continuing. This is technically equivalent to using <codeclass="docutils literal notranslate"><spanclass="pre">call_async</span></code> with a callback that continues after 5 secs. But the code with <codeclass="docutils literal notranslate"><spanclass="pre">@interactive</span></code> is a little easier to follow.</p>
<p>Within the <codeclass="docutils literal notranslate"><spanclass="pre">@interactive</span></code> function, the <codeclass="docutils literal notranslate"><spanclass="pre">response</span><spanclass="pre">=</span><spanclass="pre">yield("question")</span></code> question allows you to ask the user for input. You can then process the input, just like you would if you used the Python <codeclass="docutils literal notranslate"><spanclass="pre">input</span></code> function.</p>
<p>All of this makes the <codeclass="docutils literal notranslate"><spanclass="pre">@interactive</span></code> decorator very useful. But it comes with a few caveats.</p>
<ul>
<li><p>The decorated function/method/callable must have an argument named exactly <codeclass="docutils literal notranslate"><spanclass="pre">caller</span></code>. Evennia will look for an argument with this name and treat it as the source of input.</p></li>
<li><p>Decorating a function this way turns it turns it into a Python <aclass="reference external"href="https://wiki.python.org/moin/Generators">generator</a>. This means</p>
<ulclass="simple">
<li><p>You can’t use <codeclass="docutils literal notranslate"><spanclass="pre">return</span><spanclass="pre"><value></span></code> from a generator (just an empty <codeclass="docutils literal notranslate"><spanclass="pre">return</span></code> works). To return a value from a function/method you have decorated with <codeclass="docutils literal notranslate"><spanclass="pre">@interactive</span></code>, you must instead use a special Twisted function <codeclass="docutils literal notranslate"><spanclass="pre">twisted.internet.defer.returnValue</span></code>. Evennia also makes this function conveniently available from <codeclass="docutils literal notranslate"><spanclass="pre">evennia.utils</span></code>:</p></li>
<h2><codeclass="docutils literal notranslate"><spanclass="pre">utils.run_async</span></code><aclass="headerlink"href="#utils-run-async"title="Permalink to this headline">¶</a></h2>
<divclass="admonition warning">
<pclass="admonition-title">Warning</p>
<p>Unless you have a very clear purpose in mind, you are unlikely to get an expected result from <codeclass="docutils literal notranslate"><spanclass="pre">run_async</span></code>. Notably, it will still run your long-running function <em>in the same thread</em> as the rest of the server. So while it does run async, a very heavy and CPU-heavy operation will still block the server. So don’t consider this as a way to offload heavy operations without affecting the rest of the server.</p>
</div>
<p>When you don’t care in which order the command actually completes, you can run it <em>asynchronously</em>. This makes use of the <codeclass="docutils literal notranslate"><spanclass="pre">run_async()</span></code> function in <codeclass="docutils literal notranslate"><spanclass="pre">src/utils/utils.py</span></code>:</p>
<p>Where <codeclass="docutils literal notranslate"><spanclass="pre">function</span></code> will be called asynchronously with <codeclass="docutils literal notranslate"><spanclass="pre">*args</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">**kwargs</span></code>. Example:</p>
<p>Now, when running this you will find that the program will not wait around for <codeclass="docutils literal notranslate"><spanclass="pre">long_running_function</span></code> to finish. In fact you will see <codeclass="docutils literal notranslate"><spanclass="pre">"before</span><spanclass="pre">call</span><spanclass="pre">..."</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">"after</span><spanclass="pre">call</span><spanclass="pre">..."</span></code> printed out right away. The long-running function will run in the background and you (and other users) can go on as normal.</p>
<p>A complication with using asynchronous calls is what to do with the result from that call. What if
<codeclass="docutils literal notranslate"><spanclass="pre">long_running_function</span></code> returns a value that you need? It makes no real sense to put any lines of
code after the call to try to deal with the result from <codeclass="docutils literal notranslate"><spanclass="pre">long_running_function</span></code> above - as we saw
the <codeclass="docutils literal notranslate"><spanclass="pre">"after</span><spanclass="pre">call</span><spanclass="pre">..."</span></code> got printed long before <codeclass="docutils literal notranslate"><spanclass="pre">long_running_function</span></code> was finished, making that
line quite pointless for processing any data from the function. Instead one has to use <em>callbacks</em>.</p>
<p><codeclass="docutils literal notranslate"><spanclass="pre">utils.run_async</span></code> takes reserved kwargs that won’t be passed into the long-running function:</p>
<ul>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">at_return(r)</span></code> (the <em>callback</em>) is called when the asynchronous function (<codeclass="docutils literal notranslate"><spanclass="pre">long_running_function</span></code>
above) finishes successfully. The argument <codeclass="docutils literal notranslate"><spanclass="pre">r</span></code> will then be the return value of that function (or
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">at_return_kwargs</span></code> - an optional dictionary that will be fed as keyword arguments to the <codeclass="docutils literal notranslate"><spanclass="pre">at_return</span></code> callback.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">at_err(e)</span></code> (the <em>errback</em>) is called if the asynchronous function fails and raises an exception.
This exception is passed to the errback wrapped in a <em>Failure</em> object <codeclass="docutils literal notranslate"><spanclass="pre">e</span></code>. If you do not supply an
errback of your own, Evennia will automatically add one that silently writes errors to the evennia
log. An example of an errback is found below:</p></li>
<spanclass="nb">print</span><spanclass="p">(</span><spanclass="s2">"There was an error:"</span><spanclass="p">,</span><spanclass="nb">str</span><spanclass="p">(</span><spanclass="n">e</span><spanclass="p">))</span>
</pre></div>
</div>
<ulclass="simple">
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">at_err_kwargs</span></code> - an optional dictionary that will be fed as keyword arguments to the <codeclass="docutils literal notranslate"><spanclass="pre">at_err</span></code>
errback.</p></li>
</ul>
<p>An example of making an asynchronous call from inside a <aclass="reference internal"href="../Components/Commands.html"><spanclass="doc std std-doc">Command</span></a> definition:</p>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">caller</span><spanclass="o">.</span><spanclass="n">msg</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"The final value is </span><spanclass="si">{</span><spanclass="n">r</span><spanclass="si">}</span><spanclass="s2">"</span><spanclass="p">)</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">caller</span><spanclass="o">.</span><spanclass="n">msg</span><spanclass="p">(</span><spanclass="sa">f</span><spanclass="s2">"There was an error: </span><spanclass="si">{</span><spanclass="n">e</span><spanclass="si">}</span><spanclass="s2">"</span><spanclass="p">)</span>
<spanclass="c1"># do the async call, setting all callbacks</span>
<p>That’s it - from here on we can forget about <codeclass="docutils literal notranslate"><spanclass="pre">long_running_function</span></code> and go on with what else need to be done. <em>Whenever</em> it finishes, the <codeclass="docutils literal notranslate"><spanclass="pre">at_return_function</span></code> function will be called and the final value will pop up for us to see. If not we will see an error message.</p>
<blockquote>
<div><p>Technically, <codeclass="docutils literal notranslate"><spanclass="pre">run_async</span></code> is just a very thin and simplified wrapper around a <aclass="reference external"href="https://twistedmatrix.com/documents/9.0.0/core/howto/defer.html">Twisted Deferred</a> object; the wrapper sets up a default errback also if none is supplied. If you know what you are doing there is nothing stopping you from bypassing the utility function, building a more sophisticated callback chain after your own liking.</p>
<pclass="last">You are reading an old version of the Evennia documentation. <ahref="https://www.evennia.com/docs/latest/index.html">The latest version is here</a></p>.