Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

ipc - python interprocess querying/control

I have this Python based service daemon which is doing a lot of multiplexed IO (select).

From another script (also Python) I want to query this service daemon about status/information and/or control the processing (e.g. pause it, shut it down, change some parameters, etc).

What is the best way to send control messages ("from now on you process like this!") and query processed data ("what was the result of that?") using python?

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

Both the background service daemon AND the frontend will be programmed by me, so all options are open :)

I am using Linux.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Pipes and Named pipes are good solution to communicate between different processes. Pipes work like shared memory buffer but has an interface that mimics a simple file on each of two ends. One process writes data on one end of the pipe, and another reads that data on the other end.

Named pipes are similar to above , except that this pipe is actually associated with a real file in your computer.

More details at

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

x = os.mkfifo(filename)

In child and parent open this pipe as file

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

To write

os.write(out, 'xxxx')

To read

lines = in.readline( )

Edit: Adding links from SO

You may want to read more on "IPC and Python"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...