Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4294

Creating and Executing New Threads

From Dan King <dan.king106@yahoo.com>
Newsgroups comp.lang.ruby
Subject Creating and Executing New Threads
Date 2011-05-11 15:32 -0500
Organization Service de news de lacave.net
Message-ID <2fad4e571f05e17ee09ba2e4b856b9f4@ruby-forum.com> (permalink)

Show all headers | View raw


I expected the script below to do the following:

(1) spawn a new thread
(2) connect to a server
(3) read the data transmitted by the server
(4) close the connection.
(5) repeat 1-4

While the script works, it does NOT spawn a new thread for each
connection. Does anyone why that is the case? Thanks.

require 'socket'

threads = []

500.times do
  threads << Thread.new do
    socket = TCPSocket.open('127.0.0.1', 23)
    pid = Process.pid
    while line = socket.gets
      print "Client thread (#{pid})\t" + line.chomp + "\n"
    end
    socket.close
  end
end

threads.each { |t| t.join }

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Creating and Executing New Threads Dan King <dan.king106@yahoo.com> - 2011-05-11 15:32 -0500
  Re: Creating and Executing New Threads Joel VanderWerf <joelvanderwerf@gmail.com> - 2011-05-11 15:44 -0500
  Re: Creating and Executing New Threads 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 17:52 -0500
    Re: Creating and Executing New Threads Christopher Dicely <cmdicely@gmail.com> - 2011-05-11 19:42 -0500
      Re: Creating and Executing New Threads 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-12 12:29 -0500

csiph-web