4 ms·
"Let’s say we want to send information to another Ractor, but don’t want to block for it to finish processing it. What happens if the receiving Ractor is too sl
by peheje 5y ago
"Let’s say we want to send information to another Ractor, but don’t want to block for it to finish processing it. What happens if the receiving Ractor is too slow to process the data?"
I would prefer an API where the channels can be "buffered", so that a channel can be configured to hold at maximum N items, and trying to add to a full channel would block the producer.
I would also suggest that you totally split the consuming/producing from other tasks, that way you don't have to worry about blocking them.
Some pseudo code:
channel = newChannel<string>(size: 10)
task {
// Producer
loop {
channel.send("hello")
sleepMs(500)
}
}
task {
// Consumer
loop {
message = channel.receive()
print(message)
sleepMs(1000)
}
}
task {
// Some other code
loop {
print("working hard")
sleepMs(100)
{
}
I like the way Kotlin does this, as well as Crystal:
https://kotlinlang.org/docs/channels.html https://kotlinlang.org/docs/channels.html
https://crystal-lang.org/reference/guides/concurrency.html https://crystal-lang.org/reference/guides/concurrency.html