- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要异步读取 StdIn 以获取消息(json 由\r\n 终止)并在处理异步后将更新的消息写入 StdOut。
目前我正在同步进行,例如:
class SyncIOStdInOut():
def write(self, payload: str):
sys.stdout.write(payload)
sys.stdout.write('\r\n')
sys.stdout.flush()
def read(self) -> str:
payload=sys.stdin.readline()
return payload
如何异步执行相同操作?
最佳答案
这是回声 stdin
的示例至stdout
使用 asyncio streams (对于 Unix)。
import asyncio
import sys
async def connect_stdin_stdout():
loop = asyncio.get_event_loop()
reader = asyncio.StreamReader()
protocol = asyncio.StreamReaderProtocol(reader)
await loop.connect_read_pipe(lambda: protocol, sys.stdin)
w_transport, w_protocol = await loop.connect_write_pipe(asyncio.streams.FlowControlMixin, sys.stdout)
writer = asyncio.StreamWriter(w_transport, w_protocol, reader, loop)
return reader, writer
async def main():
reader, writer = await connect_stdin_stdout()
while True:
res = await reader.read(100)
if not res:
break
writer.write(res)
if __name__ == "__main__":
asyncio.run(main())
input
提供了额外有用的异步等价物。 ,
print
,
exec
和
code.interact
:
from aioconsole import get_standard_streams
async def main():
reader, writer = await get_standard_streams()
更新:
connect_stdin_stdout
作品。
loop = asyncio.get_event_loop()
StreamReader
实例。 reader = asyncio.StreamReader()
一般情况下,
StreamReader/StreamWriter
类不打算被直接实例化,并且只能作为
open_connection()
等函数的结果使用。和
start_server()
.
StreamReader
为某些数据流提供缓冲的异步接口(interface)。某些源(库代码)调用它的函数,例如
feed_data
,
feed_eof
, 数据被缓冲并且可以使用
documented 读取接口(interface)协程
read()
,
readline()
, 等等。
StreamReaderProtocol
实例。 protocol = asyncio.StreamReaderProtocol(reader)
此类派生自
asyncio.Protocol
和
FlowControlMixin
并有助于在
Protocol
之间进行调整和
StreamReader
.它覆盖了这样的
Protocol
方法为
data_received
,
eof_received
并调用
StreamReader
方法
feed_data
.
stdin
在事件循环中。 await loop.connect_read_pipe(lambda: protocol, sys.stdin)
connect_read_pipe
函数采用
pipe
参数一个类似文件的对象。
stdin
是一个类似文件的对象。从现在开始,从
stdin
读取的所有数据将落入
StreamReaderProtocol
然后传入
StreamReader
stdout
在事件循环中。 w_transport, w_protocol = await loop.connect_write_pipe(FlowControlMixin, sys.stdout)
在
connect_write_pipe
您需要传递一个协议(protocol)工厂,该工厂创建协议(protocol)实例来实现
StreamWriter.drain()
的流控制逻辑.此逻辑在类
FlowControlMixin
中实现.还有
StreamReaderProtocol
继承自它。
StreamWriter
实例。 writer = asyncio.StreamWriter(w_transport, w_protocol, reader, loop)
此类使用函数
write()
转发传递给它的数据,
writelines()
等到基础
transport
.
protocol
用于支持
drain()
函数等待底层传输已刷新其内部缓冲区并可再次写入。
reader
是可选参数,可以是
None
,也用于支持
drain()
函数,在此函数开始时检查是否为阅读器设置了异常,例如,由于连接丢失(与套接字和双向连接相关),然后
drain()
也会抛出异常。
StreamWriter
的更多信息和
drain()
功能在这个伟大的
answer .
\r\n
读取行分隔符
readuntil可以使用
关于python asyncio如何读取StdIn并写入StdOut?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64303607/
我有以下代码来捕获和处理运行命令输出。 如何修改它以便运行命令窗口显示输出并同时记录输出?更换 @SW_HIDE与 @SW_SHOW (或等效的)只显示一个空白的命令窗口。 类似于linux的东西te
[已编辑] 用于处理 subprocess.Popen 标准输出的 2 个选项是 stdout="a_file_name" 和 stdout=subprocess.PIPE。 stderr 可以通过
c99 的 7.19.3/7 节指出: At program start-up, three text streams are predefined and need not be opened ex
我正在运行以下 proc_open 函数。加载页面时,出现错误: Use of undefined constant STDOUT - assumed 'STDOUT'` 如何正确设置STDOUT和S
我有一个运行多个进程的开发堆栈:网络服务器、自动测试、后台编译等。所有这些都是基本的命令行命令,例如 node app.js 或 lein midje :autotest. 是否可以使用一个脚本在“后
我正在使用 SLURM 在 super 计算机上调度作业。我已设置 --output=log.out 选项,将作业标准输出中的内容放入文件 (log.out)。我发现该文件每 30-60 分钟更新一次
ansible/ansible-playbook 版本:2.1.2.0/2.2.0.0 我正在尝试使用 yum/apt 安装软件包,但由于安装软件包的存储库位于 packagecloud.io 中,有
bala@hp:~$ echo "Hello World" > stdout bala@hp:~$ cat stdout Hello World bala@hp:~$ echo "Hello Worl
在从 Fortran 编写的外部库中调用嘈杂的函数之前,我正在使用如下代码重定向标准输出: // copy standard output out = dup(STDOUT_FILENO); // c
这个问题在这里已经有了答案: How can I pipe stderr, and not stdout? (11 个答案) 关闭 6 年前。 我有一个程序,我想检查其 STDERR 输出并在其上运
我正在从 perl 运行一个 java 应用程序 这是脚本 #!/usr/bin/perl use strict; use warnings; $| = 1; my $args = join (" "
我正在尝试将 python 脚本的 STDOUT 重定向到一个文件。 如果 STDOUT 是从 sys 导入的,脚本的输出不会被重定向到一个文件: from sys import stdout std
我正在尝试使用 PHP 和 Apache(在 Docker 的前台运行)写入 stdout(或 stderr)。 这些作品: file_put_contents( "php://stderr","wo
我正在尝试重定向标准输出,以便 Windows 应用程序中的 printf 将转到我选择的文件。 我这样做: outFile = fopen("log.txt", "w"); *stdout = *o
在 Ruby 中,$stdout(前面有一个美元符号)和 STDOUT(全部大写)有什么区别?在进行输出重定向时,应该使用哪个,为什么? $stderr 和 STDERR 也是如此。 编辑: 刚找到一
让我们看看这个Hello World程序 #include int main(int argc, char ** argv) { printf("Hello, World!"); c
我在 64 位机器上运行 Ubuntu 20.04。 我想将 stdout 重定向到从 memfd_create 获得的描述符。似乎使用 memfd_create 创建的匿名文件只有在 stdout
我想在 Python 脚本末尾捕获控制台输出。也就是说,我既想正常打印到控制台,又想在执行结束时将控制台输出保存到文件中。 我看过各种相关的 SO 问题 1 , 2 , 3尽管他们要么简单地重定向输出
我知道可以将两者都重定向到特定文件: ./command 1> out.log 2> err.log 或 ./command 1>test.log 2>&1 将两者写入文件。但是我不知道在只打印其中一
我知道可以将两者都重定向到特定文件: ./command 1> out.log 2> err.log 或 ./command 1>test.log 2>&1 将两者写入文件。但是我不知道在只打印其中一
我是一名优秀的程序员,十分优秀!