如何确定 ruby​​ 是否可以在不过度使用正则表达式的情况下进行分叉?

发布于 2024-08-30 00:26:31 字数 881 浏览 3 评论 0原文

是否可以确定您正在运行的 ruby​​ 实现是否能够支持 fork,而无需针对 RUBY_PLATFORM 运行正则表达式,该正则表达式将扩展直到 召唤克苏鲁

(相关问题:Ruby -我怎样才能知道我的程序正在哪个系统上运行?

编辑:我尝试了马克-安德烈的建议。它不适用于默认禁用 fork 的 jruby:

192-168-1-7:~ agrimm$ jruby --1.9 -S jirb 
irb(main):001:0> RUBY_VERSION
=> "1.9.2dev"
irb(main):002:0> Process.respond_to?(:fork)
=> true
irb(main):003:0> Process.fork
NotImplementedError: fork is unsafe and disabled by default on JRuby

更新:从 Marc-Andre 的链接来看,从创建 ruby​​ 实现的角度来看,它似乎比我更明智地解决了这个问题,但失败了。

从编写 ruby​​ 库的人的角度来看,除了运行 fork 并查看它是否引发异常之外,最全面的咒语是什么?

Is it possible to determine whether the implementation of ruby you're running on is capable of supporting fork, without running a regex against RUBY_PLATFORM that'll expand until it summons Cthulhu?

(Related question: Ruby - How can I find out on which system my program is running?)

Edit: I tried Marc-Andre's suggestion. It doesn't work for jruby with fork disabled by default:

192-168-1-7:~ agrimm$ jruby --1.9 -S jirb 
irb(main):001:0> RUBY_VERSION
=> "1.9.2dev"
irb(main):002:0> Process.respond_to?(:fork)
=> true
irb(main):003:0> Process.fork
NotImplementedError: fork is unsafe and disabled by default on JRuby

Update: From Marc-Andre's link, it seems wiser heads than I have grappled with this problem from the perspective of creating ruby implementations, and failed.

From the perspective of someone who's writing a ruby library, what would be the most comprehensive incantation, short of running fork and seeing if it raises an exception?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

皇甫轩 2024-09-06 00:26:31

在 Ruby 1.9 中:

Process.respond_to?(:fork)  # => true if fork is supported, false otherwise

对于 Ruby 1.8 或 JRuby(当前未实现此功能),您必须实际测试它。

另请参阅关于 ruby​​-core 的长篇讨论

In Ruby 1.9:

Process.respond_to?(:fork)  # => true if fork is supported, false otherwise

For Ruby 1.8, or JRuby (which doesn't implement this currently) you'll have to actually test it.

See also this long discussion on ruby-core.

别靠近我心 2024-09-06 00:26:31

您可以测试 fork 本身,而不是针对 RUBY_PLATFORM 或其他内容进行容易出错的测试:

def can_fork?
  pid = fork
  exit unless pid # exit the child immediately
  true
rescue NotImplementedError
  false
end

一个缺点是,如果以某种方式模拟 fork ,这可能会使此检查变得昂贵。

Instead of error-prone testing against RUBY_PLATFORM or other things you could test fork itself:

def can_fork?
  pid = fork
  exit unless pid # exit the child immediately
  true
rescue NotImplementedError
  false
end

One downside would be if fork is somehow emulated which could make this check expensive.

楠木可依 2024-09-06 00:26:31

最近对 Rails 的提交用于

Config::CONFIG['host_os'] !~ /mswin|mingw/)

测试它是否不在 Windows 上,

RUBY_PLATFORM !~ /java/

并测试它是否不在 JRuby 上。但是,我知道可以启用 fork 作为命令行开关。我必须研究是否有任何方法可以判断它是否已启用。

我怀疑 macruby 不支持或强烈反对分叉,并且从 Marc-Andre 提到的线程中,您可以使用

 RUBY_ENGINE != "macruby"

Now 对所有其他平台进行测试...(正在进行中,标记为社区 wiki ,请随意编辑)

A recent commit to rails uses

Config::CONFIG['host_os'] !~ /mswin|mingw/)

to test whether it's not on Windows, and

RUBY_PLATFORM !~ /java/

to test whether it's not on JRuby. However, I know it's possible to enable fork as a command-line switch. I'd have to look into whether there's any way of telling whether it's been enabled.

I suspect macruby doesn't support, or strongly discourages, forking, and from the thread Marc-Andre refers to, you can test for it with

 RUBY_ENGINE != "macruby"

Now for all the other platforms out there... (work in progress, marking as community wiki, please feel free to edit)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文