Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subprocess.Popen() not working in a docker container - works fine locally

I have created a Django REST framework project that I want to deploy on a server. I do not have admin access to the server, and so, the only way I found to ensure that all the project dependencies (such as Anaconda distribution) will be available on the server, is to create a docker image for my project , then create a corresponding container on the server, then run it from there.

In my project, I have a python script (mymain.py) that gets called using subprocess.Popen(). This works fine locally, and subprocess.Popen() does everything it is supposed to do.

However, when I try to do it from the docker container, it seems as if the line subprocess.Popen() was completely skipped [mymain.py is not called].

For docker, I have created a docker-compose.yml file, and in command prompt I type:

docker-compose up

I do not see any error, and everything seem to be working fine, however subprocess.Popen() does not seem to be working.

mymain.py first line is:

print('Testing if subprocess called mymain.py!!!')  

In a different file, I call subprocess.Popen(). I tried printing out the error, but unfortunately, both stdout and stderr return nothing:

p = subprocess.Popen(['python', mymain_path, args], shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 
out, err = p.communicate()
print('SUBPROCESS ERROR: ' + str(err))
print('SUBPROCESS stdout: ' + str(out.decode()))    

And this is what I get:

SUBPROCESS ERROR: None
SUBPROCESS stdout:

As you can see, the first line of my main.py was never printed...

However, when I do this locally by typing in command prompt:

python manage.py runserver 9000

everything works with no issues (the line 'Testing if subprocess called mymain.py!!!' gets printed).

I even tried to open the docker container shell and type the same 'python manage.py runserver 9000' command in there, but that did not work unfortunately.

Now the question is, how do I get subprocess to work remotely (in a docker container)?

Any help on this is very appreciated!

I am using:

Docker version 18.09.2, build 6247962
docker-compose version 1.23.2, build 1110ad01
Python 3.7.0
like image 214
Nodame Avatar asked Feb 15 '19 17:02

Nodame


People also ask

How subprocess popen works in Python?

The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.

What is the difference between subprocess run and Popen?

The main difference is that subprocess. run() executes a command and waits for it to finish, while with subprocess. Popen you can continue doing your stuff while the process finishes and then just repeatedly call Popen.

Does Docker run automatically pull?

The always option always initiates a pull before creating the container.


1 Answers

Looks like a way to get rid of this issue is to: 1. use 'shell=False' 2. Not have a return value for the 'subprocess.Popen()'

subprocess.Popen(['python', mymain_path, args], shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 
like image 73
Nodame Avatar answered Sep 18 '22 14:09

Nodame