Interprocess request / python control

I have this Python-based service daemon that does a lot of multiplexed I / O (select).

From another script (also Python) I want to request this service daemon about status / information and / or control processing (for example, pause it, disable it, change some parameters, etc.).

What is the best way to send control messages ("now you are processing this!") And process the processed data ("what was the result of this?") Using python?

I read somewhere that named pipes can work, but don't know much about named pipes, especially in python, and are there any better alternatives.

Both support daemons and the external interface will be programmed by me, so all parameters are open :)

I am using Linux.

+3
source share
2 answers

Pipes and named pipes are a good solution for the interaction between different processes. Pipes act as a shared memory buffer, but have an interface that mimics a simple file at each of the two ends. One process writes data at one end of the channel, and the other reads this data at the other end.

Named channels are similar to those described above, except that this channel is actually associated with a real file on your computer.

More in

In Python, pipe named files are created with os.mkfifo

x = os.mkfifo(filename)

In the child and parent languages, this file must be opened as a file

out = os.open(filename, os.O_WRONLY)
in = open(filename, 'r')

To write

os.write(out, 'xxxx')

To read

lines = in.readline( )

: SO

"IPC Python"

+4

IPC - python, .

server process server.py( client.py interactive.py)

from multiprocessing.managers import BaseManager
import Queue
queue1 = Queue.Queue()
queue2 = Queue.Queue()
class QueueManager(BaseManager): pass
QueueManager.register('get_queue1', callable=lambda:queue1)
QueueManager.register('get_queue2', callable=lambda:queue2)
m = QueueManager(address=('', 50000), authkey='abracadabra')
s = m.get_server()
s.serve_forever()

, / interactive.py

from multiprocessing.managers import BaseManager
import threading
import sys
class QueueManager(BaseManager): pass
QueueManager.register('get_queue1')
QueueManager.register('get_queue2')
m = QueueManager(address=('localhost', 50000),authkey='abracadabra')
m.connect()
queue1 = m.get_queue1()
queue2 = m.get_queue2()

def read():
    while True:
        sys.stdout.write(queue2.get())

def write():
    while True:
        queue1.put(sys.stdin.readline())
threads = []

threadr = threading.Thread(target=read)
threadr.start()
threads.append(threadr)

threadw = threading.Thread(target=write)
threadw.start()
threads.append(threadw)

for thread in threads:
    thread.join()

Client.py

from multiprocessing.managers import BaseManager
import sys
import string
import os

class QueueManager(BaseManager): pass
QueueManager.register('get_queue1')
QueueManager.register('get_queue2')
m = QueueManager(address=('localhost', 50000), authkey='abracadabra')
m.connect()
queue1 = m.get_queue1()
queue2 = m.get_queue2()


class RedirectOutput:
    def __init__(self, stdout):
        self.stdout = stdout
    def write(self, s):
        queue2.put(s)

class RedirectInput:
    def __init__(self, stdin):
        self.stdin = stdin
    def readline(self):
        return queue1.get()

# redirect standard output

sys.stdout = RedirectOutput(sys.stdout)

sys.stdin = RedirectInput(sys.stdin)

# The test program which will take input and produce output 
Text=raw_input("Enter Text:")
print "you have entered:",Text
def x():
    while True:
        x= raw_input("Enter 'exit' to end and some thing else to continue")
        print x
        if 'exit' in x:
            break
x()

, , .

+1

Source: https://habr.com/ru/post/1766812/


All Articles