什么是进程?

进程,就是程序的一个运行状态。

 

什么是线程?

为什么要使用线程?

 

线程,是进程内部的“执行单元”。

一个进程,可以包含多个线程,每个线程又可以执行不同的代码。

即,通过多个线程,可以使一个进程同时执行多个功能!


前提安装sshpass

解法1.

#!/usr/bin/python
#-*- coding:utf-8
import threading
import os
def linux_ls1():
        while True:
                cmd="sshpass -p '00000000' ssh root@192.168.135.108 ls"
                ret=os.popen(cmd)
                print ret.read()
                break
def linux_ls2():
        while True:
                cmd="sshpass -p '00000000' ssh root@192.168.135.105 ls"
                ret=os.popen(cmd)
                print ret.read()
                break
t1=threading.Thread(target=linux_ls1)
t2=threading.Thread(target=linux_ls2)
t1.start()
t2.start()


解法2.

#!/usr/bin/python
#-*- coding:utf-8
import threading
import os
def linux_ls1(pwd,user,ip):
        while True:
                cmd="sshpass -p '%s' ssh %s@%s ls" %(pwd,user,ip)
                ret=os.popen(cmd)
                print ret.read()
                break
def linux_ls2(pwd,user,ip):
        while True:
                cmd="sshpass -p '%s' ssh %s@%s ls" %(pwd,user,ip)
                ret=os.popen(cmd)
                print ret.read()
                break
t1=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()


解法3.

#-*- coding:utf-8 -*-
import threading
import os
def my_work(pwd,user,ip):
    while True:
         cmd="sshpass -p '%s' ssh %s@%s ls" %(pwd,user,ip)
         ret=os.popen(cmd)
         print ret.read()
         break
class MyThread(threading.Thread):
        def __init__(self, pwd,user,ip):
                threading.Thread.__init__(self)
                self.pwd=pwd
                self.user=user
                self.ip=ip
        #线程启动后,会执行self.run()方法
        def run(self):
                my_work(self.pwd, self.user, self.ip)
# 创建新线程t1
t1 = MyThread("00000000", "root","192.168.135.105")
t2 = MyThread("00000000", "root","192.168.135.108")
t1.start()    #启动线程
t2.start()
print "线程启动完毕"


解法4.

#!/usr/bin/python
#-*- coding:UTF-8 -*-
import threading
import time
import os
class MyThread(threading.Thread):
        def __init__(self,ip):
                threading.Thread.__init__(self)
                self.ip=ip
        def run(self):
                cmd="sshpass -p '00000000' ssh root@%s ls" %self.ip
                ret=os.popen(cmd)
                print ret.read()
                time.sleep(3)
if __name__== '__main__':
        t1=MyThread("192.168.135.105")
        t2=MyThread("192.168.135.108")
        t1.start()
        t2.start()


解法5.

#!/usr/bin/python  
# encoding=utf-8  
# Filename: put_files_hdfs.py  
# 让多条命令并发执行,如让多条scp,ftp,hdfs上传命令并发执行,提高程序运行效率  
import os
import threading
def execCmd(cmd):
    try:
        os.system(cmd)
    except Exception, e:
        print '%s\t 运行失败,失败原因\r\n%s' % (cmd,e)
if __name__ == '__main__':
    # 需要执行的命令列表  
    cmds = ['sshpass -p "00000000" ssh root@192.168.135.108 ls','sshpass -p "00000000" ssh root@192.168.135.105 ls',]
    #线程池  
    threads = []
    for cmd in cmds:
        th = threading.Thread(target=execCmd, args=(cmd,))
        th.start()
        threads.append(th)
    # 等待线程运行完毕  
    for th in threads:
        th.join()