wren-lang / wren

The Wren Programming Language. Wren is a small, fast, class-based concurrent scripting language.
http://wren.io
MIT License
6.91k stars 552 forks source link

Fiber Queue? #369

Closed ghost closed 8 years ago

ghost commented 8 years ago

So, I'm still new to the "wren" scene but after some playing around I thought it might be useful to have a messaging queue in the Fiber objects. EDIT: Also, I'm new to actually using github for contributions and therefore, have no idea how to label issues. Sorry.

iwillspeak commented 8 years ago

Welcome to the Wren community, and welcome to GitHub! Thanks for your contribution. No need to be sorry, it's always welcome.

The idea of having a dedicated queue on the Fiber sounds interesting. I have a few questions about how you imagine it might work.

When you say that Fibers should have a message queue exactly how would you imagine working with one? Would the queue be bi-directional, or would you only be able to communicate in one direction? Could you post some pseudo-Wren to show how you think working with it might look?

munificent commented 8 years ago

Welcome! I don't think submitters are able to label issues, so don't worry about that.

I'll echo @iwillspeak's sentiment: can you give us some more details about what you have in mind?

In general, we try to keep the core pretty minimal, so it might make sense to implement what you're talking about in user code on top of the primitive fiber operations, but it depends on how you want it to behave.

ghost commented 8 years ago

Logged in on my phone right now, so the best I can say is that while playing around with the language I found myself wanting to send multiple arguments to a yielding fiber. Sure, a list worked fine but I really would have preferred to not have had to lug a list all over the place. I have been known for being to micro-optimizing and OCD about readability though.

munificent commented 8 years ago

I found myself wanting to send multiple arguments to a yielding fiber.

You can always just do multiple yields:

var sender = Fiber.new {
  Fiber.yield(1)
  Fiber.yield(2)
  Fiber.yield(3)
}

var receiver = Fiber.new {
  System.print(sender.call()) // 1.
  System.print(sender.call()) // 2.
  System.print(sender.call()) // 3.
}
ghost commented 8 years ago

I was thinking more along the lines of

var dataHandler = Fiber.new
{
//Do something with all the collected data that's stored in a queue.
}

dataHandler.addMessage(getData(1))
dataHandler.addMessage(getData(2))
dataHandler.addMessage(getData(3))

//Do some other stuff

dataHandler.addMessage(getData(3))
dataHandler.addMessage(getData(2))
dataHandler.addMessage(getData(1))

//Then, once all our data has been accumulated, execute the fiber.
dataHandler.call()
munificent commented 8 years ago

In that case, you can always do the queue yourself:

class DataHandler {
  construct new(handler) {
    _handler = handler
    _messages = []
  }

  addMessage(message) {
    _messages.add(message)
  }

  call() {
    Fiber.new(_handler).call()
  }
}

And use it like this:

var dataHandler = DataHandler.new {|messages|
  System.print("Got %(messages)")
}

dataHandler.addMessage(getData(1))
dataHandler.addMessage(getData(2))
dataHandler.addMessage(getData(3))

// Do some other stuff

dataHandler.addMessage(getData(3))
dataHandler.addMessage(getData(2))
dataHandler.addMessage(getData(1))

// Then, once all our data has been accumulated, execute the fiber.
dataHandler.call()
ghost commented 8 years ago

Wow, that's great! Sorry for my, eh, stupidity. Guess Wren is still too new to me. :) Thinking about embedding it in a game I'm building (since my own attempt at a scripting language (geared specifically at said game) fell through when I got to the runtime ;)).

Banane9 commented 8 years ago

@Icecream-burglar In case you're making it in C#

cough shameless self-plug cough

ghost commented 8 years ago

Actually, it is c#. And I hate to break it to you and all, but I've already found your ironwren AND favorite/followed it. That was a redundant self-plug (for me at-least) :D.

Banane9 commented 8 years ago

Hah, well then!

Feel free to get in contact if you need help! :)

munificent commented 8 years ago

Guess Wren is still too new to me. :)

It's new to everyone! 😁