Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 请告诉我为什么会出现这种死锁:读取和写入类似对象的Paramiko exec_command()文件--ChannelFile.close()不起作用_Python_Paramiko - Fatal编程技术网

Python 请告诉我为什么会出现这种死锁:读取和写入类似对象的Paramiko exec_command()文件--ChannelFile.close()不起作用

Python 请告诉我为什么会出现这种死锁:读取和写入类似对象的Paramiko exec_command()文件--ChannelFile.close()不起作用,python,paramiko,Python,Paramiko,此代码在join()处永远等待。如果我删除了stderr写入线程,它会按预期工作,将正确的文本写入文件“readme.txt”,但随后我会丢失dd stderr消息。问题在于stdin.close()。根据Paramiko的文件(v1.16): 警告:要正确模拟从套接字的makefile()方法创建的文件对象,Channel及其ChannelFile应该能够独立关闭或垃圾收集当前,关闭通道文件只会刷新缓冲区。 因此,您必须使用stdin.channel.close() 更新: 由于stdin、

此代码在join()处永远等待。如果我删除了stderr写入线程,它会按预期工作,将正确的文本写入文件“readme.txt”,但随后我会丢失dd stderr消息。

问题在于
stdin.close()
。根据Paramiko的文件(v1.16):

警告:要正确模拟从套接字的
makefile()
方法创建的文件对象,
Channel
及其
ChannelFile
应该能够独立关闭或垃圾收集当前,关闭
通道文件
只会刷新缓冲区。

因此,您必须使用
stdin.channel.close()


更新:

由于stdin、stdout和stderr都共享一个通道,
stdin.channel.close()
也将关闭stderr,因此
读卡器
线程可能什么也得不到。解决方案是使用
stdin.channel.shutdown\u write()
,它不允许写入通道,但仍允许从通道读取

确认其工作正常:

[步骤101]#cat foo.py
导入paramiko、线程、paramiko
def读卡器(src):
尽管如此:
data=src.read()
如果没有数据:中断
打印数据,
client=paramiko.SSHClient()
client.set\缺少\主机\密钥\策略(paramiko.AutoAddPolicy())
client.connect(“10.11.12.13”,username=“root”,password=“password”)
stdin、stdout、stderr=client.exec_命令(“dd of=/tmp/file”)
reader=threading.Thread(target=reader,args=(stderr,))
reader.start()
stdin.write(“一些数据”)
stdin.flush()
stdin.channel.shutdown\u write()
reader.join()
[步骤102]#python foo.py
中的0+1个记录
0+1记录输出
[步骤103]#

问题出在
stdin.close()
中。根据Paramiko的文件(v1.16):

警告:要正确模拟从套接字的
makefile()
方法创建的文件对象,
Channel
及其
ChannelFile
应该能够独立关闭或垃圾收集当前,关闭
通道文件
只会刷新缓冲区。

因此,您必须使用
stdin.channel.close()


更新:

由于stdin、stdout和stderr都共享一个通道,
stdin.channel.close()
也将关闭stderr,因此
读卡器
线程可能什么也得不到。解决方案是使用
stdin.channel.shutdown\u write()
,它不允许写入通道,但仍允许从通道读取

确认其工作正常:

[步骤101]#cat foo.py
导入paramiko、线程、paramiko
def读卡器(src):
尽管如此:
data=src.read()
如果没有数据:中断
打印数据,
client=paramiko.SSHClient()
client.set\缺少\主机\密钥\策略(paramiko.AutoAddPolicy())
client.connect(“10.11.12.13”,username=“root”,password=“password”)
stdin、stdout、stderr=client.exec_命令(“dd of=/tmp/file”)
reader=threading.Thread(target=reader,args=(stderr,))
reader.start()
stdin.write(“一些数据”)
stdin.flush()
stdin.channel.shutdown\u write()
reader.join()
[步骤102]#python foo.py
中的0+1个记录
0+1记录输出
[步骤103]#
import paramiko, threading

def Reader(src):
    while True:
        data = src.readline()
        if not data: break
        print data


client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect("172.17.0.2", username="test", password="test")

stdin, stdout, stderr = client.exec_command("dd of=readme.txt")

reader = threading.Thread(target=Reader, args=(stderr, ))
reader.start()

stdin.write("some data")
stdin.flush()
stdin.close()

reader.join()