-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbatch.py
61 lines (50 loc) · 1.46 KB
/
batch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python
# encoding=utf-8
# Filename: batch.py
import threading
import datetime
import time
import os
class ThreadImpl(threading.Thread):
def __init__(self, cmd):
threading.Thread.__init__(self)
self._cmd = cmd
def execCmd(self, cmd):
try:
print "命令%s开始运行%s\r\n" % (cmd,datetime.datetime.now())
os.system(cmd)
print "命令%s结束运行%s\r\n" % (cmd,datetime.datetime.now())
except Exception, e:
print '%s\t 运行失败,失败原因\r\n%s\r\n' % (cmd,e)
def run(self):
global total, mutex
# 打印线程名
print threading.currentThread().getName()
self.execCmd(self._cmd)
mutex.acquire()
total = total + 1
mutex.release()
if __name__ == '__main__':
#定义全局变量
global total, mutex
total = 0
# 创建锁
mutex = threading.Lock()
print "程序开始运行%s\r\n" % datetime.datetime.now()
#定义线程池
threads = []
# 创建线程对象
fh = open("tel.txt")
#for x in xrange(0, num):
for line in fh.readlines():
cmd = ' '.join(['php run.php', line])
threads.append(ThreadImpl(cmd))
# 启动线程
for t in threads:
t.start()
# 等待子线程结束
for t in threads:
t.join()
# 打印执行结果
print total
print "程序结束运行%s" % datetime.datetime.now()