使用多处理作为本地 IPC

发布于 2024-12-20 19:21:06 字数 415 浏览 4 评论 0原文

我正在考虑使用 Python 的多处理包 在本地 python 程序之间进行消息传递。

这似乎是正确的方法,如果:

  • 程序将始终在同一台机器(和相同的操作系统实例)上本地运行
  • 程序的实现将保留在 Python 中
  • 速度很重要

是否有可能 python 进程独立运行用户,即一个没有产生另一个?

怎么办?
文档似乎只给出了其中一个产生另一个的情况的示例。

I'm considering using Python's multiprocessing package for messaging between local python programs.

This seems like the right way to go IF:

  • The programs will always run locally on the same machine (and same OS instance)
  • The programs' implementation will remain in Python
  • Speed is important

Is it possible in case the python processes were run independently by the user, i.e. one did not spawn the other?

How?
The docs seem to give examples only of cases where one spawns the other.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木落 2024-12-27 19:21:06

程序将始终在同一台计算机(和相同的操作系统实例)上本地运行

多重处理允许具有 远程并发

程序的实现将保留在 Python 中

是或否。您可以将另一个命令包装在 python 函数中。这将起作用,例如:

from multiprocessing import Process
import subprocess

def f(name):
    subprocess.call(["ls", "-l"])

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

速度很重要

这取决于许多因素:

  • 有多少开销会导致进程之间的协调?
  • 你的CPU有多少个核心?
  • 每个进程需要多少磁盘 I/O?它们在同一个物理磁盘上工作吗?
  • ...

是否有可能 python 进程由用户独立运行,即一个进程没有生成另一个进程?

我不是这个主题的专家,但我通过使用文件交换数据实现了类似的东西[基本上一个进程的输出文件被另一个进程作为输入源监视,反之亦然]。

哈!

The programs will always run locally on the same machine (and same OS instance)

Multiprocessing allows to have remote concurrency.

The programs' implementation will remain in Python

Yes and no. You could wrap another command in a python function. This will work, for example:

from multiprocessing import Process
import subprocess

def f(name):
    subprocess.call(["ls", "-l"])

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

Speed is important

That depends from a number of factors:

  • how much overhead will cause the co-ordination between processes?
  • how many cores does your CPU have?
  • how much disk I/O is required by each process? Do them work on the same physical disk?
  • ...

Is it possible in case the python processes were run independently by the user, i.e. one did not spawn the other?

I'm not an expert on the subject, but I implemented something similar once by using files to exchange data [basically one process' output file was monitored as input source by the other, and vice-versa].

HTH!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文