Using python with subprocess Popen

2024/9/16 23:10:11

I am struggling to use subprocesses with python. Here is my task:

  1. Start an api via the command line (this should be no different than running any argument on the command line)
  2. Verify my API has come up. The easiest way to do this would be to poll the standard out.
  3. Run a command against the API. A command prompt appears when I am able to run a new command
  4. Verify the command completes via polling the standard out (the API does not support logging)

What I've attempted thus far:
1. I am stuck here using the Popen. I understand that if I use subprocess.call("put command here") this works. I wanted to try to use something similar to:

import subprocessdef run_command(command):p = subprocess.Popen(command, shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)

where I use run_command("insert command here") but this does nothing.

with respect to 2. I think the answer should be similar to here: Running shell command from Python and capturing the output, but as I can't get 1. to work, I haven't tried that yet.

Answer

To at least really start the subprocess, you have to tell the Popen-object to really communicate.

def run_command(command):p = subprocess.Popen(command, shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)return p.communicate()
https://en.xdnf.cn/q/72606.html

Related Q&A

ipdb, multiple threads and autoreloading programs causing ProgrammingError

I am using ipdb debugger to debug multithreaded web applications locally (Django, Plone). Often ipdb seems to get confused because of the autoreload which happens when I am on the debug prompt. The res…

How to turn off logging buffer to get logs in real time with python command line tool?

I have a command line tool which produces plenty of logs. I want these logs to be sent to stdout as soon as theyre made. Right now, the program finishes everything (which can take several minutes), and…

How to tell if process is responding in Python on Windows

I am writing a python script to keep a buggy program open and I need to figure out if the program is not respoding and close it on windows. I cant quite figure out how to do this.

How to load and use a pretained PyTorch InceptionV3 model to classify an image

I have the same problem as How can I load and use a PyTorch (.pth.tar) model which does not have an accepted answer or one I can figure out how to follow the advice given.Im new to PyTorch. I am trying…

Append list to pandas DataFrame as new row with index

Despite of the numerous stack overflow questions on appending data to a dataframe I could not really find an answer to the following. I am looking for a straight forward solution to append a list as la…

IPC between Python and C#

I want to pass data between a Python and a C# application in Windows (I want the channel to be bi-directional) In fact I wanna pass a struct containing data about a network packet that Ive captured wit…

Saving matplotlib subplot figure to image file

Im fairly new to matplotlib and am limping along. That said, I havent found an obvious answer to this question.I have a scatter plot I wanted colored by groups, and it looked like plotting via a loop w…

Numpy - Dot Product of a Vector of Matrices with a Vector of Scalars

I have a 3 dimensional data set that I am trying to manipulate in the following way. data.shape = (643, 2890, 10) vector.shape = (643,)I would like numpy to see data as a 643 length 1-D array of 2890x1…

delete node in binary search tree python

The code below is my implement for my binary search tree, and I want to implement delete method to remove the node. Below is my implementation, but when I perform bst = BSTRee() bst.insert(5) bst.inser…

ImportError: cannot import name cbook when using PyCharms Profiler

I am trying to run the PyCharm profiler but I get the following error message:Traceback (most recent call last):File "/home/b3053674/ProgramFiles/pycharm-2017.1.4/helpers/profiler/run_profiler.py&…