What is a process?
Process, which is a running state of the program.
What is a thread?
Why Use Threads?
Thread, which is the "execution unit" inside the process .
A process can contain multiple threads, and each thread can execute different code.
That is, with multiple threads, one process can perform multiple functions at the same time!
Prerequisite Installation Sshpass
Solution 1.
#!/usr/bin/python#-*- coding:utf-8import threadingimport osdef linux_ls1 (): while True: cmd= "sshpass -p ' 00000000 ' ssh [email Protected] ls " ret=os.popen (CMD) print ret.read ()     BREAKDEF LINUX_LS2 (): while True: cmd= " sshpass -p ' 00000000 ' ssh [email protected] ls ' &nbSp; ret=os.popen (CMD) print ret.read () breakt1=threading. Thread (TARGET=LINUX_LS1) t2=threading. Thread (TARGET=LINUX_LS2) T1.start () T2.start ()
Solution 2.
#!/usr/bin/python#-*- coding:utf-8import threadingimport osdef linux_ls1 (PWD,USER,IP): while True: cmd= "sshpass -p '%s ' ssh %[email protected]%s ls " % (PWD,USER,IP) ret=os.popen (CMD) print ret.read ()          BREAKDEF LINUX_LS2 (PWD,USER,IP): while True: cmd= "sshpass -p '%s ' ssh %[email protected]%s ls " % (PWD,USER,IP) &Nbsp; ret=os.popen (CMD) print ret.read ( ) breakt1= Threading. Thread (target=linux_ls1,args= ("00000000", "root", "192.168.135.105")) t2=threading. Thread (target=linux_ls2,args= ("00000000", "root", "192.168.135.108")) T1.start () T2.start ()
Solution 3.
#-*- coding:utf-8 -*-import threadingimport osdef my_work (PWD,USER,IP): while true: cmd= "sshpass -p "% S ' ssh %[email protected]%s ls " % (PWD,USER,IP) ret=os.popen (CMD) print ret.read () breakclass mythread (Threading. Thread): def __init__ (SELF, PWD,USER,IP): threading. Thread.__init__ (self) self.pwd=pwd self.user=user self.ip=ip #线程启动后, the Self.run () method is executed def run (self): my_work (Self.pwd,  SELF.USER, SELF.IP) # Create a new thread T1t1 = mythread ("00000000", "root", "192.168.135.105") T2 = mythread ("00000000", "root", "192.168.135.108") T1.start () # Start Thread T2.start () print "Thread Start Complete"
Solution 4.
#!/usr/bin/python # encoding=utf-8 # filename: put_files_hdfs.py # allows multiple commands to execute concurrently, such as allowing multiple SCP,FTP,HDFS upload commands to execute concurrently, improving the efficiency of program Operation import osimport threadingdef execcmd (cmd): try: os.system (CMD) except exception, e: print '%s\t run failed, failure reason \r\n%s ' % (cmd,e) if __name__ == ' __main__ ': # List of commands to execute cmds = [' sshpass -p ' 00000000 " ssh [email protected] ls", ' sshpass -p ' 00000000 " ssh [email protected] ls ',] #线程池 threads = [] for cmd in cmds: th = threading. Thread (Target=execcmd, args= (cmd,)) th.start () threads.append (TH) # wait thread run complete for th in threads: th.join ()
Python multi-threaded login remote Linux execute command