我尝试使用python多线程在远程节点上运行不同的bash脚本。第一个线程t1触发script1,这应该在其他线程之前完成。然后,第二个线程t2必须再触发3个脚本。我使用Popen(),因为它不需要等到脚本完全执行。所以,同时这3个脚本现在必须处于运行模式。然后,在200秒之后,必须启动另一个线程来触发另一个脚本。我甚至可以使用计时器在200秒后运行线程,但有时它对我不起作用。在
因此,在我的例子中,除了第一个线程t1之外,所有线程都应该并发运行。所以,所有脚本script2,script3,script4需要同时执行,script5应该在200秒后执行。在
我很困惑,我是否应该在这里使用python多处理或python多线程处理。到目前为止,我只尝试过python多线程处理。在
这里的每个脚本都使用pythonvxargs程序在多个远程节点上并行运行。我使用script2中的top命令捕捉cpu利用率,即在script3中使用tcpdump传输到网络上的数据包的长度。在某个时刻,我需要终止这两个进程(top,tcpdump),所以我使用script5使用kill命令来杀死它们。如果我在10个远程节点上运行这些脚本,我只得到可能是4个节点的结果。处理这些script2、script3或script5中的一个可能会有问题,最重要的是处理script5,因为在我看来,script5并没有杀死顶部的tcpdump进程。我想让它稳定,也就是说可以为所有远程节点工作。这是我的代码:
(在我的代码中,file1.txt包含所有远程节点的列表,其中我的脚本正在使用vxargs运行)
class plThreads (threading.Thread):
def __init__(self,threadID,name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
print "starting thread "+self.name
pyFunction(self.name)
print "Exiting thread "+self.name
def pyFunction(threadName):
if threadName == 'thread1':
global startTime
startTime=time.time()
subprocess.Popen("python vxargs -a file1.txt ssh -l user_name {} 'bash script1.sh'", shell=True)
elif threadName == 'thread2':
subprocess.Popen("python vxargs -a file1.txt ssh -l user_name {} 'bash script2.sh'", shell=True)
subprocess.Popen("python vxargs -a file1.txt ssh -l user_name {} 'bash script3.sh'", shell=True)
subprocess.Popen("python vxargs -a file1.txt ssh -l user_name {} 'bash script4.sh'", shell=True)
elif threadName == 'thread3':
subprocess.call("python vxargs -a file1.txt ssh -l user_name {} 'bash script5.sh'", shell=True)
t1=plThreads(1,"thread1")
t1.start()
t1.join()
t2=plThreads(2, "thread2")
t2.start()
while(1):
timediff = time.time()-startTime
if timediff >= 200:
t3=plThreads(3,"thread3")
t3.start()
break
提前谢谢!!在
目前没有回答
相关问题 更多 >
编程相关推荐