Thread Module
Import using: use thread. Provides process-based threads with message passing.
thread.spawn(scriptPath)
Spawns a worker process from a script file. Returns a Thread handle.
use thread
# worker.dz contents:
# use thread
# let msg = thread.receive()
# thread.reply("Hello from worker! You said: " + msg)
let w = thread.spawn("worker.dz")
w.send("Hello Worker")
let reply = w.receive()
print("Reply: " + reply)
w.close()
thread.spawnCode(code)
Spawns a worker from inline code string without needing a separate file.
use thread
let code = '
use thread
let msg = thread.receive()
thread.reply("Echo: " + msg)
'
let w = thread.spawnCode(code)
w.send("test123")
print("Reply: " + w.receive())
w.close()
Send & Receive Methods (Main VM)
t.send(message)- Send string message to worker.t.sendJson(data)- Serialize data to JSON and send.t.receive()- Receive message from worker (blocking).t.receiveJson()- Receive and deserialize JSON (blocking).t.receiveTimeout(seconds)- Receive with timeout (Null on timeout).t.tryReceive()- Non-blocking receive (Null if no message).
use thread
let w = thread.spawn("worker.dz")
w.send("hello")
print("Recv: " + w.receive())
let tryMsg = w.tryReceive()
print("Try: " + str(tryMsg))
w.close()
Lifecycle Methods
t.isAlive()- Check if worker is alive.t.isClosed()- Check if handle is closed.t.stop()- Request graceful stop.t.join()- Block until worker exits.t.joinTimeout(seconds)- Block up to seconds.t.exitCode()- Get worker exit code (Null if running).t.close()- Close handle.t.closeTimeout(seconds)- Close with timeout.
use thread
let w = thread.spawn("worker.dz")
print("Alive: " + str(w.isAlive()))
w.stop()
print("Join: " + str(w.join()))
print("Exit: " + str(w.exitCode()))
print("Closed: " + str(w.isClosed()))
w.close()
JSON Message Passing
Send and receive structured data using JSON serialization.
use thread
# Worker uses thread.receiveJson() / thread.replyJson()
let w = thread.spawn("worker.dz")
let task = {"action": "ping", "value": 42}
w.sendJson(task)
let reply = w.receiveJson()
print("Action: " + reply["original"]["action"])
w.close()
Worker-Side Functions
thread.reply(message)/thread.replyJson(data)- Send to main VM.thread.receive()/thread.receiveJson()- Receive from main VM (blocking).thread.tryReceive()/thread.tryReceiveJson()- Non-blocking receive.thread.receiveTimeout(seconds)/thread.receiveJsonTimeout(seconds)- Receive with timeout.thread.isWorker()- Check if running in worker context.thread.yield()- Cooperative yield.
# Inside worker.dz:
# use thread
# let running = True
# while running
# let msg = thread.receive()
# if msg == "STOP"
# running = False
# else
# thread.reply("GOT: " + msg)
# end
# end
Thread Pool
Create a pool of worker processes for parallel task execution.
thread.pool(workerScript, poolSize)- Create thread pool.thread.inlinePool(code, poolSize)- Create pool from inline code.pool.execute(task)- Execute task, returns Future.pool.broadcast(message)- Send message to all workers.pool.size()- Number of workers in pool.pool.pending()- Number of pending tasks.pool.close()- Shut down all workers.
use thread
let p = thread.pool("worker.dz", 3)
print("Pool size: " + str(p.size()))
let f1 = p.execute("task_one")
let f2 = p.execute("task_two")
print("R1: " + str(f1.wait()))
print("R2: " + str(f2.wait()))
print("Pending: " + str(p.pending()))
p.close()
Future (Pool Task Result)
future.wait()- Block until result ready.future.waitTimeout(seconds)- Wait with timeout.future.done()- Check if result is available.
use thread
let p = thread.pool("worker.dz", 2)
let f1 = p.execute("alpha")
let f2 = p.execute("beta")
while not f1.done()
thread.yield()
end
print("F1: " + str(f1.wait()))
print("F2: " + str(f2.waitTimeout(1)))
p.close()
Broadcast
Send a message to all workers in a pool simultaneously.
use thread
let p = thread.pool("worker.dz", 2)
let ok = p.broadcast("PING")
print("Broadcast sent: " + str(ok))
p.close()