Use capture_both with Feeder

Issue #48 new
Jeet Ray created an issue

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)

  1. Vinay Sajip repo owner

    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?

  2. Jeet Ray 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().

  3. Vinay Sajip repo owner

    The resultant value of what? capture_both and run both return Pipeline instances. Why can't you use get_both if you just want the final output streams?

  4. Jeet Ray reporter

    But does feeder not provide an alternative to piping commands, such as command 1 | command 2? In addition, if I wanted to save memory, wouldn’t capture_both be better? I do apologize; I may be completely misunderstanding the use of feeder.

  5. Vinay Sajip 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.

  6. Jeet Ray 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 the stdin 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 of fortune | cowsay, for later formatting. I see Feeder as a more efficient and safer way of piping than the above fortune | cowsay, preventing premature closure of pipes before all the data has been passed to the child / second process. Am I correct in this statement?

  7. Vinay Sajip 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 and run that (or capture_both, or get_both). If you want to store results, use a two-step process e.g. get_both with fortune | cowsay to get your intermediate data, and then pass that to your-script, possibly using Feeder. Otherwise (to access the data a bit at a time), you would have to use piecemeal reading of the stdout of the fortune | cowsay pipeline.

  8. Jeet Ray 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; would Feeder be a suitable alternative, if my situation counts as programmatically?

  9. Log in to comment