0

Here's the code:

while 1
    input = gets
    puts input
end

Here's what I want to do but I have no idea how to do it: I want to create multiple instances of the code to run in the background and be able to pass input to a specific instance.

Q1: How do I run multiple instances of the script in the background?

Q2: How do I refer to an individual instance of the script so I can pass input to the instance (Q3)?

Q3: The script is using the cmd "gets" to take input, how would I pass input into an indivdual's script's gets?

e.g

Let's say I'm running threes instances of the code in the background and I refer to the instance as #1, #2, and #3 respectively. I pass "hello" to #1, #1 puts "hello" to the screen. Then I pass "world" to #3 and #3 puts "hello" to the screen.

Thanks!

UPDATE: Answered my own question. Found this awesome tut: http://rubylearning.com/satishtalim/ruby_threads.html and resource here: http://www.ruby-doc.org/core/classes/Thread.html#M000826.

puts Thread.main

x = Thread.new{loop{puts 'x'; puts gets; Thread.stop}}
y = Thread.new{loop{puts 'y'; puts gets; Thread.stop}}
z = Thread.new{loop{puts 'z'; puts  gets; Thread.stop}}

while x.status != "sleep" and y.status != "sleep" and z.status !="sleep"
    sleep(1)
end

Thread.list.each {|thr| p thr }

x.run
x.join

Thank you for all the help guys! Help clarified my thinking.

Waley Chen
  • 929
  • 3
  • 10
  • 23
  • How will you specify which daemon you are communicating with? Are you sending information from the command line? Do you want another Ruby program that is communicating with a specific daemon? Can you please describe what you are really trying to accomplish (with more details than "gets" and "puts")? – Phrogz Jul 07 '11 at 03:45
  • I have no idea how to pick which daemon to communicate with, sorry-didn't make that clear enough. Yup, I'm sending information from the cmd line. I'll try to make it more clear. – Waley Chen Jul 07 '11 at 03:48

3 Answers3

4

I assume that you mean that you want multiple bits of Ruby code running concurrently. You can do it the hard way using Ruby threads (which have their own gotchas) or you can use the job control facilities of your OS. If you're using something UNIX-y, you can just put the code for each daemon in separate .rb files and run them at the same time.

E.g.,

# ruby daemon1.rb &
# ruby daemon2.rb &

There are many ways to "handle input and output" in a Ruby program. Pipes, sockets, etc. Since you asked about daemons, I assume that you mean network I/O. See Net::HTTP.

Dan Barowy
  • 2,270
  • 24
  • 35
  • For input I'm imagining multiple scripts in the background waiting for input using "gets". I'm guessing I would have some kind of id for each script (how do you do that?) and I'd like to know the method to access each script's input stream so I call pass input to gets. – Waley Chen Jul 07 '11 at 03:45
  • It would help to know precisely what problem you're trying to solve. UNIX provides facilities to allow programs to 'talk' to each other via 'pipes' in a producer-consumer relationship. The details of the communication are handled by the OS; all you need to do is make one program output to the standard output stream (STDOUT) and the other read from standard input (STDIN). Google will be of great help to you here, as will a good OS book. – Dan Barowy Jul 07 '11 at 06:06
2

Ignoring what you think will happen with multiple daemons all fighting over STDIN at the same time:

(1..3).map{ Thread.new{ loop{ puts gets } } }.each(&:join)

This will create three threads that loop indefinitely, asking for input and then outputting it. Each thread is "joined", preventing the main program from exiting until each thread is complete (which it never will be).

Phrogz
  • 296,393
  • 112
  • 651
  • 745
-3

You could try using multi_daemons gem which has capability to run multiple daemons and control them.

# this is server.rb

proc_code = Proc do
  loop do
    sleep 5
  end
end

scheduler = MultiDaemons::Daemon.new('scripts/scheduler', name: 'scheduler', type: :script, options: {})
looper = MultiDaemons::Daemon.new(proc_code, name: 'looper', type: :proc, options: {})
MultiDaemons.runner([scheduler, looper], { force_kill_timeout: 60 })

To start and stop

ruby server.rb start
ruby server.rb stop
Community
  • 1
  • 1