1

My main process/thread starts an executable that starts waiting for a signal after echoing Algorithm loaded. I am using the subprocess.Popen class for running the executable. Later, a thread is started that is supposed to send a signal to the earlier started executable. But I have no clue of how to send a signal to that particular subprocess from that thread.
Is it possible to pass PID's and "recover" subprocesses using the PID? The purpose of reusing the process is to send something equivalent to stdin.


Here's my code for starting the executable:

def start_module():
    cmd = '%s/libraries/OpenBR' % settings.MODULES_DIR
    process = subprocess.Popen(cmd,stdout=subprocess.PIPE)

    while True:
        line = process.stdout.readline()
        if line.find('Algorithm loaded') > -1:
            break

    return 0
Philip
  • 137
  • 1
  • 2
  • 16

1 Answers1

4

The process variable in your code refers to a Popen object, which supports a pid attribute. If you have your start_module function return the process, you can later send it a signal using os.kill. For example:

def start_module():
    cmd = '%s/libraries/OpenBR' % settings.MODULES_DIR
    process = subprocess.Popen(cmd,stdout=subprocess.PIPE)

    while True:
        line = process.stdout.readline()
        if line.find('Algorithm loaded') > -1:
            break

    return process

p = start_module()
os.kill(p.pid, signal.SIGALRM)

As far as I can see, using a thread or not to send the signal should not make any difference. Notice that os.kill does not necessarily kill a process: it sends it a signal that the process can then handle appropriately (an ALARM signal, here).


If your intention was to pass some input to the process's stdin, then things are also easy. You just need to add stdin=subprocess.PIPE to the Popen call and print to the stdin attribute of the new process:

def start_module():
    cmd = '%s/libraries/OpenBR' % settings.MODULES_DIR
    process = subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE)

    while True:
        line = process.stdout.readline()
        if line.find('Algorithm loaded') > -1:
            break

    return process

p = start_module()
print >> p.stdin, "Hello world!"
print >> p.stdin, "How are things there?"
nickie
  • 5,608
  • 2
  • 23
  • 37
  • Killing the process is not the purpose, rather to send a `stdin` to it. Basically sending a parameter and receiving the result. I believe I was unclear in my question but thanks for your answer. – Philip May 22 '14 at 01:37
  • 1
    @PhilipMasek `os.kill` can be used to send arbitrary signals to the subprocess, not just kill it. You said you wanted to send a signal in your original question, though it sounds like maybe you actually want to write to the stdin of the subprocess? – dano May 22 '14 at 01:51
  • Okey, I think I managed. But now I've got problems reading the output of the `stdin`. I tried this: `process.stdin.write(image_directory) print process.stdout.readline()` But the thread is just standing still on the print and not printing anything. I don't want to close the process. I just want the output after giving an `image_director`. I need to later be able to write to `stdin` again with another `image_directory` and retrieve the results from that. – Philip May 22 '14 at 12:47
  • I found my answer to my last question, I combined this: [http://stackoverflow.com/a/4343917/1052393](http://stackoverflow.com/a/4343917/1052393) with this: [http://stackoverflow.com/a/13605804/1052393](http://stackoverflow.com/a/13605804/1052393) Althoug, I only get the first line from the stdout... Any clues? – Philip May 22 '14 at 13:16