温馨提示×

Ruby TCP/IP通信如何处理并发

小樊
82
2024-11-03 03:49:44
栏目: 编程语言

在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.selectIO.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 = sock.gets
      puts "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中有许多事件驱动库,如EventMachineCelluloid,它们可以帮助你更高效地处理并发连接。

使用EventMachine

require 'eventmachine'

class MyServer < EventMachine::Connection
  def post_init
    @data = ''
    @send_data = false
  end

  def receive_data(data)
    @data += data
    if @data == 'hello'
      @send_data = true
    end
  end

  def send_data
    if @send_data
      send_data("Hello from EventMachine\n")
      @send_data = false
    end
  end

  def connection_closed
    @data = nil
    @send_data = false
  end
end

EventMachine.run do
  EventMachine.start_server('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适用于需要高性能的场景,而事件驱动库则适用于需要处理大量并发连接的场景。

0