-
Notifications
You must be signed in to change notification settings - Fork 1
/
BatchScriptThreaded.py
96 lines (81 loc) · 2.81 KB
/
BatchScriptThreaded.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/python
# Filename : var.py
import getopt
import threading
import time
import sys
import os
# Default arguments
Script = sys.argv[1]
current = os.getcwd()
def StringName(num, argv):
oString = ''
try:
opts, args = getopt.getopt(argv,"hn:m:z:p:",["name=","formatMinus=","formatZero=","formatPlus="])
except getopt.GetoptError:
print 'python BatchScript.py ScriptName start stop -n <inputfile> -m <formatMinus> -z <formatZero> -p <formatPlus>'
sys.exit(2)
for opt, arg in opts:
if opt in ("-n", "--name"):
name = arg
oString = oString + ' ' + name
elif opt in ("-m", "--formatMinus"):
iFile = arg
oString = oString + ' ' + iFile
val = num-1
oString = oString % (val)
elif opt in ("-z", "--formatZero"):
iFile = arg
oString = oString + ' ' + iFile
oString = oString % (num)
elif opt in ("-p", "--formatPlus"):
iFile = arg
oString = oString + ' ' + iFile
val = num+1
oString = oString % (val)
return oString
class myThread (threading.Thread):
def __init__(self, threadID, name, startt, increment, stop):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.startt = startt
self.stop = stop
self.increment = increment
def run(self):
print "Starting " + self.name
# Get lock to synchronize threads
#threadLock.acquire()
print_time(self.name, self.startt, self.increment, self.stop)
# Free lock to release next thread
#threadLock.release()
# Define a function for the thread
def print_time( threadName, startt, increment, stop ):
print "%s" % ( threadName )
# Define command
for seg in range(startt, stop, increment):
oString = StringName(seg, sys.argv[4:])
Command = "%s%s" % (Script, oString)
print Command
os.system(Command)
# Create twelve threads as follows
threadLock = threading.Lock()
threads = []
if __name__ == "__main__":
if ( len(sys.argv) < 4):
print 'BatchScriptThreaded.py ScriptName start stop -n <inputfile> -m <formatMinus> -z <formatZero> -p <formatPlus>'
print 'NOTE: Use %d as regular expression to indicate integer wildcard in strings'
else:
start = int(sys.argv[2])
stop = int(sys.argv[3])
numOfThreads = 32
stepsize = numOfThreads
# Create new threads
for i in range(0,numOfThreads):
thread = myThread(i, "Thread-"+str(i), start+i, stepsize, stop)
thread.start()
threads.append(thread)
# Wait for all threads to complete
for t in threads:
t.join()
print "Exiting Main Thread"