简体   繁体   中英

Invalid requirement, parse error at “''”

I'm trying to connect to host by different threads in python but getting error sometimes(1 times in 25 times execution) I have seen similar threads and hoped to update pip to 8.1.1 will solve this but did not solve though.

code snippet:

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)

Error seen:

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 "''"

Yesterday, I was able to get around this by using an older version of setuptools, pip install "setuptools<34" but then today the problem came back. I was able to get around it by adding a 0.1 second sleep in the loop that was queuing the threads. Why multiple threaded calls to paramiko's SSHClient cause this error with pip/setuptools, I have no idea.

It looks like the connect function is not thread safe in the version of paramiko for python2.7

The solution is to use the Lock object from the threading module, from threading import Lock . Then wrap the call to the connect function of the paramiko client with the lock object. For example: from threading import Lock lock = Lock() ... lock.acquire() client.connect(...) lock.release()

The code above makes so that only one thread will use the connect at a time, which solves the problem that the function is not thread safe.

*** I am not sure if the problem exists in newer versions of paramiko, worth a look.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM