LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-08-2012, 02:32 AM   #1
fantasy1215
Member
 
Registered: Oct 2011
Posts: 75

Rep: Reputation: Disabled
lua popen how to wait until a long job had done?


In the lua, I want to use popen to execute a external binary file and want to get the output of the binary executable file.
But the external program does a long job, Will popen hang up the lua execution till the completely return of the external program?
If popen wouldn't hang up till the completely return of the external program, then How should I do to wait until the completely return of the external program?
 
Old 06-09-2012, 07:30 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by fantasy1215 View Post
In the lua, I want to use popen to execute a external binary file and want to get the output of the binary executable file.
But the external program does a long job, Will popen hang up the lua execution till the completely return of the external program?
If popen wouldn't hang up till the completely return of the external program, then How should I do to wait until the completely return of the external program?
Sounds like you need threads. Does Lua support threads ?
 
Old 06-09-2012, 09:43 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946
Quote:
Originally Posted by fantasy1215 View Post
Will popen hang up the lua execution till the completely return of the external program?
Yes. io.popen() will only return after the command has fully completed.

The other way, letting your Lua continue at the same time while the external program is working, is harder. It is easiest to achieve by redirecting the output of the external program to a file (supplying input to the command, i.e. handle = io.popen("command > path-to-temporary-file", "w")). You can check the outputs by opening and reading the temporary file in Lua. You can even use handle = io.popen("command > path-to-temporary-file ; touch path-to-marker-file", "w") so your Lua can just check for the existence of path-to-marker-file to see if the command has exited yet.
 
Old 06-10-2012, 01:18 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,201

Rep: Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392Reputation: 1392
Quote:
Originally Posted by Nominal Animal View Post
Yes. io.popen() will only return after the command has fully completed.
Um, I think not. Lua's popen is basically just a wrapper around C's popen, of which the manpage says
Quote:
The pclose() function waits for the associated process to terminate...
So clearly, popen() doesn't wait.
 
Old 06-10-2012, 03:53 PM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946
Quote:
Originally Posted by ntubski View Post
Um, I think not. Lua's popen is basically just a wrapper around C's popen, of which the manpage says

So clearly, popen() doesn't wait.
Absolutely right. I did mean pclose() will only return after the command has completed. popen() only starts the external command, of course.

Thanks for catching my error. I don't know what on earth I was thinking at the time.

Let me rephrase my answer to OP's question:
Quote:
Originally Posted by fantasy1215 View Post
Will popen hang up the lua execution till the completely return of the external program?
If popen wouldn't hang up till the completely return of the external program, then How should I do to wait until the completely return of the external program?
No, popen() will not. pclose() however will only return after the command has completed.

If you opened the command for reading its output ([FONT=Monospace]io.popen("command,"r")/FONT]), you must read it in a loop until no more output is available, then call pclose() , to be sure the command has completed. If you do not read the output, then the command may block. When you pclose() it, the command will fail with a "Broken pipe" (EPIPE) error.

If you opened the command for writing to its input (io.popen("command,"r")), you must first write the inputs to the command, then call pclose() to wait for it to complete.

If your Lua program wants to do other work while the command is being run, you need to write the command input to a temporary file if the command expects any, and run the command with its output redirected to another temporary file. That way your Lua script won't need to read or write to the handle at all while the command is running.

Even this way you can only be certain that the command has completed by calling pclose(). However, you can use a third temporary file as a marker, appending ; touch marker-file to the command, so your Lua script can simply check for the existence of that file now and then to see if the command is ready, so you can call pclose() without having it wait.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
New user can not login and wait for a long time trwu Red Hat 6 07-12-2011 10:29 AM
How long do I have to wait for thread approval? Kardell LQ Suggestions & Feedback 4 06-30-2009 08:40 AM
Very long wait time between searches colucix LQ Suggestions & Feedback 7 07-04-2008 02:05 PM
Hello... new to Linux, why did I wait so Long! markmowbray LinuxQuestions.org Member Intro 3 11-09-2007 06:39 AM
Reboot only after long wait Kroenecker Linux - Hardware 2 03-23-2004 07:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration