Communicating with subprocess (via Popen)

Posted on
Sun Feb 07, 2021 10:26 am
FlyingDiver offline
User avatar
Posts: 7213
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Communicating with subprocess (via Popen)

I'm using this code to test communication with a Python 3 wrapper around a Python 3 only library.

Code: Select all
#client.py:   test client for communicating with wrapper.py

from subprocess import Popen, PIPE
from threading import Thread
from time import sleep

def read_it():
    print(u"read_it thread running")
    while True:
   
        for msg in process.stdout:
            print(u"subprocess output: {}".format(msg.rstrip()))
       

print(u"subprocess starting")
process = Popen(['/usr/bin/python3', './wrapper.py', 'arg1, 'arg2'],
                                stdin=PIPE, stdout=PIPE, close_fds=True, bufsize=1, universal_newlines=True)
print(u"subprocess running: {}".format(process.pid))
                               
thread = Thread(target=read_it)
thread.daemon = True
thread.start()

sleep(5.0)  # wait for initial output from subprocess


When this run using Python3, it works as expected:
Code: Select all
% /usr/bin/python3 --version
Python 3.8.2

% /usr/bin/python3 ./client.py
subprocess starting
subprocess running: 24458
read_it thread running
subprocess output: {"msg": "status", "status": "Login Complete"}


But not when running using Python2:
Code: Select all
% /usr/bin/python --version
Python 2.7.16

% /usr/bin/python ./client.py
subprocess starting
subprocess running: 24466
read_it thread running


Both processes are definitely running, I'm checking that in another Terminal window.

Any ideas?

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Feb 07, 2021 1:38 pm
matt (support) offline
Site Admin
User avatar
Posts: 21416
Joined: Jan 27, 2003
Location: Texas

Re: Communicating with subprocess (via Popen)

In your for loop try using readline().

Image

Posted on
Sun Feb 07, 2021 1:46 pm
FlyingDiver offline
User avatar
Posts: 7213
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Communicating with subprocess (via Popen)

Hmm. Changing it like this works:

Code: Select all
def read_it():
    print(u"read_it thread running")
    while True:
   
        msg = process.stdout.readline()
        print(u"subprocess output: {}".format(msg.rstrip()))       


Now to try it in the actual plugin...

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests