Use capture_both with Feeder
Hello! Would it be possible to use the feeder with capture_both
? I would like to store the result in a variable until I can display or return it manually! My current code is something like this:
from sarge import Feeder, get_stdout, capture_both
feeder = Feeder()
for line in capture_both("cowsay", input=feeder, async_=True).stdout:
print(line.decode("utf-8").rstrip())
feeder.feed(get_stdout("fortune"))
feeder.close()
Thank you kindly for the help!
Comments (8)
-
repo owner -
reporter Essentially, all I want to do is store the resultant value for later use, perhaps as a generator or similar, as I can't store the result of
run()
. -
repo owner The resultant value of what?
capture_both
andrun
both returnPipeline
instances. Why can't you useget_both
if you just want the final output streams? -
reporter But does
feeder
not provide an alternative to piping commands, such ascommand 1 | command 2
? In addition, if I wanted to save memory, wouldn’tcapture_both
be better? I do apologize; I may be completely misunderstanding the use offeeder
. -
repo owner I may be completely misunderstanding the use of
feeder
.Perhaps you are - I can't really say.
Feeder
is I believe sufficiently documented, so I propose to close this issue as Invalid because I can’t see what enhancement you are looking for. Perhaps you can re-frame it so that it makes more sense to me? Or try asking on the forum. -
reporter Alright, so I’ll try one last time before heading to the forum:
So as far as I can tell, you can use
Feeder
to pass in large amounts of input data to a child process, i.e. through thestdin
of the second process; however, you can only print it to the terminal using run, and not store the result of the combined commands, such storing the result offortune | cowsay
, for later formatting. I seeFeeder
as a more efficient and safer way of piping than the abovefortune | cowsay
, preventing premature closure of pipes before all the data has been passed to the child / second process. Am I correct in this statement? -
repo owner It's not intended to replace
fortune | cowsay
- it's where you need to generate data in some other way (e.g. programmatically) to feed to a child process. Otherwise you could just build a command line using e.g.fortune | cowsay | your-script
andrun
that (orcapture_both
, orget_both
). If you want to store results, use a two-step process e.g.get_both
withfortune | cowsay
to get your intermediate data, and then pass that toyour-script
, possibly usingFeeder
. Otherwise (to access the data a bit at a time), you would have to use piecemeal reading of thestdout
of thefortune | cowsay
pipeline. -
reporter Hmm... I'm currently using
sarge
to create a module which essentially runs shell programs as functions, similar to http://amoffat.github.io/sh/; to implement piping, I am required to attach pipes to each argument using string manipulation; wouldFeeder
be a suitable alternative, if my situation counts as programmatically? - Log in to comment
I’m not sure I quite get what you mean. Can you show a snippet which illustrates your desired usage, even if it doesn’t work at the moment?