K
Krishna Dole
Hi all,
If anyone is willing, I'd be grateful for some advice on the forking
job scheduler I've written. It works fine in simple tests, but does
not feel elegant. On IRC kbrooks recommended an asynchronous main
loop, but i don't understand how to implement that in this situation.
The first version I wrote used threads, but several sources
recommended fork instead. I have also considered just using the shell
command 'ps' to see how many jobs are running, launching more as
needed.
The basic requirements:
- Each job is a long-running external process (taking a day or more)
and all jobs require a different amount of time to run (so
asynchronous launching will be needed).
- I want to keep N jobs running at all times (N = 4 in the example below)
Thanks,
Krishna
##############################################
@jobs = (1..10).to_a # an imaginary list of jobs
# any time a job finishes, launch another
Signal.trap("CLD") { start_job unless @jobs.empty? }
def start_job
my_job = @jobs.pop
puts "starting job #{my_job}"
exec("sleep 2") if fork == nil # launch a job. in reality it would
run for a day or more
end
for num in 1..4 # i want to keep 4 jobs running at all times
start_job
end
# this doesn't wait for the last jobs to finish
while @jobs.size > 0
Process.wait
end
# this waits for the last jobs, but if i only had this line, it
wouldn't wait for all the jobs to start!
Process.wait
If anyone is willing, I'd be grateful for some advice on the forking
job scheduler I've written. It works fine in simple tests, but does
not feel elegant. On IRC kbrooks recommended an asynchronous main
loop, but i don't understand how to implement that in this situation.
The first version I wrote used threads, but several sources
recommended fork instead. I have also considered just using the shell
command 'ps' to see how many jobs are running, launching more as
needed.
The basic requirements:
- Each job is a long-running external process (taking a day or more)
and all jobs require a different amount of time to run (so
asynchronous launching will be needed).
- I want to keep N jobs running at all times (N = 4 in the example below)
Thanks,
Krishna
##############################################
@jobs = (1..10).to_a # an imaginary list of jobs
# any time a job finishes, launch another
Signal.trap("CLD") { start_job unless @jobs.empty? }
def start_job
my_job = @jobs.pop
puts "starting job #{my_job}"
exec("sleep 2") if fork == nil # launch a job. in reality it would
run for a day or more
end
for num in 1..4 # i want to keep 4 jobs running at all times
start_job
end
# this doesn't wait for the last jobs to finish
while @jobs.size > 0
Process.wait
end
# this waits for the last jobs, but if i only had this line, it
wouldn't wait for all the jobs to start!
Process.wait