I have a Python program which code contains the following fragment:
def Main():
pid = os.getpid()
print 'Process id: {}'.format(pid)
then, in other part of the code it keeps listening on its STDIN.
This process outputs the following:
Process id: 3824
From another Python process I want to write to the STDIN of the process above given its PID.
My question is: what code I need to do that from the second process?
I'm using Windows.
A program can only redirect the standard input and output of processes it creates("child processes"). To talk to another process using standard output, that process must already be listening to you.
It's the OS that controls what the file descriptors of a process refer to. That is, the stdin
of your process will be determined by the os and the context in which the programs are executed. To allow interprocess communication, you could look into the facilities provided by the OS for that, for example using a shell script and the piping operator.
What you'd want is a named pipe, which is basically a file-like object which you can write and read from, and which act as a bridge between processes. Unfortunately, python doesn't seem to have "native" support for that on Windows. You need the python for windows extension for that.
Alternatively, you could use a socket.