使用telnet应用线程时,Python运行非常慢,尽管按顺序运行(不带线程)时速度很快,请在下面的代码中为我提供帮助。
def login(node):
global tn
global response
response = None
tn = telnetlib.Telnet(node, timeout=10)
print()
idx, obj, response = tn.expect([b"Password: "],20)
if idx == -1:
sys.exit(response.decode('ascii'))
print('telnet success @{}'.format(node))
tn.write(b'ericsson' + b'\r')
idx, obj, response = tn.expect([b'>'],20)
if idx == -1:
sys.exit(response.decode('ascii'))
tn.write(b'enable' +b'\r\n')
idx, obj, response = tn.expect([b"Password: "],20)
if idx == -1:
sys.exit(response.decode('ascii'))
tn.write(b'ericsson' + b'\r\n')
q = queue.Queue()
for i in lst_nodes[12:20]: #put some tasks in the queue
q.put(i)
def worker(item):
loop_on_nodes(item) #make some tasks
q.task_done()
for i in range(5):
item = q.get_nowait()
print(item)
t = threading.Thread(target=worker, args=(item,))
t.daemon = True
t.start()
q.join() #block until all tasks are done