Hooch
Overview
Hooch is a custom async runtime written in Rust. The runtime serves as a lightweight, experimental alternative to existing asynchronous frameworks, allowing for a deeper understanding of async runtime internals.
Features
- Custom Executor and Reactor: Implements basic building blocks for async task scheduling and polling.
- Minimal Dependencies: Lightweight and tailored specifically to async task handling.
Usage
Below are examples demonstrating basic use cases for Hooch's async runtime.
Example 1: Spawning and Awaiting a Task with JoinHandle
In this example, we create a task within the runtime and use a JoinHandle to await its completion and retrieve the result. The JoinHandle allows you to asynchronously wait for the result of a spawned task, making it useful for concurrent operations.
use RuntimeBuilder;
Example 2: Using Bounded Channels for Communication between Threads
This example demonstrates using a bounded channel to send data between threads. Here, we create a sender thread that transmits a value through a bounded channel, and a receiver in the async runtime awaits and receives it.
use ;
use ;
Time
Sleep functionality is included with hooch.
use Duration;
use ;
Hooch macros
#[hooch_main] is a proc_macro_attribute that conveniently wraps your main function in the runtime. The only argument is workers, which defines the number of workers used. If the argument is not present, the default number of workers from the RuntimeBuilder will be used.
Examples
Using RuntimeBuilder default number of workers
use hooch_main;
async
Using 4 workers
use hooch_main;
async