python进程间查询/控制 [英] python interprocess querying/control

查看:70
本文介绍了python进程间查询/控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个基于Python的服务守护进程,该守护进程正在执行许多多路复用IO(选择).

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

从另一个脚本(也是Python)中,我想查询该服务守护进程的状态/信息和/或控制处理(例如,暂停,关闭,更改某些参数等).

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).

使用python发送控制消息(从现在开始就这样处理!")和查询处理过的数据(结果是什么?")的最佳方法是什么?

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?

我在某处读到了命名管道可能有用的方法,但是对命名管道(尤其是在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 :)

我正在使用Linux.

I am using Linux.

推荐答案

管道和命名管道是在不同进程之间进行通信的很好的解决方案. 管道的工作方式类似于共享内存缓冲区,但是管道的两端都模仿了一个简单文件.一个进程在管道的一端写入数据,而另一个进程在另一端读取该数据.

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.

更多详细信息,位于

在Python中,使用os.mkfifo调用创建命名管道文件

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')

os.write(out, 'xxxx')

阅读

lines = in.readline( )

从SO添加链接

  • Create a temporary FIFO (named pipe) in Python?
  • https://stackoverflow.com/search?q=python+named+pipes

您可能想了解有关"IPC和Python"的更多信息

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

这篇关于python进程间查询/控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆