与Ruby 1.8和Windows一起使用 [英] fork with Ruby 1.8 and Windows

查看:78
本文介绍了与Ruby 1.8和Windows一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ruby 1.8.7 patchlevel 302,并且正在Windows XP系统上工作. 我必须启动一个外部过程,需要对用户输入做出反应.如果使用线程,则该过程不响应,因此尝试使用fork.使用fork时,外部进程会对用户输入做出反应,但它执行的不仅是fork块.例如

I'm using ruby 1.8.7 patchlevel 302 and I'm working on a Windows xp system. I have to start an external process that needs to react on user input. The process doesn't react if I use threads, so I tried using fork. With fork the external process reacts on the user input but it executes more than just the fork block. For example

fork do
  puts 'child'
end
puts 'parent'
Process.wait
puts 'done'

在我的机器上产生以下输出:

produces the following output on my machine:

parent
child
parent
done
done

如您所见,完成"和父母"被打印两次.我该怎么做才能使孩子仅执行其代码块而不执行更多代码? (由于某些宝石原因,我无法切换到Ruby 1.9)

As you can see 'done' and 'parent' is printed twice. What can I do to make the child execute only its block and not more? (I can't switch to Ruby 1.9 because of some gems)

推荐答案

喜欢:

if child = fork
  puts 'parent'
  Process.wait
  puts 'done'
else
  puts 'child'
end

这篇关于与Ruby 1.8和Windows一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆