launch external program

Discussion in 'Ruby' started by Andrey Demidov, Jun 17, 2009.

  1. Hi all,

    I'm running external programs like this:

    status = `rdesktop .... &`

    But my ruby script doesn't work until the external program finished.
    How can I run an external program asynchronously?

    Thanks.
    --
    Posted via http://www.ruby-forum.com/.
     
    Andrey Demidov, Jun 17, 2009
    #1
    1. Advertising

  2. [Note: parts of this message were removed to make it a legal post.]

    system("command")

    You cannot save the output, though.

    On Wed, Jun 17, 2009 at 8:08 AM, Andrey Demidov <>wrote:

    > Hi all,
    >
    > I'm running external programs like this:
    >
    > status = `rdesktop .... &`
    >
    > But my ruby script doesn't work until the external program finished.
    > How can I run an external program asynchronously?
    >
    > Thanks.
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Christopher Carver, Jun 17, 2009
    #2
    1. Advertising

  3. Hi,

    Am Mittwoch, 17. Jun 2009, 22:22:49 +0900 schrieb Christopher Carver:
    > On Wed, Jun 17, 2009 at 8:08 AM, Andrey Demidov <>wrote:
    > > I'm running external programs like this:
    > >
    > > status = `rdesktop .... &`
    > >
    > > But my ruby script doesn't work until the external program finished.
    > > How can I run an external program asynchronously?
    > >

    > system("command")
    >
    > You cannot save the output, though.


    Yes, you can.

    output = ""
    t = Thread.new { output << `somecmd` }
    ...
    t.alive? or puts "The full output is:", output

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
     
    Bertram Scharpf, Jun 17, 2009
    #3
  4. On 17.06.2009 16:02, Bertram Scharpf wrote:
    > Am Mittwoch, 17. Jun 2009, 22:22:49 +0900 schrieb Christopher Carver:
    >> On Wed, Jun 17, 2009 at 8:08 AM, Andrey Demidov <>wrote:
    >>> I'm running external programs like this:
    >>>
    >>> status = `rdesktop .... &`
    >>>
    >>> But my ruby script doesn't work until the external program finished.
    >>> How can I run an external program asynchronously?
    >>>

    >> system("command")


    Christopher, system does not execute commands asynchronously.

    >> You cannot save the output, though.

    >
    > Yes, you can.


    Obama? :)

    > output = ""
    > t = Thread.new { output << `somecmd` }
    > ...
    > t.alive? or puts "The full output is:", output


    You can do this as well:

    t = Thread.new { `somecmd` }
    ....
    output = t.value

    It seems Thread#value is rarely used but it can be of very useful.

    http://ruby-doc.org/core-1.8.7/classes/Thread.html#M000243

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Jun 17, 2009
    #4
  5. Robert Klemme wrote:
    > On 17.06.2009 16:02, Bertram Scharpf wrote:
    >> output = ""
    >> t = Thread.new { output << `somecmd` }
    >> ...
    >> t.alive? or puts "The full output is:", output

    >
    > You can do this as well:
    >
    > t = Thread.new { `somecmd` }
    > ...
    > output = t.value


    Thread#value blocks while the thread is running, though.

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Jun 17, 2009
    #5
  6. Andrey Demidov

    Jeff Bowen Guest

    Joel VanderWerf wrote:
    > Robert Klemme wrote:
    >> output = t.value

    > Thread#value blocks while the thread is running, though.


    Ok but how would you in Windows start a seperate program like a vendor
    software that runs seperate from the ruby program? If I try
    `run_me.exe` or
    system("run_me.exe") the ruby program just sits waiting for the other to
    finsh?
    --
    Posted via http://www.ruby-forum.com/.
     
    Jeff Bowen, Jun 18, 2009
    #6
  7. 2009/6/17 Joel VanderWerf <>:
    > Robert Klemme wrote:
    >>
    >> On 17.06.2009 16:02, Bertram Scharpf wrote:
    >>>
    >>> =A0output =3D ""
    >>> =A0t =3D Thread.new { output << `somecmd` }
    >>> =A0...
    >>> =A0t.alive? or puts "The full output is:", output

    >>
    >> You can do this as well:
    >>
    >> t =3D Thread.new { `somecmd` }
    >> ...
    >> output =3D t.value

    >
    > Thread#value blocks while the thread is running, though.


    Well, yes. But what is your point? If you want the result you need
    to wait anyway. If not you can still use your pattern:

    t.alive? or puts "The full output is:", t.value

    Kind regards

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Jun 18, 2009
    #7
  8. Hi,

    Am Donnerstag, 18. Jun 2009, 05:45:08 +0900 schrieb Robert Klemme:
    > On 17.06.2009 16:02, Bertram Scharpf wrote:
    >> output = ""
    >> t = Thread.new { output << `somecmd` }
    >> ...
    >> t.alive? or puts "The full output is:", output

    >
    > You can do this as well:
    >
    > t = Thread.new { `somecmd` }
    > ...
    > output = t.value


    Gosh, that's the programming style I was looking for.

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
     
    Bertram Scharpf, Jun 18, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul J. Lucas
    Replies:
    7
    Views:
    818
    Thomas Hawtin
    Sep 14, 2005
  2. joshivaibhav
    Replies:
    3
    Views:
    673
    joshivaibhav
    Nov 21, 2006
  3. Yao Qi
    Replies:
    30
    Views:
    1,400
    Roedy Green
    Jul 4, 2007
  4. Replies:
    6
    Views:
    303
    Grant Edwards
    Sep 12, 2007
  5. rihad

    re-launch piped external program

    rihad, Sep 21, 2007, in forum: Perl Misc
    Replies:
    14
    Views:
    195
    Mumia W.
    Sep 25, 2007
Loading...

Share This Page