From: Craig Williams Date: 2009-03-16T07:29:33+09:00 Subject: Re: Keep thread in memory --0016e6471696b4f44204652fe6cd Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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 On Sun, Mar 15, 2009 at 6:22 PM, Robert Klemme wrote: > On 15.03.2009 22:35, Craig Williams wrote: > >> [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 > > --0016e6471696b4f44204652fe6cd--