我想检测stdin在短时间内是否有输入,并以任何一种方式继续执行,结果存储在Bool. (我的真正目标是在终端中运行的模拟上实现一个暂停按钮。第二次按键应该取消暂停程序,它应该继续执行。)我尝试使用,poll_fd但它不起作用stdin:
julia> FileWatching.poll_fd(stdin, readable=true)
ERROR: MethodError: no method matching poll_fd(::Base.TTY; readable=true)
Run Code Online (Sandbox Code Playgroud)
有没有办法对朱莉娅起作用?我找到了一个适用于 python 的解决方案,我已经考虑通过 PyCall 使用它,但我正在寻找
bytesavailable(stdin)
Run Code Online (Sandbox Code Playgroud)
这是一个示例用法。请注意,如果您捕获键盘,您还需要自己处理 Ctrl+C(在此示例中仅检查块的第一个字节)。
如果你想完全异步地运行它,就把它放在循环的@async前面while。但是,如果在这种情况下没有更多代码,则该程序将退出。
import REPL
term = REPL.Terminals.TTYTerminal("xterm",stdin,stdout,stderr)
REPL.Terminals.raw!(term,true)
Base.start_reading(stdin)
while (true)
sleep(1)
bb = bytesavailable(stdin)
if bb > 0
data = read(stdin, bb)
if data[1] == UInt(3)
println("Ctrl+C - exiting")
exit()
end
println("Got $bb bytes: $(string(data))")
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
274 次 |
| 最近记录: |