我如何测试 stdin 在 julia 中是否有可用的输入?
How can I test whether stdin has input available in julia?
我想在短时间内检测stdin
是否有输入window,并继续执行任何一种方式,结果存储在Bool
中。 (我的真正目标是在终端中运行的模拟上实现一个暂停按钮。第二次按键应该取消暂停程序,它应该继续执行。)我试过使用 poll_fd
但它不起作用stdin
:
julia> FileWatching.poll_fd(stdin, readable=true)
ERROR: MethodError: no method matching poll_fd(::Base.TTY; readable=true)
有没有办法对 julia 有效?我找到了 ,我考虑过通过 PyCall 使用它,但我正在寻找
- 一种更简洁、纯粹的 julia 方式;和
- 一种不会影响或可能干扰 julia 使用 libuv 的方法。
bytesavailable(stdin)
这是一个示例用法。请注意,如果您捕获键盘,您还需要自己处理 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
根据@Przemyslaw Szufel 的回复,这是一个完整的解决方案,允许按键 pause/unpause 循环迭代:
import REPL
term = REPL.Terminals.TTYTerminal("xterm",stdin,stdout,stderr)
REPL.Terminals.raw!(term,true)
Base.start_reading(stdin)
function read_and_handle_control_c()
b = bytesavailable(stdin)
@assert b > 0
data = read(stdin, b)
if data[1] == UInt(3)
println("Ctrl+C - exiting")
exit()
end
nothing
end
function check_for_and_handle_pause()
if bytesavailable(stdin) > 0
read_and_handle_control_c()
while bytesavailable(stdin) == 0
sleep(0.1)
end
read_and_handle_control_c()
end
nothing
end
while true
# [do stuff]
sleep(0.05)
check_for_and_handle_pause()
end
这有点不太理想,因为它要求进程即使在暂停时也能定期唤醒,但它仍然实现了我的目标。
我想在短时间内检测stdin
是否有输入window,并继续执行任何一种方式,结果存储在Bool
中。 (我的真正目标是在终端中运行的模拟上实现一个暂停按钮。第二次按键应该取消暂停程序,它应该继续执行。)我试过使用 poll_fd
但它不起作用stdin
:
julia> FileWatching.poll_fd(stdin, readable=true)
ERROR: MethodError: no method matching poll_fd(::Base.TTY; readable=true)
有没有办法对 julia 有效?我找到了
- 一种更简洁、纯粹的 julia 方式;和
- 一种不会影响或可能干扰 julia 使用 libuv 的方法。
bytesavailable(stdin)
这是一个示例用法。请注意,如果您捕获键盘,您还需要自己处理 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
根据@Przemyslaw Szufel 的回复,这是一个完整的解决方案,允许按键 pause/unpause 循环迭代:
import REPL
term = REPL.Terminals.TTYTerminal("xterm",stdin,stdout,stderr)
REPL.Terminals.raw!(term,true)
Base.start_reading(stdin)
function read_and_handle_control_c()
b = bytesavailable(stdin)
@assert b > 0
data = read(stdin, b)
if data[1] == UInt(3)
println("Ctrl+C - exiting")
exit()
end
nothing
end
function check_for_and_handle_pause()
if bytesavailable(stdin) > 0
read_and_handle_control_c()
while bytesavailable(stdin) == 0
sleep(0.1)
end
read_and_handle_control_c()
end
nothing
end
while true
# [do stuff]
sleep(0.05)
check_for_and_handle_pause()
end
这有点不太理想,因为它要求进程即使在暂停时也能定期唤醒,但它仍然实现了我的目标。