Need help with coio-rs?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

zonyitoo
439 Stars 29 Forks Other 237 Commits 11 Opened issues

Description

Coroutine I/O for Rust

Services available

!
?

Need anything else?

Contributors list

# 108,466
Shell
Rust
rust-la...
dns-cli...
173 commits
# 3,308
C++
imagema...
coffees...
flash
32 commits
# 4,794
Rust
webasse...
asmjs
webwork...
4 commits
# 1,764
C
doh
zig
dns-ove...
2 commits
# 19,756
spotify
multi-t...
shardin...
SQL
1 commit
# 34,265
Go
jsonnet
prometh...
gRPC
1 commit
# 555,206
Go
Erlang
1 commit
# 17,057
Rust
Nim
jinja2
templat...
1 commit

Coroutine I/O

Build Status Build status License

Coroutine scheduling with work-stealing algorithm.

WARN: Possibly crash because of TLS inline, check https://github.com/zonyitoo/coio-rs/issues/56 for more detail!

Feature

  • Non-blocking I/O
  • Work-stealing coroutine scheduling
  • Asynchronous computing APIs

Usage

Note: You must use Nightly Rust to build this Project.

[dependencies.coio]
git = "https://github.com/zonyitoo/coio-rs.git"

Basic Coroutines

extern crate coio;

use coio::Scheduler;

fn main() { Scheduler::new() .run(|| { for _ in 0..10 { println!("Heil Hydra"); Scheduler::sched(); // Yields the current coroutine } }) .unwrap(); }

TCP Echo Server

extern crate coio;

use std::io::{Read, Write};

use coio::net::TcpListener; use coio::{spawn, Scheduler};

fn main() { // Spawn a coroutine for accepting new connections Scheduler::new().with_workers(4).run(move|| { let acceptor = TcpListener::bind("127.0.0.1:8080").unwrap(); println!("Waiting for connection ...");

    for stream in acceptor.incoming() {
        let (mut stream, addr) = stream.unwrap();

        println!("Got connection from {:?}", addr);

        // Spawn a new coroutine to handle the connection
        spawn(move|| {
            let mut buf = [0; 1024];

            loop {
                match stream.read(&mut buf) {
                    Ok(0) => {
                        println!("EOF");
                        break;
                    },
                    Ok(len) => {
                        println!("Read {} bytes, echo back", len);
                        stream.write_all(&buf[0..len]).unwrap();
                    },
                    Err(err) => {
                        println!("Error occurs: {:?}", err);
                        break;
                    }
                }
            }

            println!("Client closed");
        });
    }
}).unwrap();

}

Exit the main function

Will cause all pending coroutines to be killed.

extern crate coio;

use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; use std::time::Duration;

use coio::Scheduler;

fn main() { let counter = Arc::new(AtomicUsize::new(0)); let cloned_counter = counter.clone();

let result = Scheduler::new().run(move|| {
    // Spawn a new coroutine
    Scheduler::spawn(move|| {
        struct Guard(Arc<atomicusize>);

        impl Drop for Guard {
            fn drop(&amp;mut self) {
                self.0.store(1, Ordering::SeqCst);
            }
        }

        // If the _guard is dropped, it will store 1 to the counter
        let _guard = Guard(cloned_counter);

        coio::sleep(Duration::from_secs(10));
        println!("Not going to run this line");
    });

    // Exit right now, which will cause the coroutine to be destroyed.
    panic!("Exit right now!!");
});

// The coroutine's stack is unwound properly
assert!(result.is_err() &amp;&amp; counter.load(Ordering::SeqCst) == 1);

}

Basic Benchmarks

See benchmarks for more details.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.