美文网首页
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--批处理--多进程

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

  • linux 进程分类

    linux把进程区分为实时进程和非实时进程, 其中非实时进程进一步划分为交互式进程和批处理进程

  • Linux中进程分为哪几类?状态有哪些?

    Linux中进程分为哪几类?在Linux系统中,进程分为:交互进程、批处理进程、监控进程三类,接下来我们通过这篇文...

  • python--批处理--批量获取模块

    根据我之前的那个小改了下;后面计划是将原本的主模块改为执行模块;重新设计一个主模块,然后通过参数,指定各个子模块。...

  • python--批处理--批量发送文件

    目前写这个的目的是为了实现,可以批处理发送的一个模块;后期将各个模块整合到一起,实现更加丰富的功能,类似于ansi...

  • 注释信息合并处理脚本

    将所有的注释信息合并到一块,先写python脚本,再用shell进行批处理test.py 2.批处理,约50个进程

  • 2019-06-20-操作系统

    概述 操作系统经历了哪些阶段 人工 批处理 多道 分时计算首次适应算法、最佳适应算法、最坏适应算法 进程管理 进程...

  • python--进程、线程简析

    引入进程和线程的概念及区别 1、线程的基本概念 概念:线程是进程中执行运算的最小单位,是进程的一个实体,是被系统独...

  • windows 杀占用端口的进程

    先找到占用端口的进程号 grep 命令原生是没有的,通过写批处理命令的方式支持的,可以用findstr命令 杀进程...

  • 计算机操作系统——死锁

    1、死锁的概念 在多道批处理系统,由于多个进程的并发执行,改善了系统资源的利用率和提高了系统的并发处理能力,然而多...

网友评论

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

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