# Gserver hogging CPU

**URL:** <https://www.ruby-forum.com/t/gserver-hogging-cpu/169385>\
**Category:** Ruby\
**Created:** [June 3, 2009, 4:25am UTC](https://www.ruby-forum.com/t/gserver-hogging-cpu/169385 "2009-06-03T04:25:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sclaerhout](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/85f322/32.png) [@sclaerhout](https://www.ruby-forum.com/u/sclaerhout)\
**Post date:** [June 3, 2009, 4:25am UTC](https://www.ruby-forum.com/t/gserver-hogging-cpu/169385/1 "2009-06-03T04:25:50Z")

</div>

I’m running a server on Mac OS 10.5.7, Ruby 1.8.6, that’s almost exactly  
the same as the one in Peter C.'s Beginning Ruby book. When I run  
the Activity Monitor it shows Ruby is consuming about 98% of one of my  
CPUs when there’s no client activity (I have only 3 clients, 2 in the  
same computer). I need the computer for other important things, so this  
concerns me.

## What can I do to free up the CPU? I’ve tried adding a few sleeps, to not avail. Here’s the code:

#!/usr/bin/env ruby

# Start this server with

# ruby -w GServerchat.rb \<port#\>

# or just

# GServerchat.rb \<port#\> if file is executable

# 

require ‘gserver’

class ChatServer \< GServer # Server class derived from GServer super  
class  
def initialize(\*args)  
super(\*args)

```
# Keep a record of the client IDs allocated
# and the lines of chat
@@client_id = 0
@@chat = []

```

end

def serve(io) # Serve method handles connections

# Increment the client ID so each client gets a unigue ID

```
  @@client_id += 1
  my_client_id = @@client_id
  my_position = @@chat.size

  io.puts("welcome, client #{@@client_id}!")

  #leave a 'joined' message on the chat queue
  @@chat << [my_client_id, "<joins the chat>"]

  loop do
  #Every 2 seconds check for data
  if IO.select([io], nil, nil, 2)
  # If so, retrieve the data and process it...
  line = io.gets

  # Write the new line to the log file
logfile = File.open('/tmp/chatlog', 'a')
logfile.puts line
logfile.close

  # If user says quit, disconnect them
  if line =~ /quit/
    @@chat << [my_client_id, "< leaves the chat>"]
    break
  end

  # Shut down the server if we hear 'shutdown'
  self.stop if line =~ /shutdown/

  # Add the client's text to the chat array along with client's ID

  @@chat << [my_client_id, line]
else
  # No data, so print any newlines from the chat stream
  @@chat[my_position..(@@chat.size - 1)].each_with_index do |line,

```

index|  
io.puts("#{line[0]} says: #{line[1]}")  
end

```
   #Move the position to one past array end
  my_position = @@chat.size
  end

```

end

end  
end

##server = ChatServer.new(ARGV[0])

portnum = ARGV[0] || 50000  
my\_ip =  
Socket::getaddrinfo(Socket.gethostname,“echo”,Socket::AF\_INET)[0][3]  
server = ChatServer.new(portnum, my\_ip, 5, $stdout, true)

server.start # Start the server

loop do  
break if server.stopped?  
end

* * *

Thanks for any help-  
Scott

---

<div class="post-metadata">

**Author:** ![sclaerhout](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/85f322/32.png) [@sclaerhout](https://www.ruby-forum.com/u/sclaerhout)\
**Post date:** [June 3, 2009, 6:28am UTC](https://www.ruby-forum.com/t/gserver-hogging-cpu/169385/2 "2009-06-03T06:28:07Z")

</div>

> What can I do to free up the CPU? I’ve tried adding a few sleeps, to not  
> avail. Here’s the code:

perhaps profile it with ruby prof, perhaps try other versions of ruby  
(MBARI, etc.)  
-=r

---

<div class="post-metadata">

**Author:** ![sclaerhout](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/85f322/32.png) [@sclaerhout](https://www.ruby-forum.com/u/sclaerhout)\
**Post date:** [June 3, 2009, 7:26am UTC](https://www.ruby-forum.com/t/gserver-hogging-cpu/169385/3 "2009-06-03T07:26:48Z")

</div>

On Jun 2, 2009, at 7:25 PM, Scott Cole wrote:

> not  
> require ‘gserver’  
> end  
> @@chat \<\< [my\_client\_id, “”]  
> logfile.close  
> # Add the client’s text to the chat array along with client’s ID  
> my\_position = @@chat.size  
> my\_ip =  
> Socket::getaddrinfo(Socket.gethostname,“echo”,Socket::AF\_INET)[0][3]  
> server = ChatServer.new(portnum, my\_ip, 5, $stdout, true)
> 
> server.start # Start the server
> 
> loop do  
> break if server.stopped?  
> end

This last loop looks suspect. You are basically calling  
server.stopped? in a loop as fast as you can. Try sleeping a bit in  
that loop:

loop do  
sleep 1  
break if server.stopped?  
end

Cheers-  
Ezra Z.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sclaerhout](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/85f322/32.png) [@sclaerhout](https://www.ruby-forum.com/u/sclaerhout)\
**Post date:** [June 3, 2009, 12:09pm UTC](https://www.ruby-forum.com/t/gserver-hogging-cpu/169385/4 "2009-06-03T12:09:55Z")

</div>

Ezra Z. wrote:

> > What can I do to free up the CPU? I’ve tried adding a few sleeps, to not  
> > require ‘gserver’  
> > end  
> > @@chat \<\< [my\_client\_id, “”]  
> > logfile.close  
> > # Add the client’s text to the chat array along with client’s ID  
> > my\_position = @@chat.size  
> > my\_ip =  
> > server.stopped? in a loop as fast as you can.  
> > Indeed.

Much better to do:

server.join

instead of using the loop.

-Justin

---

<div class="post-metadata">

**Author:** ![sclaerhout](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/85f322/32.png) [@sclaerhout](https://www.ruby-forum.com/u/sclaerhout)\
**Post date:** [June 3, 2009, 7:33pm UTC](https://www.ruby-forum.com/t/gserver-hogging-cpu/169385/5 "2009-06-03T19:33:40Z")

</div>

Ezra, thanks. This worked great! My CPU usage is below 1% now, even with  
sleep 0.1.

Ezra Z. wrote:

> > server.start # Start the server
> > 
> > loop do  
> > break if server.stopped?  
> > end
> 
> This last loop looks suspect. You are basically calling  
> server.stopped? in a loop as fast as you can. Try sleeping a bit in  
> that loop:
> 
> loop do  
> sleep 1  
> break if server.stopped?  
> end
> 
> Cheers-  
> Ezra Z.  
> removed\_email\_address@domain.invalid
