Non-GUI, single processort inter process massaging - how?

Steven D'Aprano steve+comp.lang.python at
Sat Jul 21 08:47:10 EDT 2018

On Sat, 21 Jul 2018 09:07:23 +0100, Chris Green wrote:

> I want to be able to interrogate the server process from several client
> processes, some will interrogate it multiple times, others once only. 
> They are mostly (all?) run from the command line (bash).

This sounds like a good approach for signals. Your server script sets up 
one or more callbacks that print the desired information to stdout, or 
writes it to a file, whichever is more convenient, and then you send the 
appropriate signal to the server process from the client processes.

At the bash command line, you use the kill command: see `man kill` for 

Here's a tiny demo:

# === cut ===

import signal, os, time
state = 0

def sig1(signum, stack):
    print(time.strftime('it is %H:%m:%S'))

def sig2(signum, stack):
    print("Current state:", stack.f_globals['state'])

# Register signal handlers
signal.signal(signal.SIGUSR1, sig1)
signal.signal(signal.SIGUSR2, sig2)

# Print the process ID.
print('My PID is:', os.getpid())

while True:
    state += 1

# === cut ===

Run that in one terminal, and the first thing it does is print the 
process ID. Let's say it prints 12345, over in another terminal, you can 

kill -USR1 12345
kill -USR2 12345

to send the appropriate signals.

To do this programmatically from another Python script, use the os.kill() 

Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson

More information about the Python-list mailing list