在jRuby中使用"fork"生成进程的替代方法? [英] Alternative for spawning a process with 'fork' in jRuby?
本文介绍了在jRuby中使用"fork"生成进程的替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在MRI Ruby中,我可以这样做:
In MRI Ruby I can do this:
def transfer
internal_server = self.init_server
pid = fork do
internal_server.run
end
# Make the server process run independently.
Process.detach(pid)
internal_client = self.init_client
# Do other stuff with connecting to internal_server...
internal_client.post('some data')
ensure
# Kill server
Process.kill('KILL', pid) if pid
end
但是上述代码无法在jRuby中运行,因为它不支持'fork'方法:
However the above code will not run in jRuby, because it does not support 'fork' method:
NotImplementedError: fork is not available on this platform
jRuby中是否有其他解决方案?
Is there any alternative solution for this in jRuby?
谢谢.
推荐答案
我找到了解决方案.我们可以使用JRuby中的内置库FFI在MRI中模拟" Process.fork.
I found out the solution for this. We can use the built-in library FFI in JRuby to 'simulate' the Process.fork in MRI.
# To mimic the Process.fork in MRI Ruby
module JRubyProcess
require 'ffi'
extend FFI::Library
ffi_lib FFI::Library::LIBC
attach_function :fork, [], :int
end
pid = JRubyProcess.fork do
#internal_server.run
end
更多详细信息:
http://blog.headius.com/2008/10/ffi-for-ruby-now-available.html
这篇关于在jRuby中使用"fork"生成进程的替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文