Ruby thread or process expiration in Ruby

I can approach this in the wrong direction, so any help would be appreciated.

I have a Ruby script that, among other things, runs an executable file. I want to run this executable file - it is currently launched using the system "", and then continue to work with the script. When the script ends, I want it to exit, but leave the executable file running.

I originally had the following

# Do some work # Start the executable system("executable_to_run.exe") # Continue working 

But executable_to_run.exe is an executable lock file, and the system "" will not be completed until the executable completes execution (which I do not want)

So now I have something like this (abbreviated)

 # Do some work # Start the executable on it one thread Thread.new do system("executable_to_run.exe") end # Continue working 

This works well, as my script can continue to work while the thread runs the executable in the background. Unfortunately, when my script comes to the exit, the executable thread is still working, and it will not exit until the thread can exit. If I kill the executable, the thread will end and the script will exit.

So what I need to do is run "executable_to_run.exe" and just leave it in the background.

I am using Ruby 1.8.7 on Windows, which means fork is not implemented. I cannot upgrade to 1.9, because there are internal and external dependencies of the commands that I need to resolve first (and which will not be executed in the near future).

I tried

  • Starting the process through the "start", but it still blocks
  • Calling Thread.kill on the executable thread but it still requires the executable to be killed

So this is what I can do in Ruby and I just missed something or am I having a problem because I cannot use Fork?

Thanks in advance

+6
source share
2 answers

A debugged answer should work on windows. This is a cross-platform platform:

 pid = spawn 'some_executable' Process.detach(pid) #tell the OS we're not interested in the exit status 
+4
source

I just tried and start does not block on Windows 7 x64 with Ruby 1.8.7.

 system 'start notepad' puts 'Exiting now...' 

It clearly depends on Windows.

+3
source

Source: https://habr.com/ru/post/885491/


All Articles