Non-GUI, single processort inter process massaging - how?
steve+comp.lang.python at pearwood.info
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
# Print the process ID.
print('My PID is:', os.getpid())
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()
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson
More information about the Python-list