python - 与 Python 异步从管道子进程获取流
问题描述
我想直接运行以下命令Python
:
dumpcap -q -f http -i eth0 -w - | tshark -l -n -T json -r - | my_app.py
我想通过使用subprocess
和运行它来asyncio
运行它async
。
所以起初我想运行:
dumpcap -q -f http -i eth0 -w -
此输出应通过管道传输到下一个命令,该命令应该/可以不同步运行:
tshark -l -n -T json -r -
这个输出应该通过管道传输到我可以使用的流中。
有没有一个简单的解决方案?
解决方案
除了@user4815162342的回答,请注意,您可以简单地将完整的shell命令传递给create_subprocess_shell并使用管道与子进程的两端进行通信:
例子:
proc = await asyncio.create_subprocess_shell(
"tr a-z A-Z | head -c -2 | tail -c +3",
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
)
stdout, _ = await proc.communicate(b"**hello**")
assert stdout == b"HELLO"
推荐阅读
- ubuntu - 谷歌上的网站花时间通过 https 访问 ubuntu
- php - 检测没有前置字符的换行符
- jquery - 通过 jquery 在单个函数中触发更改和就绪事件
- mysql - 找到 avg() 后如何在最后添加空值行
- linux - 将 Linux 文件系统类型分配给变量
- javascript - 将本地文件与远程同步
- c# - 在将数据表导出到 excel 时,列中的十进制值(例如 5 然后 5.1、5.2)会创建一个新的整行并使 excel 工作表变得混乱
- sql - SQL如何求和总数量
- angular - 当返回的 JSON 与 Angular 服务中的类不完全匹配时,将 API 响应映射到类
- python - 如何获取序列化方法字段中的所有实例