Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a exe Asynchronously with two arguments in ruby?

exe should run when i am open the page. Asynchronous process need to run.

Is there any way for run exe Asynchronously with two arguments in ruby?

i have tried ruby commands - system() , exec() but its waiting the process complete. i need to start exe with parameter no need to wait for the process complete

is any rubygems going to support for my issue?

like image 614
Jeyavel Avatar asked Apr 10 '12 16:04

Jeyavel


1 Answers

You can use Process.spawn and Process.wait2:

pid = Process.spawn 'your.exe', '--option'

# Later...
pid, status = Process.wait2 pid

Your program will be executed as a child process of the interpreter. Besides that, it will behave as if it had been invoked from the command line.

You can also use Open3.popen3:

require 'open3'
*streams, thread = Open3.popen3 'your.exe', '--option'

# Later...
streams.each &:close
status = thread.value

The main difference here is that you get access to three IO objects. The standard input, output and error streams of the process are redirected to them, in that order.

This is great if you intend to consume the output of the program, or communicate with it through its standard input stream. Text that would normally be printed on a terminal will instead be made available to your script.

You also get a thread which will wait for the program to finish executing, which is convenient and intuitive.

like image 189
Matheus Moreira Avatar answered Oct 13 '22 00:10

Matheus Moreira