我知道您可以在红宝石中执行类似的操作,
download_process = IO.popen "wget #{goodies}"
download_process.wait
puts "goodies gotten"
生成子流程并在完成时做出响应。
但是,说我想让我的脚本在等待子进程完成的同时忙于其他任务,然后定期检查是否有任何子进程已经完成。我将如何在红宝石中做到这一点?
我的目标是让n个同步下载线程由单个ruby线程管理,该线程还清理并处理下载的数据文件(因此,在处理文件后,它会检查有多少完整的下载文件正在等待,并决定相应地产生多少个下载线程)。
对于这种情况,请使用IO.popen,并将其包装在Thread中。为该工作示例添加了-q选项:
file1 = "http://www.vim.org/scripts/download_script.php?src_id=7701"
file2 = "http://www.openss7.org/repos/tarballs/strx25-0.9.2.1.tar.bz2"
threads = []
threads << Thread.new do
IO.popen("wget -q #{file1}"){ |io| io.read}
end
threads << Thread.new do
IO.popen("wget -q #{file2}"){ |io| io.read }
end
while true
sleep(2)
threads.each_with_index do |tr, index|
if tr.alive?
puts "Downloading in \##{index}"
else
puts "Downloaded in \##{index}"
threads[index] = Thread.new do
IO.popen("wget -q #{file1}"){ |io| io.read}
end
end
end
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句