richardhundt / shine

A Shiny Lua Dialect
Other
231 stars 18 forks source link

Provide examples of using coroutines with Nyanga #28

Closed romix closed 10 years ago

romix commented 10 years ago

Nyanga's project page mentions generators. Is it the same thing as coroutines? How it can be used if you want to play with coroutines a-la Lua? In Lua you have yield, resume, create for coroutines. What do you use in Nyanga instead? How do you schedule them, etc? How it can be used e.g. for async/non-blocking IO? It looks like the std library provides something, but it is not described or mentioned anywhere.

Some examples on the project page and/or in the tests directory would be nice.

richardhundt commented 10 years ago

On 2/4/14 10:45 AM, romix wrote:

Nyanga's project page mentions generators. Is it the same thing as coroutines?

Generators are plain wrapped coroutines:

function* gen(x)
   for i=1, x do
      yield i
   end
end

ten_times = gen(10)
print(ten_times()) -- prints 1
print(ten_times()) -- prints 2
print(ten_times()) -- prints 3
... etc

You can also use them as short function syntax:

gen = *(x) =>
   ...
end

Or as methods:

class Foo
   *gen()
      ...
   end
end

How it can be used if you want to play with coroutines a-la Lua?

All coroutines get a metatable (see the Fiber class in lib/system.nga).

In Lua you have yield, resume, create for coroutines. What do you use in Nyanga instead? How do you schedule them, etc? How it can be used e.g. for async/non-blocking IO? It looks like the std library provides something, but it is not described or mentioned anywhere.

You can create a coroutine any way you like and the call ready() on it to add it to the scheduler. The yield statement then detects if it was called from the main thread, if so it switches into the scheduler, otherwise it just does coroutine.yield(). After the coroutine has yielded, then it needs to be put back into the queue. Have a look at the sleep implementation for a callback based pattern for doing this.

Some examples on the project page and/or in the tests directory would be nice.

I know, but this is exactly what I'm currently working on, so it's all still in flux. This is the last major piece and then I'll get around to fixing the documentation. Right now I'm pulling out the system poll stuff and replacing it with ØMQ's zmq_poll because it's cross platform (kqueue, epoll, poll, etc.). So the scheduler is getting a rewrite. In the meantime I've added test/fiber.nga so that you can play with the old (current) implementation.

— Reply to this email directly or view it on GitHub https://github.com/richardhundt/nyanga/issues/28.

romix commented 10 years ago

Richard, thanks for the explanations.

BTW, I get this now: nyanga: ./lib/system.nga:31: 'queue' used but not defined

richardhundt commented 10 years ago

On 2/4/14 11:12 AM, romix wrote:

Richard, thanks for the explanations.

BTW, I get this now: nyanga: ./lib/system.nga:31: 'queue' used but not defined

Yeah, forgot to push a commit to the parser. Should work now. I've also added test/tcp_server.nga which shows async I/O with fibers.

Note: probably leaks memory, and has other bugs. As I said, this is currently being hacked on heavily.

— Reply to this email directly or view it on GitHub https://github.com/richardhundt/nyanga/issues/28#issuecomment-34045858.