Many Ruby client connections

Basically, we have a large (100+) number of remote embedded devices that we need for streaming data.

I can connect to any single remote host with the following code:

require 'socket' socket = TCPSocket.new '192.168.1.115', 8016 loop do socket.write("GET_DATA") data = socket.read(32) end s.close 

But I want to contact many of them at once. Is there a recommended way to do this?

+4
source share
1 answer

Use streams:

 ips = ['192.168.1.115', '...', '...'] threads = [] ips.each do |ip| threads << Thread.new do # Connect here, do some stuff with the socket end end threads.each { |t| t.join } 

Note that if you want real threading, you should use Jruby or Ruby> = 1.9, ruby ​​MRI <1.9 (standard ruby) thread implementation emulates threads ( green themes ), and only does real threading when one thread is waiting for input -output.

+4
source

Source: https://habr.com/ru/post/1468751/


All Articles