我有一个代码,它将使用telentlib连接到多个路由器,在其中运行一些代码,最后将输出写入一个文件。它运行平稳。在
然而,当我必须访问大量路由器(+50)时,任务会消耗大量时间(代码以串行方式运行,一次运行一个路由器)。我当时想实现线程以加速进程。在
这几乎就是代码(只是其中的一小部分):# We import the expect library for python
import telnetlib
import sys
import csv
import time
import threading
# --- Timers
TimeLogin = 10
TelnetWriteTimeout = 1
TelnetReadTimeout = 2
# --- CSV
FileCsv = "/home/lucas/Documents/script/file.csv"
# --- Extras
cr="\n"
# variables
IP = "A.B.C.D"
User = ["user","password"]
CliLogin = "telnet " + IP
Prompt = ["root@host.*]#"]
PromptLogin = ["login:"]
PromptLogout = ["logout"]
PromptPass = ["Password:"]
CliLine = "ls -l"
class MiThread(threading.Thread):
def __init__(self,num,datos):
threading.Thread.__init__(self)
self.num = num
self.datos = datos
self.systemIP = self.datos[0]
self.tn = telnetlib.Telnet(IP)
self.tn.timeout = TimeLogin
# File declaration
self.FileOutGen = self.systemIP + "_commands"
self.FileOutSend = self.systemIP + "_output"
self.FileOutRx = self.systemIP + "_rx"
self.fg = open(self.FileOutGen,"w")
self.fr = open(self.FileOutRx,"a")
self.fs = open(self.FileOutSend,"w")
def run(self):
print "Soy el hilo", self.num
self.telnetLogin()
self.runLs()
self.telnetLogout()
def telnetLogin(self):
i=self.tn.expect(PromptLogin)
print i
if i:
writeTelnet(User[0],TelnetWriteTimeout)
j=self.tn.expect(PromptPass)
print j
if j:
writeTelnet(User[1],TelnetWriteTimeout)
def telnetLogout(self):
i=self.tn.expect(Prompt)
if i:
writeTelnet("exit",TelnetWriteTimeout)
j=self.tn.expect(PromptLogout)
if j:
print "Logged out OK from SAM"
def runLs(self):
writeLog("Prueba de Ls " + self.systemIP)
self.writeCsv(self.systemIP,1)
i=self.tn.expect(Prompt,TimeLogin)
print i
if i:
# Prompt
CliLine = "ls -l "
writeTelnet(CliLine,TelnetWriteTimeout)
def writeCsv(self,inText,lastIn):
lock.acquire(1)
if lastIn==0:
fc.write(inText + ",")
elif lastIn==1:
fc.write(inText + "\n")
lock.release()
def writeTelnet(inText, timer):
tn.write(inText + cr)
time.sleep(timer)
def writeLog(inText):
print (inText)
t.fs.write("\n" + inText)
def printConsole(inText):
print (inText)
oFile = csv.reader(open(FileCsv,"r"), delimiter=",", quotechar="|")
lock = threading.Lock()
routers = list(oFile)
threads_list = []
for i in range(len(routers)):
FileOutCsv = "00_log.csv"
# creating output file
fc = open(FileOutCsv,"a")
# running routine
t = MiThread(i,routers[i])
threads_list.append(t)
t.start()
。。。一切都运行得很好,但是没有时间上的收获,因为t.join()将强制一个线程在运行下一个线程之前完成!在
序列化线程的事实(即使用t.join())使我认为某些内存空间是共享的,因为当我确实想并行化它们时(注释掉t.join()),就会出现问题。在
我做错什么了吗?如果需要的话,我可以提供更多的信息,但我真的不知道我做错了什么。。。在