A Hash-like async primitive similar to Notification or Variable #275
postmodern
started this conversation in
Ideas
Replies: 2 comments 5 replies
-
Sounds interesting, can you give me a simple example of the usage? |
Beta Was this translation helpful? Give feedback.
5 replies
-
Could you use a hash of require 'async'
require 'async/queue'
class Cubbies
def initialize() @inboxes = {} end
def box(id) @inboxes[id] ||= Async::Queue.new end
def drop(id) @inboxes.delete(id) end
def place(id, value) box(id).enqueue value end
def fetch id
value = box(id).dequeue
drop(id) if box(id).empty?
value
end
end
cubbies = Cubbies.new
names = [:anna,:john,:lily,:dave]
fruits = [:apple,:orange,:banana,:pear,:mango]
Async do
Async do
loop do
person, fruit = names.sample, fruits.sample
puts "#{fruit} ready for #{person}"
cubbies.place(person,fruit)
sleep rand(10)*0.1
end
end
Async do
loop do
person = names.sample
puts "#{person} is hungry"
fruit = cubbies.fetch(person)
puts "#{person} eats #{fruit}"
sleep rand(10)*0.1
end
end
end => |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello, I was searching the async gem's API looking for a primitive that would allow me to manage async RPC requests and the resulting responses. While the Async API already provides Notification and Variable, I think it would be useful to provide a Hash-like object for registering a notification using a unique key, and then allowing other code to trigger it's notification once a value has been returned for that unique key. This would help developers implement a "cubby-hole" style notification system for requested values and the eventual returned values.
Beta Was this translation helpful? Give feedback.
All reactions