Nim: coroutine and channel support like go.

Created on 1 Jan 2020  Â·  5Comments  Â·  Source: nim-lang/Nim

coroutine support like go.

If Nim is compiled to C, will it be difficult to support coroutine ??

Waiting on author Stdlib

Most helpful comment

See also https://github.com/treeform/greenlet for a Nim coroutine implementation based upon cgreenlet.

All 5 comments

There is co-routine support via import coro https://nim-lang.org/docs/coro.html

Though I think what you are looking for is asyncdispatch for async/await: https://nim-lang.org/docs/asyncdispatch.html
And channels in Nim are for parallelism: https://nim-lang.org/docs/channels.html.
Asyncdispatch futures should be enough for synchronizing/communicating between async tasks.

Also can you detail use-cases, even in pseudo-code so that we know:

  • what you have in mind
  • if maybe we don't already have support for that
  • if it can be implemented as a library or if it needs compiler support

There is co-routine support via import coro https://nim-lang.org/docs/coro.html

Though I think what you are looking for is asyncdispatch for async/await: https://nim-lang.org/docs/asyncdispatch.html
And channels: https://nim-lang.org/docs/channels.html

Also can you detail use-cases, even in pseudo-code so that we know:

  • what you have in mind
  • if maybe we don't already have support for that
  • if it can be implemented as a library or if it needs compiler support

We need lightweight thread support to deal with high concurrency when we implement web services. Equivalent to threads

  • if choice asyncdispatch futures like rust(async_std,stand lib support the Future+asyn+await,and other project impl the Future async,just like a call back):
#[async_std::main]
async fn main() {
    let id = task::current().id();
    println!("{:?}", id);
    let task = task::spawn(async {
        let id = task::current().id();
        println!("{:?}", id);
        task::sleep(Duration::from_millis(1000)).await;
    });
    println!("waiting for the task");
    let res = task.await;
    println!("task ended with result {:?}", res);
}
  • if choice coroutine like go(go have the channel, but not support the coroutine ID, so fi Nim impl the coroutine must be have coroutine ID! ):
for i := 0; i < ncpu; i++ {
        go func() {
            for {
                select {
                case <-done:
                    return
                case ch <- Must(NewUUID()):
                }
            }
        }()
    }

There is co-routine support via import coro https://nim-lang.org/docs/coro.html

Though I think what you are looking for is asyncdispatch for async/await: https://nim-lang.org/docs/asyncdispatch.html
And channels in Nim are for parallelism: https://nim-lang.org/docs/channels.html.
Asyncdispatch futures should be enough for synchronizing/communicating between async tasks.

Also can you detail use-cases, even in pseudo-code so that we know:

  • what you have in mind
  • if maybe we don't already have support for that
  • if it can be implemented as a library or if it needs compiler support

It co-routine very difficult to use. The best thing is that Nim officially supports async await。

Of course, Nim can also provide future interfaces for other projects to implement。just like rust lang done

I think theres lot of misunderstanding about naming and terminology here.
Nim has async/await. You can implement your own Types that use Futures.
Jester/HTTPBeast use threads for implementing web services and stuff.

If you want to use Nim with Rust like syntax you can create a Nim module that mimic its look&feel,
kinda like https://github.com/Yardanico/nimpylib#nimpylib mimics Python using metaprogramming.

See also https://github.com/treeform/greenlet for a Nim coroutine implementation based upon cgreenlet.

Was this page helpful?
0 / 5 - 0 ratings