簡體   English   中英

讀取“watch”命令 output 時,readline 在 paramiko.Channel 上掛起

[英]readline hangs on paramiko.Channel when reading “watch” command output

我正在測試此代碼以讀取watch命令的 output。 我懷疑這與watch的工作方式有關,但我不知道出了什么問題或如何解決它:

import paramiko

host = "micro"
# timeout = 2  # Succeeds
timeout = 3  # Hangs!
command = 'ls / && watch -n2 \'touch "f$(date).txt"\''

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(host, password='', look_for_keys=False)

transport = ssh_client.get_transport()
channel = transport.open_session()
channel.get_pty()
channel.settimeout(timeout)
channel.set_combine_stderr(True)
stdout = channel.makefile()
channel.exec_command(command)

for line in stdout:  # Hangs here
    print(line.strip())

有幾個類似的問題,其中一些很老( 12 ,可能還有其他問題)

其他不使用watch的命令也不會發生這種情況。

有人知道這個特定命令有什么特別之處和/或如何可靠地為讀取操作設置超時嗎?

(在 Python 3.4.2 和 paramiko 1.15.1 上測試)

編輯 1 :我按照相關問題的答案中的建議合並了channel.set_combine_stderr(True) ,但仍然沒有成功。 但是, watch確實產生了很多 output,所以問題可能正是如此。 實際上,使用此命令刪除了掛起:

command = 'ls / && watch -n2 \'touch "f$(date).txt"\' > /dev/null'

所以,可能這個問題幾乎是Paramiko ssh die/hang with big output的重復,但讓我想知道是否真的沒有辦法使用.readline() (在這種情況下通過__next__調用)並且必須求助於閱讀固定緩沖區大小並手動組裝行。

這可能會掛起,因為watch不會產生換行符。 如果換一個

for line in stdout:
    print(line.strip())

有一個繁忙的循環

stdout.readline(some_fixed_size)

可以看出字節從不包含換行符。 因此,這是一個非常特殊的情況,與其他問題和 SO 問題中報告的其他掛起無關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM