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


Groups > comp.lang.ruby > #4296

Re: Creating and Executing New Threads

From Joel VanderWerf <joelvanderwerf@gmail.com>
Newsgroups comp.lang.ruby
Subject Re: Creating and Executing New Threads
Date 2011-05-11 15:44 -0500
Organization Service de news de lacave.net
Message-ID <4DCAF500.8040505@gmail.com> (permalink)
References <2fad4e571f05e17ee09ba2e4b856b9f4@ruby-forum.com>

Show all headers | View raw


On 05/11/2011 01:32 PM, Dan King wrote:
> 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 }
>

All the threads are running in one process, hence there is only one pid.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next 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