Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to get process handler?
#1
Hi All,

I have few questions
1. how to get the process handler?
2. how to keep the process open throughout the entire execution of the script
3. while reading an output from the process, it should not terminate the process, since it should able to get the input again

how to achieve these, would really helps.
Regards,
Maiya
Reply
#2
Hi All,

#!/usr/bin/env python3

import subprocess

proc = subprocess.Popen(['ls'], stdin = subprocess.PIPE, stdout = subprocess.PIPE)

proc.stdin.write('\n'.encode())

while True:
    output = proc.stdout.readline()
    if type(output) is str:
        output = output
    else:
        output = output.decode('utf-8', 'ignore')
    
    if output == '':
        break

    print(output)
    

proc.stdin.write('ls -l'.encode())
proc.stdin.write('\n'.encode())

while True:
    output = proc.stdout.readline()
    if type(output) is str:
        output = output
    else:
        output = output.decode('utf-8', 'ignore')
    
    if output == '':
        break

    print(output)
        
proc.wait()
Here I would like to write a command to previously opened process and read the output from it. However for the command which I have fired ('ls'), could able to read output, but then for the second command ('ls -l'), could not able to read any output from.

How to achieve this, while reading output from the process it should not terminate (which I could able to succeed), and process should able to accept the second set of command (which I have fired it in second ) and return the output (since I have list of commands, will be firing one after the other)

In the end how ever doing proc.wait() to terminate the process (will do once all commands are fired and read the output from it)

Much appreciable for any suggestions on this. Thanks a lot.

Regards,
Maiya
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error handler appears to be turned off. How do I turn it back on? jpotter0 0 563 Nov-26-2022, 11:44 AM
Last Post: jpotter0
  Create exception handler oradba4u 7 3,025 May-23-2020, 03:25 AM
Last Post: oradba4u
  Custom logging handler looping continuously linuxaddikt 0 1,756 Mar-12-2020, 06:58 PM
Last Post: linuxaddikt
  How to sharing object between multiple process from main process using Pipe Subrata 1 3,617 Sep-03-2019, 09:49 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020