Keep thread in memory

C

Craig Williams

[Note: parts of this message were removed to make it a legal post.]

Hi,

I'm writing a simple UDP listening server. It works great using this

PORT = 1234
server = UDPSocket.open
server.bind("127.0.0.1", PORT)

server_thread = Thread.start(server) do |server|
100.times { #do something }
end

server_thread.join

The above was based off some code from the Ruby 1.9 book - I have a problem
now trying to take it a step further.

How do I get the above to run permanently or until some other condition
besides (100) is reached?

When I run this on my remote server over SSH it works but if I ctrl-z or
exit ssh, the process seems to be in memory but isn't actually working. I'd
like to run it and have it go into memory until it is killed and return me
to the ssh command.

I tried leaving off the server_thread.join but then it just quits. Could
someone explain exactly what Thread.start is doing here.

thanks!
-c
 
M

Michael Malone

Craig said:
Hi,

I'm writing a simple UDP listening server. It works great using this

PORT = 1234
server = UDPSocket.open
server.bind("127.0.0.1", PORT)

server_thread = Thread.start(server) do |server|
100.times { #do something }
end

server_thread.join

The above was based off some code from the Ruby 1.9 book - I have a problem
now trying to take it a step further.

How do I get the above to run permanently or until some other condition
besides (100) is reached?

When I run this on my remote server over SSH it works but if I ctrl-z or
exit ssh, the process seems to be in memory but isn't actually working. I'd
like to run it and have it go into memory until it is killed and return me
to the ssh command.

I tried leaving off the server_thread.join but then it just quits. Could
someone explain exactly what Thread.start is doing here.

thanks!
-c
It does exactly what you think it will do. It starts a thread, runs the
block and exits when the block is completed.
If you want it to wait around for things to happen, like something being
fed down a socket, you just need to make a call to an async function,
like IO.read(server) which will wait around until there is something to
read.

I can't explain why your process is still in memory. To me, it should
have exited and dropped off the face of the planet.

=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
=======================================================================
 
R

Robert Klemme

[Note: parts of this message were removed to make it a legal post.]

Hi,

I'm writing a simple UDP listening server. It works great using this

PORT = 1234
server = UDPSocket.open
server.bind("127.0.0.1", PORT)

server_thread = Thread.start(server) do |server|
100.times { #do something }
end

server_thread.join

First of all, why are you using a thread here? Do you have other work
that needs to be done concurrently?
The above was based off some code from the Ruby 1.9 book - I have a problem
now trying to take it a step further.

How do I get the above to run permanently or until some other condition
besides (100) is reached?

Depends on the condition. If you want this to run indefinitely you can
simply do

loop do
...
end
When I run this on my remote server over SSH it works but if I ctrl-z or
exit ssh, the process seems to be in memory but isn't actually working. I'd
like to run it and have it go into memory until it is killed and return me
to the ssh command.

"Go into memory"??? All programs are executed in memory. What you
probably rather need is a demon, i.e. a program that continues to work
even if the terminal gets removed (ssh close).
I tried leaving off the server_thread.join but then it just quits. Could
someone explain exactly what Thread.start is doing here.

In your example above the thread is completely superfluous since you do
not do anything concurrently. A plain old infinite loop would do the
same job. Basically your code is equivalent to

PORT = 1234
server = UDPSocket.open
server.bind("127.0.0.1", PORT)

100.times { #do something }

Kind regards

robert
 
C

Craig Williams

[Note: parts of this message were removed to make it a legal post.]

thanks Robert,

A simple loop..do will work and yes, a daemon is what I want to create. I'll
look it up and see if I can find some theory around this in Ruby. I should
have said a TSR right ;-)

Is it good practice in Ruby to sleep when in an infinite loop? or will Ruby
protect me from that kind of issue?

thanks,

-c


[Note: parts of this message were removed to make it a legal post.]

Hi,

I'm writing a simple UDP listening server. It works great using this

PORT = 1234
server = UDPSocket.open
server.bind("127.0.0.1", PORT)

server_thread = Thread.start(server) do |server|
100.times { #do something }
end

server_thread.join

First of all, why are you using a thread here? Do you have other work that
needs to be done concurrently?

The above was based off some code from the Ruby 1.9 book - I have a
problem
now trying to take it a step further.

How do I get the above to run permanently or until some other condition
besides (100) is reached?

Depends on the condition. If you want this to run indefinitely you can
simply do

loop do
...
end

When I run this on my remote server over SSH it works but if I ctrl-z or
exit ssh, the process seems to be in memory but isn't actually working.
I'd
like to run it and have it go into memory until it is killed and return me
to the ssh command.

"Go into memory"??? All programs are executed in memory. What you
probably rather need is a demon, i.e. a program that continues to work even
if the terminal gets removed (ssh close).

I tried leaving off the server_thread.join but then it just quits. Could
someone explain exactly what Thread.start is doing here.

In your example above the thread is completely superfluous since you do not
do anything concurrently. A plain old infinite loop would do the same job.
Basically your code is equivalent to

PORT = 1234
server = UDPSocket.open
server.bind("127.0.0.1", PORT)

100.times { #do something }

Kind regards

robert
 
M

Michael Linfield

Is it good practice in Ruby to sleep when in an infinite loop? or will

Dunno why you would sleep a program when you don't have to o_O

sleeping is most useful when working with Threads, though in some cases
there may be a reason you'd wish to have the program wait for a few
seconds I suppose.

- Mac
 
T

Tommy Nordgren

Hi,

I'm writing a simple UDP listening server. It works great using this

PORT = 1234
server = UDPSocket.open
server.bind("127.0.0.1", PORT)

server_thread = Thread.start(server) do |server|
100.times { #do something }
end

server_thread.join

The above was based off some code from the Ruby 1.9 book - I have a
problem
now trying to take it a step further.

How do I get the above to run permanently or until some other
condition
besides (100) is reached?

When I run this on my remote server over SSH it works but if I ctrl-
z or
ctrl-z suspends the current job, and moves it to the background
exit ssh, the process seems to be in memory but isn't actually
working. I'd
like to run it and have it go into memory until it is killed and
return me
to the ssh command.

I tried leaving off the server_thread.join but then it just quits.
Could
someone explain exactly what Thread.start is doing here.

thanks!
-c

-----------------------------------
See the amazing new SF reel: Invasion of the man eating cucumbers from
outer space.
On congratulations for a fantastic parody, the producer replies :
"What parody?"

Tommy Nordgren
(e-mail address removed)
 
C

Craig Williams

[Note: parts of this message were removed to make it a legal post.]

thanks everyone - I found the Daemons wrapper and a simple loop was well
simple :) and it works great.

I also know what ctrl-z does now .... learning is fun ;-)

-c

thanks Robert,

A simple loop..do will work and yes, a daemon is what I want to create.
I'll
look it up and see if I can find some theory around this in Ruby. I should
have said a TSR right ;-)

Is it good practice in Ruby to sleep when in an infinite loop? or will Ruby
protect me from that kind of issue?

thanks,

-c


[Note: parts of this message were removed to make it a legal post.]

Hi,

I'm writing a simple UDP listening server. It works great using this

PORT = 1234
server = UDPSocket.open
server.bind("127.0.0.1", PORT)

server_thread = Thread.start(server) do |server|
100.times { #do something }
end

server_thread.join

First of all, why are you using a thread here? Do you have other work that
needs to be done concurrently?

The above was based off some code from the Ruby 1.9 book - I have a
problem
now trying to take it a step further.

How do I get the above to run permanently or until some other condition
besides (100) is reached?

Depends on the condition. If you want this to run indefinitely you can
simply do

loop do
...
end

When I run this on my remote server over SSH it works but if I ctrl-z or
exit ssh, the process seems to be in memory but isn't actually working.
I'd
like to run it and have it go into memory until it is killed and return me
to the ssh command.

"Go into memory"??? All programs are executed in memory. What you
probably rather need is a demon, i.e. a program that continues to work even
if the terminal gets removed (ssh close).

I tried leaving off the server_thread.join but then it just quits. Could
someone explain exactly what Thread.start is doing here.

In your example above the thread is completely superfluous since you do not
do anything concurrently. A plain old infinite loop would do the same job.
Basically your code is equivalent to

PORT = 1234
server = UDPSocket.open
server.bind("127.0.0.1", PORT)

100.times { #do something }

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top