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 7498467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:29:14+00:00 2026-05-29T19:29:14+00:00

I would like to run a process with subprocess.Popen() and communicate with it through

  • 0

I would like to run a process with subprocess.Popen() and communicate with it through the python shell, like the subprocess.Popen usual behavior. Beside that, I would like to discursively log the STDIN and STDOUT to a logfile.

How can I do it?

  • 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-05-29T19:29:17+00:00Added an answer on May 29, 2026 at 7:29 pm

    Assuming discursively means rambling and rambling means all in the same file, then the following snippet is what you requested.

    Discursive logging with discrimination of the source and interaction

    Override its communicate method like similar question here

    import subprocess
    
    def logcommunicate(self, s):
        self.logfilehandle.write("Input "+s)
        std = self.oldcommunicate(s)
    
        self.logfilehandle.write("Output "+std[0])
        return std
    
    subprocess.Popen.oldcommunicate = subprocess.Popen.communicate
    subprocess.Popen.communicate = logcommunicate
    logfh = open("/tmp/communicate.log", "a")
    
    proc = subprocess.Popen(['cat'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    proc.logfilehandle = logfh
    
    result = proc.communicate("hello there\n")
    print result 
    

    Discursive logging with discrimination of the source

    First use StringIO instead of files, then subclass StringIO to override its write method to open that appends timestamp and source. Then write a custom compare function that sorts based on timestamp and source, timestamp first and then source Input and then output

     with open("file.log","wb") as in logfile:
     out = MyOutPutStringIO.StringIO() 
     in = MyInputStringIO.StringIO()
     subprocess.Popen(cmd, shell=True, universal_newlines = True, stdin=in, stdout=out)
    
     #Then after you are done
     linestotal = []
     for line in in.readlines():
         linestotal.append(line)
     for line in out.readlines():
         linestotal.append(line)
    
     linestotal.sort(customsortbasedontimestampandinput)
    
     for line in linestotal.readlines():
        logwrite.write(line)
    

    Discursive logging

     with open("file.log","wb") as in logfile:
     subprocess.Popen(cmd, shell=True, universal_newlines = True, stdin=logfile, stdout=logfile)
    

    The opposite is shown below

    Cursive logging

     with open("stdout.txt","wb") as out:
     with open("stderr.txt","wb") as err:
     with open("stdin.txt","wb") as in:
     subprocess.Popen(cmd, shell=True, universal_newlines = True, stdin=in,stdout=out,stderr=err)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run a process from Python (2.4/2.5/2.6) using Popen , and
I have a process intensive task that I would like to run in the
I would like to run a job through cron that will be executed every
I have a DLL that I would like to run as a background process.
I would like to accomplish two things during my build process: Run unit tests
I would like to configure asp.net process to run under an account with domain
I would like to run a (extensive) query that produces one line of XML.
I would like to run some environment checks when my django process starts and
I'm in the process of learning Objective-c but would like to run code snippets
I would like to run a program from the bash shell. When the program

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.