简体   繁体   English

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

[英]How to listen port parallel in python + linux

everyone! 大家! I have a python script, which process input from COM-port. 我有一个python脚本,可处理来自COM端口的输入。

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

It runs like this: 它像这样运行:

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

I want to make it parallel, for example by using GNU parallel. 我想使其平行,例如通过使用GNU parallel。 My way is like this: A| 我的方式是这样的: parallel B 平行B

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

But it is not working. 但这是行不通的。

Any ideas? 有任何想法吗? Many thanks. 非常感谢。 UPDATE: I found some solution: 更新:我找到了一些解决方案:

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

Another one by Inian: Inian的另一个:

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

You can invoke GNU parallel with the --pipe option as below:- 您可以使用--pipe选项GNU parallel调用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.

Use the restart flag before the --pipe --pipe之前使用restart标志

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

More info about GNU parallel . 有关GNU parallel更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM