#scheduler #luau #async-executor #thread

mlua-luau-scheduler

Luau-based async scheduler, using mlua and async-executor

6 releases

new 0.1.3 May 6, 2025
0.1.2 Apr 30, 2025
0.0.2 Mar 11, 2024
0.0.1 Feb 16, 2024

#388 in Asynchronous

Download history 10/week @ 2025-01-14 12/week @ 2025-01-21 11/week @ 2025-01-28 24/week @ 2025-02-04 10/week @ 2025-02-11 18/week @ 2025-02-18 20/week @ 2025-02-25 8/week @ 2025-03-04 28/week @ 2025-03-11 7/week @ 2025-03-18 11/week @ 2025-03-25 13/week @ 2025-04-01 7/week @ 2025-04-08 14/week @ 2025-04-15 172/week @ 2025-04-22 348/week @ 2025-04-29

542 downloads per month
Used in 7 crates

MPL-2.0 license

61KB
1K SLoC

mlua-luau-scheduler

An async scheduler for Luau, using mlua and built on top of async-executor.

This crate is runtime-agnostic and is compatible with any async runtime, including Tokio, smol, async-std, and others.
However, since many dependencies are shared with smol, depending on it over other runtimes may be preferred.

Example Usage

1. Import dependencies

use std::time::{Duration, Instant};
use std::io::ErrorKind;

use async_io::{block_on, Timer};
use async_fs::read_to_string;

use mlua::prelude::*;
use mlua_luau_scheduler::*;

2. Set up Lua environment

let lua = Lua::new();

lua.globals().set(
    "sleep",
    lua.create_async_function(|_, duration: f64| async move {
        let before = Instant::now();
        let after = Timer::after(Duration::from_secs_f64(duration)).await;
        Ok((after - before).as_secs_f64())
    })?,
)?;

lua.globals().set(
    "readFile",
    lua.create_async_function(|lua, path: String| async move {
        // Spawn background task that does not take up resources on the lua thread
        // Normally, futures in mlua can not be shared across threads, but this can
        let task = lua.spawn(async move {
            match read_to_string(path).await {
                Ok(s) => Ok(Some(s)),
                Err(e) if e.kind() == ErrorKind::NotFound => Ok(None),
                Err(e) => Err(e),
            }
        });
        task.await.into_lua_err()
    })?,
)?;

3. Set up scheduler, run threads

let sched = Scheduler::new(&lua)?;

// We can create multiple lua threads ...
let sleepThread = lua.load("sleep(0.1)");
let fileThread = lua.load("readFile(\"Cargo.toml\")");

// ... spawn them both onto the scheduler ...
sched.push_thread_front(sleepThread, ());
sched.push_thread_front(fileThread, ());

// ... and run until they finish
block_on(sched.run());

Dependencies

~4–10MB
~99K SLoC