python interact with running process

  • Last Update :
  • Techknowledgy :

It sounds like omxplayer is reading from stdin - you can probably write to that process' stdin with your commands.

from subprocess
import Popen, PIPE
p = Popen(['omxplayer', filePath], stdin = PIPE, stdout = PIPE, stderr = PIPE, close_fds = True)
p.stdin.write(' ') # sends a space to the running process
p.stdin.flush() #
if the above isn 't enough, try adding a flush

Suggestion : 2

Last Updated : 29 Dec, 2020

1._
pip install wmi

Output: 
 

3196 RuntimeBroker.exe
3524 ShellExperienceHost.exe
3548 SearchIndexer.exe
3796 SearchUI.exe
4136 IDMan.exe
4368 IEMonitor.exe
4488 notepad.exe
2616 SettingSyncHost.exe
4212 dasHost.exe
4664 AdaptiveSleepService.exe
4716 svchost.exe
5412 chrome.exe
1376 chrome.exe
1280 cmd.exe
4928 conhost.exe
5596 py.exe
5060 python.exe
1508 WmiPrvSE.exe

Explanation:
Firstly, we initialize the WMI() function of wmi library. This allows us to use the functions found inside it such as WMI.Win32_Service, WMI.Win32_Process, WMI.Win32_Printjob which are designed to perform different tasks. We would be using the WMI.Win32_Process function in order to get the list of running processes on the system. Then we called the function WMI.Win32_Process() to get the running processes, iterated through each process and stored in variable process. Then we obtained the ProcessID (pid) and ProcessName (name) of the process using the associated attributes. We used F-strings for the output in order to add padding to the output to align it properly. 
  
Method 2:
In this method, we would be using a command found inside the Windows Command Processor (cmd.exe) under the name WMIC ( Windows Management Instrumentation Command line) in order to get the desired result. WMIC is a commandline utility that allows users to performs Windows Management Instrumentation (WMI) operations with a command prompt. For the purpose of getting running processes, we would be executing the command:
 

wmic process get description, processid

Suggestion : 3

The timeout argument is passed to Popen.communicate(). If the timeout expires, the child process will be killed and waited for. The TimeoutExpired exception will be re-raised after the child process has terminated.,All of the functions and methods that accept a timeout parameter, such as call() and Popen.communicate() will raise TimeoutExpired if the timeout expires before the process exits.,If the process does not terminate after timeout seconds, a TimeoutExpired exception will be raised. Catching this exception and retrying communication will not lose any output.,If the process does not terminate after timeout seconds, raise a TimeoutExpired exception. It is safe to catch this exception and retry the wait.

os.system
os.spawn *
>>> subprocess.run(["ls", "-l"]) # doesn 't capture output
CompletedProcess(args = ['ls', '-l'], returncode = 0)

   >>>
   subprocess.run("exit 1", shell = True, check = True)
Traceback(most recent call last):
   ...
   subprocess.CalledProcessError: Command 'exit 1'
returned non - zero exit status 1

   >>>
   subprocess.run(["ls", "-l", "/dev/null"], capture_output = True)
CompletedProcess(args = ['ls', '-l', '/dev/null'], returncode = 0,
   stdout = b 'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr = b '')
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
>>>
import shlex, subprocess
   >>>
   command_line = input() /
   bin / vikings - input eggs.txt - output "spam spam.txt" - cmd "echo '$MONEY'" >>>
   args = shlex.split(command_line) >>>
   print(args)['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"] >>>
   p = subprocess.Popen(args) # Success!
Popen(['/bin/sh', '-c', args[0], args[1], ...])
with Popen(["ifconfig"], stdout = PIPE) as proc:
   log.write(proc.stdout.read())

Suggestion : 4

Python 3 includes the subprocess module for running external programs and reading their outputs in your Python code.,You can use the subprocess.run function to run an external program from your Python code. First, though, you need to import the subprocess and sys modules into your program:,Now that we can invoke an external program using subprocess.run, let’s see how we can capture output from that program. For example, this process could be useful if we wanted to use git ls-files to output all your files currently stored under version control.,Warning: Never pass untrusted input to subprocess.run. Since subprocess.run has the ability to perform arbitrary commands on your computer, malicious actors can use it to manipulate your computer in unexpected ways.

You can use the subprocess.run function to run an external program from your Python code. First, though, you need to import the subprocess and sys modules into your program:

import subprocess
import sys

result = subprocess.run([sys.executable, "-c", "print('ocean')"])

If you run this, you will receive output like the following:

Outputocean

Let’s add to our previous example:

import subprocess
import sys

result = subprocess.run(
   [sys.executable, "-c", "print('ocean')"], capture_output = True, text = True
)
print("stdout:", result.stdout)
print("stderr:", result.stderr)

Let’s try an example that produces a non-empty value for stderr:

import subprocess
import sys

result = subprocess.run(
   [sys.executable, "-c", "raise ValueError('oops')"], capture_output = True, text = True
)
print("stdout:", result.stdout)
print("stderr:", result.stderr)

If we run this code, we receive output like the following:

Outputstdout:
stderr: Traceback (most recent call last):
File "<string>", line 1, in <module>
      ValueError: oops