Update.
This commit is contained in:
+90
-18
@@ -85,6 +85,7 @@
|
||||
<li><a href="../tutorials/database-app.html">Database App</a></li>
|
||||
<li><a href="../tutorials/template-rendering.html">Templates</a></li>
|
||||
<li><a href="../tutorials/cli-tool.html">CLI Tool</a></li>
|
||||
<li><a href="../tutorials/web-server.html">Web Server</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="section">
|
||||
@@ -99,6 +100,19 @@
|
||||
<li><a href="../howto/error-handling.html">Error Handling</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="section">
|
||||
<span class="section-title">Contributing</span>
|
||||
<ul>
|
||||
<li><a href="../contributing/index.html">Overview</a></li>
|
||||
<li><a href="../contributing/module-overview.html">Module Architecture</a></li>
|
||||
<li><a href="../contributing/pure-wren-module.html">Pure-Wren Modules</a></li>
|
||||
<li><a href="../contributing/c-backed-module.html">C-Backed Modules</a></li>
|
||||
<li><a href="../contributing/foreign-classes.html">Foreign Classes</a></li>
|
||||
<li><a href="../contributing/async-patterns.html">Async Patterns</a></li>
|
||||
<li><a href="../contributing/testing.html">Writing Tests</a></li>
|
||||
<li><a href="../contributing/documentation.html">Documentation</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
</aside>
|
||||
<main class="content">
|
||||
@@ -115,7 +129,17 @@
|
||||
|
||||
<p>The <code>scheduler</code> module manages the async event loop and fiber scheduling. It is the foundation for all async operations in Wren-CLI.</p>
|
||||
|
||||
<pre><code>import "scheduler" for Scheduler</code></pre>
|
||||
<pre><code>import "scheduler" for Scheduler, Future</code></pre>
|
||||
|
||||
<div class="admonition note">
|
||||
<div class="admonition-title">Quick Reference</div>
|
||||
<p>The scheduler module enables async programming with <code>async</code> and <code>await</code>:</p>
|
||||
<ul>
|
||||
<li><code>async { code }</code> — Create an async function</li>
|
||||
<li><code>await fn()</code> — Call async function and wait for result</li>
|
||||
<li><code>fn.call()</code> — Start async function, return Future (for concurrent execution)</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<h2>Scheduler Class</h2>
|
||||
|
||||
@@ -241,6 +265,48 @@ System.print(await square(8)) // 64</code></pre>
|
||||
<p>The <code>fn(args)</code> syntax only works directly after <code>await</code>. Outside of <code>await</code>, use <code>fn.call(args)</code> to invoke async functions.</p>
|
||||
</div>
|
||||
|
||||
<h2>Future Class</h2>
|
||||
|
||||
<div class="class-header">
|
||||
<h3>Future</h3>
|
||||
<p>Represents a pending async result</p>
|
||||
</div>
|
||||
|
||||
<p>A <code>Future</code> is returned when you call an async function with <code>.call()</code> instead of using <code>await</code> directly. This enables concurrent execution by starting multiple operations without waiting.</p>
|
||||
|
||||
<h3>Creating Futures</h3>
|
||||
|
||||
<pre><code>import "scheduler" for Scheduler, Future
|
||||
|
||||
var double = async { |x| x * 2 }
|
||||
|
||||
// Direct call with await - waits immediately
|
||||
var result = await double(21)
|
||||
|
||||
// Using .call() returns a Future - does not wait
|
||||
var future = double.call(21)
|
||||
|
||||
// Later, await the future to get the result
|
||||
var result = await future</code></pre>
|
||||
|
||||
<h3>Concurrent Execution Pattern</h3>
|
||||
|
||||
<pre><code>import "scheduler" for Scheduler, Future
|
||||
import "web" for Client
|
||||
|
||||
var fetch = async { |url| Client.get(url) }
|
||||
|
||||
// Start all requests at once (returns Futures)
|
||||
var f1 = fetch.call("https://api.example.com/users")
|
||||
var f2 = fetch.call("https://api.example.com/posts")
|
||||
var f3 = fetch.call("https://api.example.com/comments")
|
||||
|
||||
// All three requests are now running concurrently
|
||||
// Wait for each result
|
||||
var users = await f1
|
||||
var posts = await f2
|
||||
var comments = await f3</code></pre>
|
||||
|
||||
<h2>How Async Works</h2>
|
||||
|
||||
<p>Wren-CLI uses an event loop (libuv) for non-blocking I/O. Here is how async operations work:</p>
|
||||
@@ -266,28 +332,34 @@ class Timer {
|
||||
|
||||
<h2>Examples</h2>
|
||||
|
||||
<h3>Sequential vs Parallel</h3>
|
||||
<p>Operations are sequential by default:</p>
|
||||
<pre><code>import "http" for Http
|
||||
<h3>Sequential vs Concurrent Execution</h3>
|
||||
<p>Using <code>await fn(args)</code> executes operations sequentially:</p>
|
||||
<pre><code>import "scheduler" for Scheduler, Future
|
||||
import "web" for Client
|
||||
|
||||
// These run one after another (sequential)
|
||||
var r1 = Http.get("https://api.example.com/1")
|
||||
var r2 = Http.get("https://api.example.com/2")
|
||||
var r3 = Http.get("https://api.example.com/3")</code></pre>
|
||||
var fetch = async { |url| Client.get(url) }
|
||||
|
||||
<p>For parallel operations, use multiple fibers:</p>
|
||||
<pre><code>import "http" for Http
|
||||
// Sequential: each request waits for the previous one
|
||||
var r1 = await fetch("https://api.example.com/1")
|
||||
var r2 = await fetch("https://api.example.com/2")
|
||||
var r3 = await fetch("https://api.example.com/3")</code></pre>
|
||||
|
||||
var fibers = [
|
||||
Fiber.new { Http.get("https://api.example.com/1") },
|
||||
Fiber.new { Http.get("https://api.example.com/2") },
|
||||
Fiber.new { Http.get("https://api.example.com/3") }
|
||||
]
|
||||
<p>Using <code>.call()</code> enables concurrent execution:</p>
|
||||
<pre><code>import "scheduler" for Scheduler, Future
|
||||
import "web" for Client
|
||||
|
||||
// Start all fibers
|
||||
for (f in fibers) f.call()
|
||||
var fetch = async { |url| Client.get(url) }
|
||||
|
||||
// Results are already available when fibers complete</code></pre>
|
||||
// Start all requests at once (concurrent)
|
||||
var f1 = fetch.call("https://api.example.com/1")
|
||||
var f2 = fetch.call("https://api.example.com/2")
|
||||
var f3 = fetch.call("https://api.example.com/3")
|
||||
|
||||
// All three requests are running in parallel
|
||||
// Now wait for results
|
||||
var r1 = await f1
|
||||
var r2 = await f2
|
||||
var r3 = await f3</code></pre>
|
||||
|
||||
<div class="admonition note">
|
||||
<div class="admonition-title">Note</div>
|
||||
|
||||
Reference in New Issue
Block a user