0

I have program A which is a command line application that prints out specific status messages at random times. It also takes input and runs commands based on that. What I'm trying to do is use Python to make a wrapper around that program so that when the output (stdout) meets specific criteria, a python function is run. I also have a function that will send input to Program A based on Slack- I'm using the slackhq/python-slackclient rtm_connect() to listen to a channel. However, when I run my handler, the STDIN/STDOUT don't appear (STDOUT is being sent to PIPE because of a guide), and when I manually trigger events that meet the criteria for the first python function, nothing happens. No print statement, nothing.

Pexpect keeps hitting pexpect.TIMEOUT: Timeout exceeded.

a = Popen(["/proj/a"], stdout=PIPE)

def main():
    sc.rtm_connect() #The RTM connect
    a_to_slack(test_message) #Post message when connected

    if a:
        print("A was opened previously")
        listen_to_a()
    else:
        world = Popen(["/proj/a"], stdout=PIPE)
        listen_to_a()

def listen_to_a():
    while True:
        data, error = a.stdout.read()[0] #From duplicate, to get STDOUT
        print(data) #Never prints
        a_to_slack(data) #Nothing..
        print sc.rtm_read() #Doesn't print here either
        time.sleep(1)
Rudi
  • 95
  • 9
  • `a.communicate()` will wait for the process to terminate ... there is a dupe around here somewhere about streaming stdout from a long running process – Joran Beasley Apr 20 '16 at 18:15
  • Use pexpect to spawn the C++ binary and interact with it. This example is a good starting point for what you want to do: https://github.com/pexpect/pexpect/blob/master/examples/ftp.py – WreckeR Apr 20 '16 at 18:23
  • The c++ program behaves differently if it is in a shell or not, specifically it buffers it's output if it is being run by subprocess. Check out this question and the 3 dupes linked from it: https://stackoverflow.com/questions/1606795/catching-stdout-in-realtime-from-subprocess – nephlm Apr 20 '16 at 18:26
  • 1- `a.stdout.read()` won't return until EOF (don't call it on a blocking fd if you need multiple back-and-forths). I don't see it in [the question @JoranBeasley linked—btw, it is not a duplicate: something like pexpect's ftp example is closer to the topic](http://stackoverflow.com/q/375427). Read questions from the section in [tag:subprocess] tag info: [Interacting with a subprocess while it is still running](http://stackoverflow.com/tags/subprocess/info), try solutions yourself and make sure you understand them 2- *"Pexpect keeps hitting pexpect.TIMEOUT:"* is not informative. Provide [mcve] – jfs Apr 23 '16 at 11:05

0 Answers0