Python中线程的并发处理
我正在尝试使用python多线程在远程节点上运行不同的bash脚本。第一个线程t1触发script1,这应该在其他线程之前首先完成。然后,第二个线程t2必须再触发3个脚本。我之所以使用Popen(),是因为它不需要等到脚本完全执行。因此,所有这3个脚本现在必须同时处于运行模式。然后,200秒后,必须启动另一个线程,该线程将触发另一个脚本。我甚至可以使用定时器,在200秒后运行一个线程,但它有时对我不起作用 因此,在我的例子中,除了第一个线程t1之外,所有线程都应该并发运行。因此,所有脚本script2、script3、script4都需要同时执行,script5应该在200秒后执行 我很困惑,我应该在这里使用python多处理还是python多线程。到目前为止,我只尝试过python多线程 这里的每个脚本都使用python vxargs程序在多个远程节点上并行运行。我使用script2中的top命令捕获cpu利用率,即使用script3中的tcpdump传输到网络上的数据包长度。在某个时刻,我需要终止这两个进程(top,tcpdump),因此我使用script5使用kill命令终止它们。如果我在10个远程节点上运行这些脚本,我只会在4个节点上得到结果。处理这些script2、script3或script5中的一个可能有问题,最重要的是处理script5,因为在我看来,script5并没有杀死顶级的tcpdump进程。我想使其稳定,即为所有远程节点工作。这是我的密码: (在我的代码中,file1.txt包含所有远程节点的列表,其中我的脚本正在使用vxargs运行)Python中线程的并发处理,python,multithreading,python-3.x,multiprocessing,subprocess,Python,Multithreading,Python 3.x,Multiprocessing,Subprocess,我正在尝试使用python多线程在远程节点上运行不同的bash脚本。第一个线程t1触发script1,这应该在其他线程之前首先完成。然后,第二个线程t2必须再触发3个脚本。我之所以使用Popen(),是因为它不需要等到脚本完全执行。因此,所有这3个脚本现在必须同时处于运行模式。然后,200秒后,必须启动另一个线程,该线程将触发另一个脚本。我甚至可以使用定时器,在200秒后运行一个线程,但它有时对我不起作用 因此,在我的例子中,除了第一个线程t1之外,所有线程都应该并发运行。因此,所有脚本scri
提前谢谢 什么与您的代码不兼容?是因为thread3有时无法启动吗?如果您不提供任何关于哪些不起作用的详细信息,请尝试帮助您。我至少找到了3位候选人,但我需要更多的细节来确定并填写答案。@Micheled'Amico我已经用更多细节更新了我的问题
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