473,386 Members | 1,702 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,386 software developers and data experts.

How to send a var to stdin of an external software

Hi the list,

I need to send a var to stdin of an external soft ("cat" command for
example).

How can I do this ? I would like a function like that :

theFunction ('cat -', stdin=myVar)

I don't need to get any return value.

Another related question : Is there's a limitation of var size ? I would
have var up to 10 MB.

Thanks !

Ben

Mar 13 '08 #1
7 2926
Benjamin Watine <wa****@cines.fr>:
How can I do this ? I would like a function like that :

theFunction ('cat -', stdin=myVar)

Another related question : Is there's a limitation of var size ? I
would have var up to 10 MB.
import subprocess
myVar = '*' * 10000000
cat = subprocess.Popen('cat',shell = True,stdin = subprocess.PIPE)
cat.stdin.write(myVar)
cat.stdin.close()
cat.wait()
Marko

--
Marko Rauhamaa mailto:ma***@pacujo.net http://pacujo.net/marko/
Mar 13 '08 #2
Benjamin Watine <wa****@cines.frwrote:
>How can I do this ? I would like a function like that :

theFunction ('cat -', stdin=myVar)

I don't need to get any return value.
http://docs.python.org/lib/node534.html says this is spelt

myVar = subprocess.Popen(["cat", "-"], stdout=subprocess.PIPE).communicate()[0]

(Probably not obvious how to find this if you've not come across the
backtick notation in shell or Perl.)

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Mar 13 '08 #3
Marko Rauhamaa a écrit :
Benjamin Watine <wa****@cines.fr>:
>How can I do this ? I would like a function like that :

theFunction ('cat -', stdin=myVar)

Another related question : Is there's a limitation of var size ? I
would have var up to 10 MB.

import subprocess
myVar = '*' * 10000000
cat = subprocess.Popen('cat',shell = True,stdin = subprocess.PIPE)
cat.stdin.write(myVar)
cat.stdin.close()
cat.wait()
Marko
Thank you Marko, it's exactly what I need.

And if somebody need it : to get the stdout in a var (myNewVar), not in
the shell :

cat = subprocess.Popen('cat', shell = True, stdin = subprocess.PIPE,
stdout=subprocess.PIPE)
cat.stdin.write(myVar)
cat.stdin.close()
cat.wait()
myNewVar = cat.stdout.read()

Is it correct ?

Ben
Mar 13 '08 #4
Benjamin Watine wrote:
And if somebody need it : to get the stdout in a var (myNewVar), not in
the shell :

cat = subprocess.Popen('cat', shell = True, stdin = subprocess.PIPE,
stdout=subprocess.PIPE)
cat.stdin.write(myVar)
cat.stdin.close()
cat.wait()
myNewVar = cat.stdout.read()

Is it correct ?
No, not really. It is prone to deadlock. The external program might
work by iteratively reading a little input and writing a little
output, as 'cat' almost surely does. If the size of myVar exceeds
the buffer space in cat and the pipes, you get stuck.

Your Python program can block at "cat.stdin.write(myVar)", waiting
for cat to read from its input pipe, while cat blocks at a write
to its output stream, waiting for you to start reading and freeing
up buffer space. Pipe loops are tricky business.

Popular solutions are to make either the input or output stream
a disk file, or to create another thread (or process) to be an
active reader or writer.
--
--Bryan
Mar 13 '08 #5
I wrote:
[...] Pipe loops are tricky business.

Popular solutions are to make either the input or output stream
a disk file, or to create another thread (or process) to be an
active reader or writer.
Or asynchronous I/O. On Unix-like systems, you can select() on
the underlying file descriptors. (MS-Windows async mechanisms are
not as well exposed by the Python standard library.)
--
--Bryan
Mar 13 '08 #6
Bryan Olson a écrit :
I wrote:
>[...] Pipe loops are tricky business.

Popular solutions are to make either the input or output stream
a disk file, or to create another thread (or process) to be an
active reader or writer.

Or asynchronous I/O. On Unix-like systems, you can select() on
the underlying file descriptors. (MS-Windows async mechanisms are
not as well exposed by the Python standard library.)
Hi Bryan

Thank you so much for your advice. You're right, I just made a test with
a 10 MB input stream, and it hangs exactly like you said (on
cat.stdin.write(myStdin))...

I don't want to use disk files. In reality, this script was previously
done in bash using disk files, but I had problems with that solution
(the files wasn't always cleared, and sometimes, I've found a part of
previous input at the end of the next input.)

That's why I want to use python, just to not use disk files.

Could you give me more information / examples about the two solutions
you've proposed (thread or asynchronous I/O) ?

Thank you !

Ben
Mar 14 '08 #7
On Mar 14, 11:37 am, Benjamin Watine <wat...@cines.frwrote:
Bryan Olson a écrit :
I wrote:
[...] Pipe loops are tricky business.
Popular solutions are to make either the input or output stream
a disk file, or to create another thread (or process) to be an
active reader or writer.
Or asynchronous I/O. On Unix-like systems, you can select() on
the underlying file descriptors. (MS-Windows async mechanisms are
not as well exposed by the Python standard library.)

Hi Bryan

Thank you so much for your advice. You're right, I just made a test with
a 10 MB input stream, and it hangs exactly like you said (on
cat.stdin.write(myStdin))...

I don't want to use disk files. In reality, this script was previously
done in bash using disk files, but I had problems with that solution
(the files wasn't always cleared, and sometimes, I've found a part of
previous input at the end of the next input.)

That's why I want to use python, just to not use disk files.

Could you give me more information / examples about the two solutions
you've proposed (thread or asynchronous I/O) ?
The source code of the subprocess module shows how to do it with
select IIRC. Look at the implementation of the communicate() method.

Regards
Floris
Mar 14 '08 #8

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

Similar topics

5
by: Jean-Pierre Bergamin | last post by:
Dear python-Community We are forced to use a quite old simulation software that's based on Modula-2. The idea is now to let this software "talk" to the outside world over a TCP/IP network. ...
18
by: mountain man | last post by:
Greetings to all database professionals and laymen, Let us make a bold assumption that we have developed a software tool for the SQL Server environment which simply acts as an interface between...
12
by: Charles Law | last post by:
This is a bit of a vague question, but I am just starting on this, and wonder if anyone has ideas of where to start. I have a program that controls some external equipment. It sends messages in...
7
by: ~toki | last post by:
I have a full work app (server and client) that talk between two executables trought Console.WriteLine(); & Console.ReadLine(); I have the source code of the server. I need to make the client....
0
by: David Parker | last post by:
I have built slony 1.0.2 with postgres 7.4.5. This needs to be deployed on a system other than the one on which is was built. Somebody on our team had earlier gotten around the problem of not being...
1
by: KrishnaGovind | last post by:
Hi, I am new to this group. plz send c,c++ software plz...............
0
by: jfigueiras | last post by:
>I have a problem with the module subprocess! As many other programs... I'm not sure what you mean by "non-standard file descriptors". The other program is free to open, read, write, etc any...
0
by: Alfons Nonell-Canals | last post by:
Dear all, I have a complex program developed using Python. It contains lot of files and classes. Now I have the lazy task to documentate it. I have some notes about is class but I would like to...
4
by: joydeepdg | last post by:
I want to know how can I execute external softwares from within my java code.Actually what my java code does is that it takes a voice input from user and converts it into a .wav file.Then it has to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.