Forum: Ruby exec (new process or new thread?) to continue

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ee6788c2faaad7a5cd0bc466c9ea60d8?d=identicon&s=25 aidy (Guest)
on 2009-02-24 14:20
(Received via mailing list)
Hi,

I have fired off an exe with the kernal method exec

loader_app = "#{Dir.getwd}/PublishUi.exe"
exec(loader_app)

The exec method seems to hold up the process

so

exec(loader_app)
puts "we got here"

Will not print "we got here" till I close the app.

I am not sure whether I should use a new thread or a new process here,
or whether there is an alternative.

Thanks

Aidy
990bf71a4e84e1145a3131f35656dc18?d=identicon&s=25 List Rb (listx300108u79872)
on 2009-02-24 14:30
(Received via mailing list)
On Feb 24, 2009, at 8:19 AM, aidy <aidy.lewis@googlemail.com> wrote:

>
> Aidy
>
>

IO.popen(an_exe)
E16e84e861c1815ce11ba7bd851c857d?d=identicon&s=25 lasitha (Guest)
on 2009-02-24 14:59
(Received via mailing list)
On Tue, Feb 24, 2009 at 6:59 PM, List.rb <list.rb@gmail.com> wrote:
> IO.popen(an_exe)
>

And with ruby 1.9, Kernel.spawn:
http://ruby-doc.org/core-1.9/classes/Kernel.html#M006071

Cheers,
lasitha.
6e366eb5a71be2bad7f383d42aeb4788?d=identicon&s=25 Justin Collins (Guest)
on 2009-02-24 15:00
(Received via mailing list)
aidy wrote:
>
> Aid
exec doesn't hold up the process, it replaces it completely[1]. Your
second example will never print "we got here".

Assuming you do not need to communicate with, wait on, or know anything
about the process you are starting, you can use fork[2] and exec this
way:

fork { exec load_app }

-Justin


[1] http://ruby-doc.org/core/classes/Kernel.html#M005979
[2] http://ruby-doc.org/core/classes/Kernel.html#M005980
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-02-24 21:07
(Received via mailing list)
aidy wrote:
> Hi,
>
> I have fired off an exe with the kernal method exec
>
> loader_app = "#{Dir.getwd}/PublishUi.exe"
> exec(loader_app)

Since you appear to be on windows, you can't fork, but you can do this:

Thread.new do
   loader_app = "#{Dir.getwd}/PublishUi.exe"
   system(loader_app)
end

# continue with other stuff here

This is a fairly cross-platform way to handle it. Of course on windows,
you can also do

system "start ..."

and then you don't even need a ruby thread.
2867f2ca8b9670f42868a41a309ecdb9?d=identicon&s=25 Colin Brumelle (cbrumelle)
on 2009-04-08 22:35
I like this pattern:

def fire_and_forget(&block)
  pid = fork do
    begin
      yield
    ensure
      Process.exit!
    end
  end
  Process.detach pid
end

A little bit safer then just using 'fork'...

Taken from a post here:
http://www.caboo.se/articles/2006/10/14/premcache-caching-an
d-precaching-with-memcached

Justin Collins wrote:
> Assuming you do not need to communicate with, wait on, or know anything
> about the process you are starting, you can use fork[2] and exec this
> way:
>
> fork { exec load_app }
>
> -Justin
>
>
> [1] http://ruby-doc.org/core/classes/Kernel.html#M005979
> [2] http://ruby-doc.org/core/classes/Kernel.html#M005980
This topic is locked and can not be replied to.