Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > popen pipe limit

Reply
Thread Tools

popen pipe limit

 
 
skunkwerk
Guest
Posts: n/a
 
      04-07-2008
I'm getting errors when reading from/writing to pipes that are fairly
large in size. To bypass this, I wanted to redirect output to a file
in the subprocess.Popen function, but couldn't get it to work (even
after setting Shell=True). I tried adding ">","temp.sql" after the
password field but mysqldump gave me an error.

the code:
p1 = subprocess.Popen(["mysqldump","--all-databases","--user=user","--
password=password"], shell=True)
p2 = subprocess.Popen(["gzip","-9"], stdin=p1.stdout)
output = p2.communicate()[0]
file=open('test.sql.gz','w')
file.write(str(output))
file.close()

the output:
gzip: compressed data not written to a terminal. Use -f to force
compression.
For help, type: gzip -h
mysqldump: Got errno 32 on write

I'm using python rather than a shell script for this because I need to
upload the resulting file to a server as soon as it's done.

thanks
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      04-08-2008
En Mon, 07 Apr 2008 20:52:54 -0300, skunkwerk <>
escribió:

> I'm getting errors when reading from/writing to pipes that are fairly
> large in size. To bypass this, I wanted to redirect output to a file
> in the subprocess.Popen function, but couldn't get it to work (even
> after setting Shell=True). I tried adding ">","temp.sql" after the
> password field but mysqldump gave me an error.
>
> the code:
> p1 = subprocess.Popen(["mysqldump","--all-databases","--user=user","--
> password=password"], shell=True)
> p2 = subprocess.Popen(["gzip","-9"], stdin=p1.stdout)
> output = p2.communicate()[0]
> file=open('test.sql.gz','w')
> file.write(str(output))
> file.close()


You need a pipe to chain subprocesses:

import subprocess
p1 =
subprocess.Popen(["mysqldump","--all-databases","--user=user","--password=password"],
stdout=subprocess.PIPE)
ofile = open("test.sql.gz", "wb")
p2 = subprocess.Popen(["gzip","-9"], stdin=p1.stdout, stdout=ofile)
p1.wait()
p2.wait()
ofile.close()

If you don't want the final file on disk:

p1 =
subprocess.Popen(["mysqldump","--all-databases","--user=user","--password=password"],
stdout=subprocess.PIPE)
p2 = subprocess.Popen(["gzip","-9"], stdin=p1.stdout,
stdout=subprocess.PIPE)
while True:
chunk = p2.stdout.read(4192)
if not chunk: break
# do something with read chunk

p1.wait()
p2.wait()

--
Gabriel Genellina

 
Reply With Quote
 
 
 
 
skunkwerk
Guest
Posts: n/a
 
      04-10-2008
On Apr 7, 6:17*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar> wrote:
> En Mon, 07 Apr 2008 20:52:54 -0300,skunkwerk<skunkw...@gmail.com> *
> escribió:
>
> > I'm getting errors when reading from/writing to pipes that are fairly
> > large in size. *To bypass this, I wanted to redirect output to a file
> > in the subprocess.Popen function, but couldn't get it to work (even
> > after setting Shell=True). *I tried adding ">","temp.sql" after the
> > password field but mysqldump gave me an error.

>
> > the code:
> > p1 = subprocess.Popen(["mysqldump","--all-databases","--user=user","--
> > password=password"], shell=True)
> > p2 = subprocess.Popen(["gzip","-9"], stdin=p1.stdout)
> > output = p2.communicate()[0]
> > file=open('test.sql.gz','w')
> > file.write(str(output))
> > file.close()

>
> You need a pipe to chain subprocesses:
>
> import subprocess
> p1 = *
> subprocess.Popen(["mysqldump","--all-databases","--user=user","--password=password"], *
> stdout=subprocess.PIPE)
> ofile = open("test.sql.gz", "wb")
> p2 = subprocess.Popen(["gzip","-9"], stdin=p1.stdout, stdout=ofile)
> p1.wait()
> p2.wait()
> ofile.close()
>
> If you don't want the final file on disk:
>
> p1 = *
> subprocess.Popen(["mysqldump","--all-databases","--user=user","--password=password"], *
> stdout=subprocess.PIPE)
> p2 = subprocess.Popen(["gzip","-9"], stdin=p1.stdout, *
> stdout=subprocess.PIPE)
> while True:
> * *chunk = p2.stdout.read(4192)
> * *if not chunk: break
> * *# do something with read chunk
>
> p1.wait()
> p2.wait()
>
> --
> Gabriel Genellina


thanks Gabriel - tried the first one and it worked great!
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Popen: NameError: name 'PIPE' is not defined Mathieu Prevot Python 3 05-24-2008 12:56 PM
Popen pipe hang schickb Python 0 05-13-2008 12:35 AM
Re: subprocess.Popen and replacing the shell pipe line jepler@unpythonic.net Python 1 09-23-2005 08:28 AM
subprocess.Popen and replacing the shell pipe line Tom Brown Python 0 09-22-2005 10:38 PM
[named pipe] i wanna know about validate of pipe handle of client lee, wonsun C++ 1 11-02-2004 04:29 AM



Advertisments