簡體   English   中英

Ruby Http服務器在單獨的線程中

[英]Ruby Http server in separate thread

我正在使用https://practicingruby.com/articles/implementing-an-http-file-server?u=dc2ab0f9bb上找到的代碼在Ruby中啟動簡單的http服務器。

這段代碼很棒。 但是,server.accept調用和循環確實會阻塞主線程。 我將代碼更改為以下內容:

require 'socket' # Provides TCPServer and TCPSocket classes

#start the server thread
server_thread = Thread.start do

    # Initialize a TCPServer object that will listen
    # on localhost:2345 for incoming connections.
    server = TCPServer.new('localhost', 2345)

    # loop infinitely
    loop do

      # use a seprate thread, acception multiple incoming connections
      Thread.start(server.accept) do |socket|

          # Read the first line of the request (the Request-Line)
          request = socket.gets

          response = "Hello World!\n"

          # We need to include the Content-Type and Content-Length headers
          # to let the client know the size and type of data
          # contained in the response. Note that HTTP is whitespace
          # sensitive, and expects each header line to end with CRLF (i.e. "\r\n")
          socket.print "HTTP/1.1 200 OK\r\n" +
                       "Content-Type: text/plain\r\n" +
                       "Content-Length: #{response.bytesize}\r\n" +
                       "Connection: close\r\n"

          # Print a blank line to separate the header from the response body,
          # as required by the protocol.
          socket.print "\r\n"

          # Print the actual response body, which is just "Hello World!\n"
          socket.print response

          # Close the socket, terminating the connection
          socket.close

      end#do
    end#do
end#do

這樣,由於服務器在單獨的線程中運行,因此主線程不會阻塞。 但是,現在當我瀏覽到http://localhost:2345/沒有任何回報。

如何在單獨的線程中運行服務器?

重要的是要知道此腳本在接受Ruby插件的應用程序中運行。 因此,這並不意味着主線程將終止,從而導致子線程關閉。

解決方法如下:

require 'socket' # Provides TCPServer and TCPSocket classes

#start the server thread
server_thread = Thread.start do

    # Initialize a TCPServer object that will listen
    # on localhost:2345 for incoming connections.
    server = TCPServer.new('localhost', 2345)

    # loop infinitely
    loop do
      puts "Server started"
      # use a seprate thread, acception multiple incoming connections
      Thread.start(server.accept) do |socket|

          # Read the first line of the request (the Request-Line)
          request = socket.gets

          response = "Hello World!\n"

          # We need to include the Content-Type and Content-Length headers
          # to let the client know the size and type of data
          # contained in the response. Note that HTTP is whitespace
          # sensitive, and expects each header line to end with CRLF (i.e. "\r\n")
          socket.print "HTTP/1.1 200 OK\r\n" +
                       "Content-Type: text/plain\r\n" +
                       "Content-Length: #{response.bytesize}\r\n" +
                       "Connection: close\r\n"

          # Print a blank line to separate the header from the response body,
          # as required by the protocol.
          socket.print "\r\n"

          # Print the actual response body, which is just "Hello World!\n"
          socket.print response

          # Close the socket, terminating the connection
          socket.close

      end#do
    end#do
end#do

spam_thread = Thread.start do
  loop do
    puts "Another thread"
    sleep 1
  end
end

server_thread.join
spam_thread.join

重要的是要join從主線程產生的所有線程,因為在主線程完成后,所有未連接的線程都會被殺死。在本例中,一切正常,因為主線程處於無限循環中,占用了主線程創建服務器線程后,主線程立即完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM