Replies: 4 comments 1 reply
-
Firstly, you should stop using Regarding your program, you need to have multiple #!/usr/bin/env ruby
require 'async'
require 'io/endpoint/unix_endpoint'
require 'io/endpoint/bound_endpoint'
Async do
endpoint = IO::Endpoint.unix("server.ipc")
bound_endpoint = endpoint.bound
server_task = Async do
bound_endpoint.accept do |client|
Console.logger.info(client, "Accepted connection")
while line = client.gets
client.puts line
end
end
end
sleep 1
10.times do
UNIXSocket.open(endpoint.path) do |server|
server.puts "Hello World"
p server.gets
server.close
end
end
ensure
server_task&.stop
bound_endpoint&.close
end In this case we have the server running in a nested task which allows our client to make connections to it, concurrently. |
Beta Was this translation helpful? Give feedback.
-
Hi @ioquatix, Thanks for the quick reply. If I understand correctly, I should wrap all the code in an async block. While this works, the main async is also blocking, and it is only released because we stopped the server at some point, right? Thanks in advance, tremendous work on the Async |
Beta Was this translation helpful? Give feedback.
-
In general, if you want those parts of the code to run concurrently, then yes.
Yes, at some level all programs must wait for the work to be completed.
Yes, and there are different strategies for how you achieve this, e.g.
This is unstructured concurrency. I don't think it's a good model. https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/ |
Beta Was this translation helpful? Give feedback.
-
Hi Samuel, thanks for the detailed answer. I'm trying to implement some sort of OTP framework with gen_servers, tcp_server, supervisors, etc. I've implemented a gen_server with Ractor, but I find some of the safety safeguards a little bit limiting and challenging to debug. Async suite seems to be better suited for this use case! |
Beta Was this translation helpful? Give feedback.
-
Sorry for the noob question here, but something that I do not understand is that Async is blocking the code execution; I'm assuming that Async should not block and run concurrently, but I am not sure if I understand this correctly.
Example 1, a running server:
now, If I put a Thread new on the Async, it will not block.
how can I achieve the non block only with async gems?
Beta Was this translation helpful? Give feedback.
All reactions