Open krhougs opened 1 week ago
Thanks for the contribution!
You are correct that creating a new tokio runtime each time is a waste, that's actually fixed in master as of now
What's the advantage of a seperate runtime over the async variants of the existing functions?
Hi, @rscarson!
What's the advantage of a seperate runtime over the async variants of the existing functions?
Current Runtime
with sync API provides an easy way to run any JS async functions in sync programs, it blocks current thread on every operation on the deno runtime until finished or timed out. While it is quite simple and elegant when the program is tiny or the JS code doesn't play too much with IO, it brings limitations:
Runtime
will always panic if the caller function is already running on a tokio runtime (Worker
solves this but there are still problems)Worker
)To solve the problems above:
AsyncRuntime
provides non-blocking APIs that directly works within a tokio CurrentThread
runtime (JsRuntime
from deno_core is not thread-safe, it panics in tokio MultiThreaded
runtimes if you are trying to invoke any Rust closures(e.g. deno_fetch
) in JS codes)AsyncWorker
wraps the AsyncRuntime
with a dedicated thread and a dedicated tokio CurrentThread
runtime, and provides non-blocking APIs for complex applications running with an existing tokio MultiThreaded
runtime.Hi, @rscarson!
What's the advantage of a seperate runtime over the async variants of the existing functions?
Current
Runtime
with sync API provides an easy way to run any JS async functions in sync programs, it blocks current thread on every operation on the deno runtime until finished or timed out. While it is quite simple and elegant when the program is tiny or the JS code doesn't play too much with IO, it brings limitations:
- The
Runtime
will always panic if the caller function is already running on a tokio runtime (Worker
solves this but there are still problems)- On common circumstances, the whole program will block to wait for js calls if the calls take too long to finish(even with
Worker
)- It's impossible to exchange data and send events between rust code and current running js code
- Example 1: I'd like to run a JS function to provide background IPC
- Example 2: I'd like to run a JS function polling for external states and reporting the states to rust code
To solve the problems above:
AsyncRuntime
provides non-blocking APIs that directly works within a tokioCurrentThread
runtime (JsRuntime
from deno_core is not thread-safe, it panics in tokioMultiThreaded
runtimes if you are trying to invoke any Rust closures(e.g.deno_fetch
) in JS codes)AsyncWorker
wraps theAsyncRuntime
with a dedicated thread and a dedicated tokioCurrentThread
runtime, and provides non-blocking APIs for complex applications running with an existing tokioMultiThreaded
runtime.
I mean compared to the WIP Async API already present in master
The upcoming 0.5.0
release will have full async support on the existing runtime, and reuse its tokio runtime
Problems to solve
Runtime
creates a new tokio runtime on each operation, which is wasting resources.To-do
AsyncRuntime
: Deno wrapper that works within a single-threaded tokio runtimeAsyncWorker
: Worker that works with its own thread and tokio runtimeAsyncRuntime
to make js async function calls no more blocking current thread to allow concurrent js async function callsDefaultWorker
forAsyncWorker
trait to non-blocking async APIDefaultWorker
forSyncWorker
trait to work withAsyncRuntime
SyncRuntime
(Runtime
in current version)