在Ruby中处理TCP/IP通信并发可以通过多种方式实现,包括使用线程、异步IO和事件驱动库等。下面是一些常见的方法:
1. 使用线程(Threads)
线程是Ruby中最基本的并发机制。你可以创建多个线程来同时处理多个TCP连接。
require 'socket'
server = TCPServer.new('localhost', 8080)
threads = []
server.each_connection do |client|
threads << Thread.new do
client.puts "Hello from thread #{Thread.current.object_id}"
client.close
end
end
threads.each(&:join)
2. 使用异步IO(Async IO)
Ruby的IO
模块提供了异步IO支持,可以使用IO.select
或IO.wait
来处理多个连接。
require 'socket' require 'io/select' server = TCPServer.new('localhost', 8080) inputs = [server] outputs = [] while inputs.any? readable, writable, exceptional = IO.select(inputs, outputs, inputs) readable.each do |sock| if sock == server client = server.accept inputs << client puts "New connection from #{client.peerhost}:#{client.peerport}" else data = https://www.yisu.com/ask/sock.gets"Received: #{data}" outputs << sock end end writable.each do |sock| sock.puts "Message received" end exceptional.each do |sock| puts "Exception on #{sock.path}: #{sock.error}" inputs.delete(sock) sock.close end end
3. 使用事件驱动库(Event-driven libraries)
Ruby中有许多事件驱动库,如EventMachine
和Celluloid
,它们可以帮助你更高效地处理并发连接。
使用EventMachine
require 'eventmachine' class MyServer < EventMachine::Connection def post_init @datahttps://www.yisu.com/ask/= '' @send_data = https://www.yisu.com/ask/false'hello' @send_data = https://www.yisu.com/ask/true"Hello from EventMachine\n") @send_data = https://www.yisu.com/ask/false'localhost', 8080, MyServer) end
使用Celluloid
require 'celluloid' require 'socket' class MyServer include Celluloid::IO def initialize(host, port) @server = TCPServer.new(host, port) end def start @server.each_connection do |client| handle_connection(client) end end def handle_connection(client) client.puts "Hello from Celluloid" client.close end end MyServer.run('localhost', 8080)
总结
选择哪种方法取决于你的具体需求和应用场景。线程适用于简单的并发场景,异步IO适用于需要高性能的场景,而事件驱动库则适用于需要处理大量并发连接的场景。