美文网首页
python--批处理--多进程

python--批处理--多进程

作者: w_dll | 来源:发表于2020-07-21 21:31 被阅读0次

    2020-07-23 更新,增加线程自定义

    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    
    import threading
    import time
    import sys
    import os
    import shutil
    
    
    class myThread (threading.Thread):
        def __init__(self, threadID, name, counter):
            threading.Thread.__init__(self)
            self.threadID = threadID
            self.name = name
            self.counter = counter
        def run(self):
            this_str="start :" + self.name
            print (this_str)
            os.system(('scp -q log/tmp.sh root@%s:/home') % (str(self.name)))
            #res = os.system(('ssh root@%s "cd /home;bash tmp.sh; rm -f tmp.sh"') % (str(self.name)))
            res = os.popen(('ssh root@%s "cd /home;bash tmp.sh; rm -f tmp.sh"') % (str(self.name))).read()
            this_str = "end :" + self.name + '\n' + str(res) + '\n\n'
            print (this_str)
    
    exec_file = sys.argv[1]
    source = str(exec_file)
    target = 'log/tmp.sh'
    shutil.copy(source, target)
    
    
    ip_lists = []
    ip_file = open('ip.txt', 'rb')
    for li in ip_file :
      if li :
        li = str(li).split()
        li = li[0]
        ip_lists.append(str(li))
    ip_file.close()
    
    max_tasks = input('请输入并行个数:')
    while ip_lists :
      this_ip_lists = []
      for li in range(max_tasks) :
        if ip_lists :
          this_ip = ip_lists.pop(0)
          this_ip_lists.append(str(this_ip))
      this_threads = []
      for ip in this_ip_lists :
        t_name = str(ip)
        t_name = myThread(t_name, t_name, 1)
        this_threads.append(t_name)
      [ thr.start() for thr in this_threads ]
      [ thr.join() for thr in this_threads ]
      print('-----------------------------------------')
    

    初始

    #!/usr/bin/python
    
    import threading
    import time
    import sys
    import os
    import shutil
    
    
    class myThread (threading.Thread):
        def __init__(self, threadID, name, counter):
            threading.Thread.__init__(self)
            self.threadID = threadID
            self.name = name
            self.counter = counter
        def run(self):
            this_str="start :"+self.name
            print (this_str)
            os.system(('scp -q log/tmp.sh root@%s:/home') % (str(self.name)))
            os.system(('ssh root@%s "cd /home;bash tmp.sh; rm -f tmp.sh"') % (str(self.name)))
            this_str="end :"+self.name
            print (this_str)
    
    exec_file = sys.argv[1]
    source = str(exec_file)
    target = 'log/tmp.sh'
    shutil.copy(source, target)
    
    
    ip_lists = []
    ip_file = open('ip.txt', 'rb')
    for li in ip_file :
      if li :
        li = str(li).split()
        li = li[0]
        ip_lists.append(str(li))
    ip_file.close()
    
    
    for ip in ip_lists :
      t_name = str(ip)
      t_name = myThread(t_name, t_name, 1)
      t_num = threading.activeCount()
      this_str = "current num :" + str(t_num)
      print(this_str)
      t_name.start()
    
    
    print ("exit main")
    

    相关文章

      网友评论

          本文标题:python--批处理--多进程

          本文链接:https://www.haomeiwen.com/subject/itzwkktx.html