要求无效,"" 处出现解析错误



我正在尝试通过python中的不同线程连接到主机,但有时会出现错误(25次执行25次)我已经看到了类似的线程,希望将PIP更新为8.1.1可以解决此问题,但没有解决。

代码段:

def getkpis(self,cmd,host):
    ssh=paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        ssh.connect(host,username='root',look_for_keys=True)
        stdin, stdout, stderr = ssh.exec_command(cmd)
        paramiko.util.log_to_file("kpiparamiko.log")
        output=stdout.read()
        appendarray=output.split('n')
        sys.stdin.flush()
        ssh.close()
    except paramiko.SSHException, e:
        print str(e)

错误:

Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib64/python2.7/threading.py", line 811, in __bootstrap_inner
self.run()
File "/usr/lib64/python2.7/threading.py", line 764, in run
self.__target(*self.__args, **self.__kwargs)
File "/conf/home/smodugu/kpiparse.py", line 56, in getkpis
ssh.connect(host,username='root',look_for_keys=True)
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 338, in     connect
t.start_client()
File "/usr/lib/python2.7/site-packages/paramiko/transport.py", line 493, in       start_client
raise e
RequirementParseError: Invalid requirement, parse error at "''"

昨天,我能够使用旧版本的setuptools, pip install "setuptools<34"来解决这个问题,但今天问题又回来了。我能够通过在排队线的环中添加0.1秒的睡眠来解决它。为什么对Paramiko的Sshclient进行多个螺纹呼叫导致PIP/setuptools的此错误,我不知道。

看起来连接函数在python2.7

的paramiko版本中不是线程

解决方案是使用螺纹模块中的Lock对象, from threading import Lock。然后用锁定对象将调用呼叫到Paramiko客户端的connect函数。例如: from threading import Lock lock = Lock() ... lock.acquire() client.connect(...) lock.release()

上面的代码制作,使得只有一个线程一次使用连接,这解决了函数不是线程安全的问题。

***我不确定问题是否存在于paramiko的新版本中,值得一看。

最新更新