Python Forum
Subprocess Popen command issues. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Subprocess Popen command issues. (/thread-3869.html)



Subprocess Popen command issues. - Rabster - Jul-03-2017

I am having troubles using subprocess.Popen as it seems to give me an error when I try specifying the process to open test to test.py
If I leave it as test it seems to create a subprocess but the return code isn't correct(should be 42 not 1) and I don't get anything piped to stdout or stderr.
However, if it is test.py I get this output
stderr:  b'nice: \xe2\x80\x98test.py\xe2\x80\x99: No such file or directory\n'
Process completed:  127

If I get rid of the ulimit memory and nice flags it works completely fine? Is there any I could keep those flags in place. I would like to limit the amount of memory and allow for the process to throttle itself for other processes?
I have both of my files located in the same directory on the desktop and I'm using python version 3.5.2 on Linux Mint.

sub_proc.py
import os
import sys
import signal
import subprocess

print("Program starting, version: ", sys.version)

process = subprocess.Popen([sys.executable, "test.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE); #This one works as expected
#process = subprocess.Popen([sys.executable, 'ulimit -v 16384; nice -n 15 test.py'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) #This one does nothing, appears frozen? Removing shell as an argument then gives me an error that it can't find the file?
#process = subprocess.Popen('ulimit -v 16384; nice -n 15 test.py', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) #Outputs stderr:  b'nice: \xe2\x80\x98test.py\xe2\x80\x99: No such file or directory\n' Process completed:  127

try:
   out, err = process.communicate(timeout=3)
   if out: 
       print("stdout: ", out)
   if err:
       print("stderr: ", err)
   print("Process completed: ", process.returncode)
except subprocess.TimeoutExpired: 
   print("TIMEOUT")
   process.kill()
test.py
import sys
print("Hello from test.py")
sys.exit(42)



RE: Subprocess Popen command issues. - DeaD_EyE - Jul-03-2017

The command ulimit is a shell builtin: https://stackoverflow.com/questions/17483723/command-not-found-when-using-sudo-ulimit
Look into the documentation of the module resource: https://docs.python.org/3.6/library/resource.html
I am not sure, if the process invokes the resource limit, which you set inside the Python interpreter. Just try it.

Also you should locate the command nice with which nice. On my Ubuntu system the path is:
Output:
/usr/bin/nice