Ruby 1.9.2 中的 Process.fork 和 Process.spawn 有什么区别 [英] What's the difference between Process.fork and Process.spawn in Ruby 1.9.2

查看:109
本文介绍了Ruby 1.9.2 中的 Process.fork 和 Process.spawn 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Process.fork 和 Ruby 1.9.2 中新的 Process.spawn 方法有什么区别,哪个更适合在子进程中运行另一个程序?据我了解 Process.fork 接受代码块,Process.spawn 接受一个系统命令和一些其他参数.我什么时候应该使用一个而不是另一个?

解决方案

Process.fork 和 Ruby 1.9.2 中新的 Process.spawn 方法有什么区别

Process.fork 允许您在另一个进程中运行 ruby​​ 代码.Process.spawn 允许您在另一个进程中运行另一个程序.基本上 Process.spawn 就像使用 Process.fork 然后在 fork 进程中调用 exec 一样,只是它给了你更多的选择.><块引用>

哪个更适合在子进程中运行另一个程序?

如果您需要向后兼容,请使用 fork + exec 因为 spawn 在 1.8 中不可用.否则使用 spawn 因为在子进程中运行另一个程序正是 spawn 的目的.

<块引用>

据我所知,Process.fork 接受代码块,Process.spawn 接受一个系统命令和一些其他参数.

没错.

<块引用>

我什么时候应该使用一个而不是另一个?

如果您需要在单独的进程中运行任意 ruby​​ 代码,请使用 fork(使用 spawn 不能这样做).如果您需要在子进程中调用应用程序,请使用 spawn.

What's the difference between Process.fork and the new Process.spawn methods in Ruby 1.9.2 and which one is better to run another program in a subprocess? As far as I understand Process.fork accepts block of code and Process.spawn takes a system command plus some other parameters. When I should use one instead of the other?

解决方案

What's the difference between Process.fork and the new Process.spawn methods in Ruby 1.9.2

Process.fork allows you to run ruby code in another process. Process.spawn allows you to run another program in another process. Basically Process.spawn is like using Process.fork and then calling exec in the forked process, except that it gives you more options.

and which one is better to run another program in a subprocess?

If you need backwards compatibility, use fork + exec as spawn is not available in 1.8. Otherwise use spawn since running another program in a subprocess is exactly what spawn is made for.

As far as I understand Process.fork accepts block of code and Process.spawn takes a system command plus some other parameters.

Exactly.

When I should use one instead of the other?

Use fork if you need to run arbitrary ruby code in a separate process (you can't do that with spawn). Use spawn if you need to invoke an application in a subprocess.

这篇关于Ruby 1.9.2 中的 Process.fork 和 Process.spawn 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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