This pattern is based upon the Multi-Threaded Synchronization pattern.
An independent thread is used to run the scripting
An MPSC channel (or any other appropriate synchronization primitive) is used to send function call arguments, packaged as a message, to another Rust thread that will perform the actual async calls.
Results are marshaled back to the
Enginethread via another MPSC channel.
See the Multi-Threaded Synchronization pattern.
Spawn another thread (the
workerthread) that can perform the actual async calls in Rust. This thread may actually be the main thread of the program.
Create a pair of MPSC channels (named
replybelow) for full-duplex communications between the two threads.
Register async API function to the
Enginewith a closure that captures the MPSC end-points.
If there are more than one async function, the receive end-point on the
replychannel can simply be cloned. The send end-point on the
commandchannel can be wrapped in an
Arc<Mutex<Channel>>for shared access.
In the async function, the name of the function and call arguments are serialized into JSON (or any appropriate message format) and sent to
commandchannel, where they’ll be removed by the
workerthread and the appropriate async function called.
Engineblocks on the function call, waiting for a reply message on the
When the async function call complete on the
workerthread, the result is sent back to the
Enginethread via the
After the result is obtained from the
Enginereturns it as the return value of the function call, ending the block and continuing evaluation.