开发者

system and fork calls are blocking the port 3000

I am using ruby 1.8.7 and rails 2.3.4 . I am developing a plugin so I don't have too much of leeway.

In my controller I need to invoke a rake task. The rake task will take longer to finish so I am following the approach mentioned in Railscast which is

system "rake #{task} &"

This solution works great and everything is fine. I know this solution will not work on windows 开发者_如何学Goand I'm fine with that.

I started my server at port 3000. The controller was invoked which fired the rake task in the background. However if I ctrl +c my script/server and if I try to restart the server then I get this error.

Address already in use - bind(2) (Errno::EADDRINUSE)

Then I changed my code to do this

fork do
 system "rake #{task} &"
end

Still the same issue.

Does anyone how do I get around this problem of port 3000 getting blocked. Also any explanation of why rake task is blocking port 3000 would help.


From ruby-docs:

Kernel.fork [{ block }] => fixnum or nil

Process.fork [{ block }] => fixnum or nil

Creates a subprocess. If a block is specified, that block is run in the subprocess, and the subprocess terminates with a status of zero. Otherwise, the fork call returns twice, once in the parent, returning the process ID of the child, and once in the child, returning nil. The child process can exit using Kernel.exit! to avoid running any at_exit functions. The parent process should use Process.wait to collect the termination statuses of its children or use Process.detach to register disinterest in their status; otherwise, the operating system may accumulate zombie processes.

The thread calling fork is the only thread in the created child process. fork doesn‘t copy other threads. Final solution based on comments below:

command = "rake #{task} #{args.join(' ')}" 
p1 = Process.fork { system(command) } 
Process.detach(p1)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜