Reputation: 343
Learning to use Ruby Threads for transportability of code between different OS platforms.
The problem is the console is frozen until non_join1 completes which also prevents non_join2 from being started. non_join1 waits at the join command until the threads complete.
The software requires multiple routines running independently. The primary program is a standalone that runs in realtime collecting data. The data collected is written to files. Different programs, using Threads, process the data in parallel. The start/stop and status is controlled from a main console.
What is the best ruby method to launch the separate programs needed to analyze the data files and get status back from the threads?
thanks, pb
# This is the console that starts up the multiple threads.
#!/usr/bin/ruby
loop do
puts " input a command"
command = gets.chop!
control = case command
when "1" : "1"
when "2" : "2"
end
if control == "1" then
puts `date`+ "routine 1"
puts `./non_join1.rb`
puts `date`
end
if control == "2" then
puts `date` + "routine 2"
`./non_join2.rb`
end
end
#!/usr/bin/ruby
# Example of worker program 1 used to process data files
#file non_join1.rb
x = Thread.new { sleep 0.1; print "xxxxxxxxx"; print "yyyyyyyyyyy"; print "zzzzzzzzzz" }
a = Thread.new { print "aaaaaaaaa"; print "bbbbbbbbbb"; sleep 0.1; print "cccccccc" }
puts " "
(1..10).each {|i| puts i.to_s+" done #{i}"}
x.join
a.join
sleep(30)
#!/usr/bin/ruby
# Example of worker program 2 used to process data files
#file non_join2.rb
x = Thread.new { sleep 0.1; print "xxxxxxxxx"; print "yyyyyyyyyyy"; print "zzzzzzzzzz" }
a = Thread.new { print "aaaaaaaaa"; print "bbbbbbbbbb"; sleep 0.1; print "cccccccc" }
x.join
a.join
$ ./call_ruby.rb
input a command
1
Sat Feb 18 10:36:43 PST 2012
routine 1
aaaaaaaaabbbbbbbbbb
1 done 1
2 done 2
3 done 3
4 done 4
5 done 5
6 done 6
7 done 7
8 done 8
9 done 9
10 done 10
xxxxxxxxxyyyyyyyyyyyzzzzzzzzzzcccccccc
Sat Feb 18 10:37:13 PST 2012
input a command
Upvotes: 1
Views: 239
Reputation: 1969
Instead of executing with `` try this forking (creating a new process), using for example this function:
class Execute
def self.run(command)
pid = fork do
Kernel.exec(command)
end
return pid
end
end
Your code will look like
loop do
puts " input a command"
command = gets.chop!
control = case command
when "1" : "1"
when "2" : "2"
end
if control == "1" then
puts `date`+ "routine 1"
Execute.run("./non_join1.rb")
puts `date`
end
if control == "2" then
puts `date` + "routine 2"
Execute.run("./non_join2.rb")
end
end
Upvotes: 1