473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

determine if os.system() is done

suppose i'm calling two system processes, one to unzip, and one to
“tail” to get the last line. How can i determine when the first
process is done?

Example:

subprocess.Popen([r"/sw/bin/gzip","-d","access_log.4.gz"]);

last_line=subprocess.Popen([r"/usr/bin/tail","-n 1","access_log.4"],
stdout=subprocess.PIPE).communicate()[0]

of course, i can try workarounds something like os.system("gzip -d
thiss.gz && tail thiss"), but i wish to know if there's non-hack way to
determine when a system process is done.

Xah
xa*@xahlee.org
http://xahlee.org/

Sep 7 '05 #1
24 2207
Xah Lee wrote:
of course, i can try workarounds something like os.system("gzip -d
thiss.gz && tail thiss"), but i wish to know if there's non-hack way to
determine when a system process is done.


Well, if you use a function of the "popen" family, you get some kind of
return value from the subprocess on your "output" pipe. You should be able
to determine if your subprocess has terminated by examining (parsing) this
output pipe.

If you use os.system(), you should get a single return value (usually "None"
or a error code) that you can use for this task.

In both cases, you may have to use a loop (sleep(x)) to wait for the return
value (and check it) from within your code.

Have a look at the docu of the os.process module for details. (Maybe the
newer "subprocess" module is a better choice...)

HTH

-----------------------------------
Alessandro Bottoni
Sep 7 '05 #2
"Xah Lee" <xa*@xahlee.org> writes:
suppose i'm calling two system processes, one to unzip, and one to
tail to get the last line. How can i determine when the first
process is done? Example: subprocess.Popen([r"/sw/bin/gzip","-d","access_log.4.gz"]); last_line=subprocess.Popen([r"/usr/bin/tail","-n 1","access_log.4"],
stdout=subprocess.PIPE).communicate()[0] of course, i can try workarounds something like os.system("gzip -d
thiss.gz && tail thiss"), but i wish to know if there's non-hack way to
determine when a system process is done.


Have you tried reading the manual for the subprocess module? You
just *might* find the answer to your question if you look at what
you can do with Popen objects. Actually, just learning about the
exact semantics of the communicate() method might be enought to
solve your problem.
--
Thomas Bellman, Lysator Computer Club, Linkping University, Sweden
"I refuse to have a battle of wits with an ! bellman @ lysator.liu.se
unarmed person." ! Make Love -- Nicht Wahr!

Sep 7 '05 #3
Thomas Bellman wrote:
Have you tried reading the manual for the subprocess module?


han har frskt, men hans tourette tog verhanden:

http://mail.python.org/pipermail/pyt...er/297642.html

</F>

Sep 7 '05 #4
Thomas Bellman wrote:
"Xah Lee" <xa*@xahlee.org> writes:
suppose i'm calling two system processes, one to unzip, and one to
tail to get the last line. How can i determine when the first
process is done?

Example:

subprocess.Popen([r"/sw/bin/gzip","-d","access_log.4.gz"]);

last_line=subprocess.Popen([r"/usr/bin/tail","-n 1","access_log.4"],
stdout=subprocess.PIPE).communicate()[0]

of course, i can try workarounds something like os.system("gzip -d
thiss.gz && tail thiss"), but i wish to know if there's non-hack way to
determine when a system process is done.


Have you tried reading the manual for the subprocess module? You
just *might* find the answer to your question if you look at what
you can do with Popen objects.

Oh, come on. Don't you know that all Python documentation is rubbish
and not worth reading, written by IT idiots who throw around useless
jargon and indulge in extreme forms of self-gratification? Someone of
the caliber of Xah Lee would *never* stoop so low as to actually read
the documentation. It is beneath him. Instead, he posts messages to a
group of IT idiots who throw around useless jargon and indulge in
extreme forms of self-gratification in posting answers to questions.

<snip>

JMJ
Sep 7 '05 #5
Xah Lee wrote:
suppose i'm calling two system processes, one to unzip, and one to
“tail” to get the last line. How can i determine when the first
process is done?

Example:

subprocess.Popen([r"/sw/bin/gzip","-d","access_log.4.gz"]);

last_line=subprocess.Popen([r"/usr/bin/tail","-n 1","access_log.4"],
stdout=subprocess.PIPE).communicate()[0]

of course, i can try workarounds something like os.system("gzip -d
thiss.gz && tail thiss"), but i wish to know if there's non-hack way to
determine when a system process is done.

Xah
xa*@xahlee.org
http://xahlee.org/


I think the idea is you wait for the first call to subprocess.call to
finish before executing the second...

http://docs.python.org/lib/node231.html
call( *args, **kwargs)
Run command with arguments. *Wait for command to complete*, then
return the returncode attribute.

The arguments are the same as for the Popen constructor. Example:

retcode = call(["ls", "-l"])

Sep 7 '05 #6
Yeah, I agree. The Python documentation just merey describes what
arguements a function can take not as much how to use the actual
function.

Sep 7 '05 #7
"Nainto" <Na****@gmail.com> wrote:
Yeah, I agree. The Python documentation just merey describes what
arguements a function can take not as much how to use the actual
function.


yeah, that's a really relevant criticism when we're talking about a
module that contains one function and one class, and for which the
documentation contains *sixteen* examples.

</F>

Sep 7 '05 #8
[Fredrik Lundh]
han har frskt, men hans tourette tog verhanden:


IMHO it's more likely an Asperger's syndrome.

http://en.wikipedia.org/wiki/Asperger_Syndrome

--
Lars Gustbel
la**@gustaebel.de

Any sufficiently advanced technology is indistinguishable
from magic.
(Arthur C. Clarke)
Sep 7 '05 #9
Xah Lee wrote:
suppose i'm calling two system processes, one to unzip, and one to
“tail” to get the last line. How can i determine when the first
process is done?

Example:

subprocess.Popen([r"/sw/bin/gzip","-d","access_log.4.gz"]);

last_line=subprocess.Popen([r"/usr/bin/tail","-n 1","access_log.4"],
stdout=subprocess.PIPE).communicate()[0]

of course, i can try workarounds something like os.system("gzip -d
thiss.gz && tail thiss"), but i wish to know if there's non-hack way to
determine when a system process is done.

Xah
xa*@xahlee.org
http://xahlee.org/


As far as I can tell from the docs (worth reading),
system(command) doesn't return until the process has completed.
It would have to do some fancy footwork to return the exit code
BEFORE the process had completed!

Steve
Sep 7 '05 #10
Lars Gustbel wrote:
[Fredrik Lundh]
han har frskt, men hans tourette tog verhanden:

IMHO it's more likely an Asperger's syndrome.

http://en.wikipedia.org/wiki/Asperger_Syndrome


I disagree, in his writings I found no evidence of autisme.
Actually most of it can be classified as being stubborn against better
knowledge, what actually a common thing is.

But he does ask the question and by this does admits he is not
knowledged in that topic.

The only thing I am disappointed at his writing style, most likely he
has a disrupted view on social acceptable behavior and communication.
These skills might be still in development, so perhaps it is reasonable
to give him a chance and wait until he is out of his puberty.

--
mph
Sep 7 '05 #11
Martin P. Hellwig wrote:
The only thing I am disappointed at his writing style, most likely he
has a disrupted view on social acceptable behavior and communication.
These skills might be still in development, so perhaps it is reasonable
to give him a chance and wait until he is out of his puberty.


He's 37 years old! How long should one be given to mature?

-Peter
Sep 8 '05 #12
Thanks all.

I found the answer, rather easily.
To make a system call and wait for it, do:

subprocess.Popen([r"/sw/bin/gzip","-d","access_log.4.gz"]).wait();

------
this post is archived at:
http://xahlee.org/perl-python/system_calls.html

Xah
xa*@xahlee.org
http://xahlee.org/
Xah Lee wrote:
suppose i'm calling two system processes, one to unzip, and one to
“tail” to get the last line. How can i determine when thefirst
process is done?

Example:

subprocess.Popen([r"/sw/bin/gzip","-d","access_log.4.gz"]);

last_line=subprocess.Popen([r"/usr/bin/tail","-n 1","access_log.4"],
stdout=subprocess.PIPE).communicate()[0]

of course, i can try workarounds something like os.system("gzip -d
thiss.gz && tail thiss"), but i wish to know if there's non-hack way to
determine when a system process is done.

Xah
xa*@xahlee.org
http://xahlee.org/


Sep 8 '05 #13
Peter Hansen wrote:
Martin P. Hellwig wrote:
The only thing I am disappointed at his writing style, most likely he
has a disrupted view on social acceptable behavior and communication.
These skills might be still in development, so perhaps it is
reasonable to give him a chance and wait until he is out of his puberty.

He's 37 years old! How long should one be given to mature?

Yeah well, eeh I guess that rules out the development part.
Although puberty can take a long time (according to my SO) :-)

--
mph

Sep 8 '05 #14
Xah Lee wrote:
suppose i'm calling two system processes, one to unzip, and one to
“tail” to get the las t line. How can i determine when the first process is done?

Example:

subprocess.Popen([r"/sw/bin/gzip","-d","access_log.4.gz"]);

last_line=subprocess.Popen([r"/usr/bin/tail","-n 1","access_log.4"],
stdout=subprocess.PIPE).communicate()[0]

of course, i can try workarounds something like os.system("gzip -d
thiss.gz && tail thiss"), but i wish to know if there's non-hack way to
determine when a system process is done.

Xah
xa*@xahlee.org
http://xahlee.org/

I know you've found the answer to your question, however for the exact
example you gave a much better solution comes to mind...
import gzip

log_file = gzip.open("access_log.4.gz")
last_line = log_file.readlines()[-1]
log_file.close()
Regards
Martin

Sep 8 '05 #15
Martin Franklin wrote:
import gzip
log_file = gzip.open("access_log.4.gz")
last_line = log_file.readlines()[-1]
log_file.close()


does the
log_file.readlines()[-1]
actually read all the lines first?

i switched to system call with tail because originally i was using a
pure Python solution

inF = gzip.GzipFile(ff, 'rb');
s=inF.readlines()
inF.close()
last_line=s[-1]

and since the log file is 100 megabytes it takes a long time and hogs
massive memory.

Xah
xa*@xahlee.org
http://xahlee.org/

Sep 8 '05 #16
Xah Lee wrote:
Martin Franklin wrote:
import gzip
log_file = gzip.open("access_log.4.gz")
last_line = log_file.readlines()[-1]
log_file.close()

does the
log_file.readlines()[-1]
actually read all the lines first?

Yes I'm afraid it does.


i switched to system call with tail because originally i was using a
pure Python solution

inF = gzip.GzipFile(ff, 'rb');
s=inF.readlines()
inF.close()
last_line=s[-1]

and since the log file is 100 megabytes it takes a long time and hogs
massive memory.

Ok, in that case stick to your shell based solution, although 100
megabytes does not sound that large to me I guess it is relative
to the system you are running on :) (I have over a gig of memory here)

Xah
xa*@xahlee.org
http://xahlee.org/


Sep 8 '05 #17
Martin Franklin wrote:
Xah Lee wrote:
Martin Franklin wrote:

import gzip
log_file = gzip.open("access_log.4.gz")
last_line = log_file.readlines()[-1]
log_file.close()

does the
log_file.readlines()[-1]
actually read all the lines first?


Yes I'm afraid it does.
i switched to system call with tail because originally i was using a
pure Python solution

inF = gzip.GzipFile(ff, 'rb');
s=inF.readlines()
inF.close()
last_line=s[-1]

and since the log file is 100 megabytes it takes a long time and hogs
massive memory.

Ok, in that case stick to your shell based solution, although 100
megabytes does not sound that large to me I guess it is relative
to the system you are running on :) (I have over a gig of memory here)


And just a few minutes after I sent that... this...

import gzip

logfile = gzip.open("access_log.4.BIG.gz")

## seek relative to the end of the file
logfile.seek(-500)

last_line = logfile.readlines()[-1]

logfile.close()

print last_line
Works quite fast on my machine...

Regards
Martin





Sep 8 '05 #18
Martin Franklin wrote:
Martin Franklin wrote:
Xah Lee wrote:

Martin Franklin wrote:

import gzip
log_file = gzip.open("access_log.4.gz")
last_line = log_file.readlines()[-1]
log_file.close()
does the
log_file.readlines()[-1]
actually read all the lines first?


Yes I'm afraid it does.

i switched to system call with tail because originally i was using a
pure Python solution

inF = gzip.GzipFile(ff, 'rb');
s=inF.readlines()
inF.close()
last_line=s[-1]

and since the log file is 100 megabytes it takes a long time and hogs
massive memory.

Ok, in that case stick to your shell based solution, although 100
megabytes does not sound that large to me I guess it is relative
to the system you are running on :) (I have over a gig of memory here)

And just a few minutes after I sent that... this...

import gzip

logfile = gzip.open("access_log.4.BIG.gz")

## seek relative to the end of the file
logfile.seek(-500)

last_line = logfile.readlines()[-1]

logfile.close()

print last_line
Works quite fast on my machine...


whoops, no it doesn't looking at wrong window :( just ignore
me please :)
Sep 8 '05 #19
Martin Franklin wrote:
Ok, in that case stick to your shell based solution, although 100
megabytes does not sound that large to me I guess it is relative
to the system you are running on :) (I have over a gig of memory here)


since the file is gzipped, you need to read all of it to get the last line.
however, replacing

last_line = logfile.readlines()[-1]

with a plain

for last_line in logfile: pass

avoids storing the entire file in memory.

(note that seek() actually reads the file in 1024 blocks until it gets
to the right location; reading via a line iterator only seems to be
marginally slower. zcat|tail is a LOT faster. ymmv.)

</F>

Sep 8 '05 #20
On Wed, 07 Sep 2005 23:28:13 -0400, rumours say that Peter Hansen
<pe***@engcorp.com> might have written:
Martin P. Hellwig wrote:
The only thing I am disappointed at his writing style, most likely he
has a disrupted view on social acceptable behavior and communication.
These skills might be still in development, so perhaps it is reasonable
to give him a chance and wait until he is out of his puberty.
He's 37 years old! How long should one be given to mature?


I (lots of female friends, actually :) believe many men remain in
puberty for longer than that.
--
TZOTZIOY, I speak England very best.
"Dear Paul,
please stop spamming us."
The Corinthians
Sep 8 '05 #21
Xah Lee wrote:
i switched to system call with tail because originally i was using a
pure Python solution

inF = gzip.GzipFile(ff, 'rb');
s=inF.readlines()
inF.close()
last_line=s[-1]

and since the log file is 100 megabytes it takes a long time and hogs
massive memory.


How about:

inF = gzip.GzipFile(ff, 'rb')
for line in inF:
pass
last_line = line

It's a bit slower than gzip and tail, but the memory usage is fine.
Sep 8 '05 #22
Fredrik Lundh wrote:
zcat|tail is a LOT faster.


and here's the "right way" to use that:

from subprocess import Popen, PIPE
p1 = Popen(["zcat", filename], stdout=PIPE)
p2 = Popen(["tail", "-1"], stdin=p1.stdout, stdout=PIPE)
last_line = p2.communicate()[0]

(on my small sample, this is roughly 15 times faster than the empty for loop)

</F>

Sep 8 '05 #23
isn't there a way to implement tail in python with the same class of
performance?

how's tail implemented?

Xah
xa*@xahlee.org
http://xahlee.org/

Fredrik Lundh wrote:
Fredrik Lundh wrote:
zcat|tail is a LOT faster.


and here's the "right way" to use that:

from subprocess import Popen, PIPE
p1 = Popen(["zcat", filename], stdout=PIPE)
p2 = Popen(["tail", "-1"], stdin=p1.stdout, stdout=PIPE)
last_line = p2.communicate()[0]

(on my small sample, this is roughly 15 times faster than the empty for loop)

</F>


Sep 8 '05 #24
Xah Lee wrote:
isn't there a way to implement tail in python with the same class of
performance?

how's tail implemented?:


Those crazy open source developers have an implementation here:
http://cvs.sourceforge.net/viewcvs.p....1&view=markup

It's not documented. It's pretty short though, I'm sure you won't have any
problems <strike>ripping it to pieces</strike> understanding it.

If it is a problem, a more documented version here (however I'm sorry, this
also by those open source/free software people you love to attack):

http://www.koders.com/c/fid8DEE98A42...4E25CF377.aspx

If you want something not by open source crazies (as you like to refer to
us), you may prefer this link:
http://minnie.tuhs.org/UnixTree/V7/u...md/tail.c.html

However that's even less documented (despite being not open source, just
"visible"), and more obfuscated.

As a compromise, you could always look at plan 9's implementation here:
* http://swtch.com/usr/local/plan9/src/cmd/tail.c

If you're after alternatives, you could always try here: (where all these
links came from)
http://www.google.com/search?q=tail.c

Fairly informative set of links provided by that highly non-obvious
search....

Hope that helps,

Best Regards,
Michael.

Sep 8 '05 #25

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
5
by: Hennie de Nooijer | last post by:
Hi, This is a diffcult issue to explain. I hope to make my problem clear to you. SITUATION I'm building A SLA Query for a customer. This customer has an awkward way to determine the SLA results...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
3
by: Web Webon | last post by:
Hi everybody! I wonder if this is possible? I need to determine if a client is using "windows classic folders" or anything else. If I instantiate a Shell ActiveX object is there a way of...
2
by: icodeurs2 | last post by:
I am interested in how to determine if a particular type of hardware has been plugged into Windows XP/2000 programmatically. It could be a service or application running that could detect the new...
2
by: Johannes | last post by:
When you do a webrequest like: Dim objWebRequest As WebRequest = WebRequest.Create(objURI) the returned class can be httpwebrequest, ftpwebrequest or any othe descendant webrequest type that is...
3
by: Developer in California | last post by:
I am working on developing a generic Web framework using Master Pages in ASP.NET 2.0. What I have done is created a PageRenderer class which has a public method which will retrieve the path of the...
9
by: | last post by:
I am interested in scanning web pages for content of interest, and then auto-classifying that content. I have tables of metadata that I can use for the classification, e.g. : "John P. Jones" "Jane...
3
by: Giampaolo Rodola' | last post by:
Hi, I'd like to know if there's a way to determine which is the best buffer size to use when you have to send() and recv() some data over the network. I have an FTP server application which, on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.