python批处理程序,python--批处理--多进程

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")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值