python - 如何在 python + linux 中监听端口并行

标签 python linux bash python-3.x parallel-processing

各位! 我有一个 python 脚本,它处理来自 COM 端口的输入。

import sys
for string in sys.stdin:
    some_calculation(string)

它是这样运行的:

cat -v /dev/pts/2 | python3 'process.py'

我想让它并行,例如通过使用 GNU 并行。 我的方式是这样的:A|平行B

cat -v /dev/pts/2 | parallel  --pipe --recstart '>' python3 process.py

但它不起作用。

有什么想法吗? 非常感谢。 更新: 我找到了一些解决方案:

cat /dev/pts/2 | parallel -j2 "echo {} | python3 process.py"

Inian 的另一个:

cat -v /dev/pts/2 | parallel --recstart '>' --pipe python3 process.py

最佳答案

您可以使用 --pipe 选项调用 GNU parallel,如下所示:-

--pipe
        Spread input to jobs on stdin (standard input). Read a block
        of data from stdin (standard input) and give one block of data
        as input to one job.

--pipe之前使用restart标志

cat -v /dev/pts/2 | parallel --recstart '>' --pipe python3 process.py

关于 GNU parallel 的更多信息.

关于python - 如何在 python + linux 中监听端口并行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40966591/

相关文章:

python - 在多维数组中写入值数组的坐标

python - Teradata 和 sqlachemy 连接

需要 Linux Cap_Kill 帮助

Linux 中的 C 编程 : not getting correct output for program that finds number of occurrences of substring in file

linux - shell find 在搜索结果中不显示根目录

Python:并行运行子进程

python - 列宽 - 初学者Python

java - 在 Tomcat 7 中部署的 war 在 Linux 上的行为与在 Windows 上的行为不同

linux - satement "export variable=lib:/dev/input/event0"中的运算符冒号在 linux 环境中是什么意思?

git - 带有 Git Bash 删除键的 ConEmu 不起作用