Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8053061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:44:04+00:00 2026-06-05T07:44:04+00:00

I am trying to create a stream object that triggers a callback function any

  • 0

I am trying to create a stream object that triggers a callback function any time data is written to it.

class MonitoredStream():
    def __init__(self, outstream, callback):
        self.outstream = outstream
        self.callback = callback

    def write(self, s):
        self.callback(s)
        self.outstream.write(s)

    def __getattr__(self, attr):
        return getattr(self.outstream, attr)

This works fine when I call the write method directly, but I would love to have it work also when I have a subprocess’ output hooked to the stream. For example:

def f(s):
    print("Write")

p = sub.Popen(["sh", "test.sh"], stdout=MonitoredStream(sys.stdout, f))
p.communicate()

This just sends output directly to sys.stdout, bypassing the write function completely. Is there a way that I can monitor this output also?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:44:05+00:00Added an answer on June 5, 2026 at 7:44 am

    I believe the issue here is that subprocess.Popen doesn’t use the Python interface to the pipe – it instead gets the file descriptor and then uses that to write to the pipe directly, which, as you give the attributes of the stdout pipe, means it uses that, bypassing your code.

    My best guess at solving this is to make a new in-between pipe that sits in the middle to let you deal with the stream yourself. I would implement this as a context manager:

    import sys
    import os
    from subprocess import Popen
    from contextlib import contextmanager
    
    @contextmanager
    def monitor(stream, callback):
        read, write = os.pipe()
        yield write
        os.close(write)
        with os.fdopen(read) as f:
            for line in f:
                callback(line)
                stream.write(line)
    
    def f(s):
        print("Write")
    
    with monitor(sys.stdout, f) as stream:
        p = Popen(["ls"], stdout=stream)
        p.communicate()
    

    Although you could, of course, still use a class:

    import sys
    import os
    from subprocess import Popen
    
    class MonitoredStream():
        def __init__(self, stream, callback):
            self.stream = stream
            self.callback = callback
            self._read, self._write = os.pipe()
    
        def fileno(self):
            return self._write
    
        def process(self):
            os.close(self._write)
            with os.fdopen(self._read) as f:
                for line in f:
                    self.callback(line)
                    self.stream.write(line)
    
    def f(s):
        print("Write")
    
    stream = MonitoredStream(sys.stdout, f)
    p = Popen(["ls"], stdout=stream)
    p.communicate()
    print(stream.process())
    

    Although I feel this is less elegant.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a class that will contain functions for serializing/deserializing objects to/from
I'm trying to recreate a SyndicationFeed object (System.ServiceModel.Syndication) from XML data that has been
Trying to create a mapper for an Microsoft Office object to POCO's and found
Trying to create COM -object on server. code is: var myGuid = new Guid(530A1815-820C-11D3-BBB7-008048DE406A);
I want to create a helper method that can take in ANY type of
I'm trying to post data to a page that handles it for me. I
I'm trying to create and use a .NET StreamWriter object inside a classic ASP
Am trying to do update a clob column using a connection object that is
I'm trying to create a simple application that does a HTTP request/response on a
I'm currently trying create a service that will return results of a OLAP cube

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.