Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here you have a working solution without using the <code>subprocess</code> module. Although, you could use it for the tee process while still using the <code>exec*</code> functions suite for your custom subprocess (just use <code>stdin=subprocess.PIPE</code> and then duplicate the descriptor to your stdout).</p> <pre><code>import os, time, sys pr, pw = os.pipe() pid = os.fork() if pid == 0: os.close(pw) os.dup2(pr, sys.stdin.fileno()) os.close(pr) os.execv('/usr/bin/tee', ['tee', 'log.txt']) else: os.close(pr) os.dup2(pw, sys.stdout.fileno()) os.close(pw) pid2 = os.fork() if pid2 == 0: # Replace with your custom process call os.execv('/usr/bin/yes', ['yes']) else: try: while True: time.sleep(1) except KeyboardInterrupt: pass </code></pre> <p>Note that the <code>tee</code> command, internally, does the same thing as Ben suggested in his answer: reading input and looping over output file descriptors while writing to them. It may be more efficient because of the optimized implementation and because it's written in C, but you have the overhead of the different pipes (don't know for sure which solution is more efficient, but in my opinion, reassigning a custom file-like object to <code>stdout</code> is a more elegant solution).</p> <p>Some more resources:</p> <ul> <li><a href="https://stackoverflow.com/questions/616645/how-do-i-duplicate-sys-stdout-to-a-log-file-in-python">How do I duplicate sys.stdout to a log file in python?</a></li> <li><a href="http://www.shallowsky.com/blog/programming/python-tee.html" rel="nofollow noreferrer">http://www.shallowsky.com/blog/programming/python-tee.html</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload