Multiple channel listeners #46
Replies: 1 comment
-
In theory that's how the Channel class is supposed to work, but it's an experimental feature, so I don't really recommend using it. There are bits of the Channel implementation that I don't like, for example, it copies the message payload 4 times if I recall. The concept is modeled after goroutines and channels in Golang, but I don't think I have a full understanding of this feature in Go. For one thing, Go is fully multi-threaded and Go channels is both a communication mechanism and a point of synchronization. But AceRoutine works in a single-threaded environment, so thread synchronization is not really relevant. This part of AceRoutine has not been high on my priority list, so I don't have any plans for supporting multiple listeners to a Channel. If you have a working mechanism of passing info from your keyboard coroutine to its handlers, you should probably keep using it. You'll be able to debug the code better if something doesn't work. If you tried to use something like Channels, the control-flow is far more complicated, and the debugging is much harder. I had an implementation of a command-line parser and dispatcher using a producer Coroutine and a consumer Coroutine with a Channel in between, but I discovered that it was far simpler and more efficient to collapse the two coroutines into a single one, and get rid of the Channel. One thing that pops into my mind: You say that you use a class variable. I often use an instance variable, but if you have only a singleton instance of the keyboard coroutine, it doesn't make a difference I guess. Having said all that, if you are interested in exploring the concept of Channels further for just for your education and interest, you should do it, and let me know what you find out. |
Beta Was this translation helpful? Give feedback.
-
Hey, if I understand the concept of channels correctly, they are meant for sending messages from one coroutine to another, but it has to be a 1:1 relationship, one producer, one consumer.
In a project I am working on, I have a coroutine which reads inputs from a keyboard, but depending on the key that is pressed different coroutines should be triggered. The way I have solved this currently is by setting the key read as a class variable of the keyboard coroutine, and then all the other coroutines are reading the value from there and are awaiting if "their" key was pressed.
While this works, I am wondering if there is a nicer way of implementing this using channels. I am thinking of something like a broadcast channel, which multiple coroutines can listen to. Would that make sense?
Beta Was this translation helpful? Give feedback.
All reactions