Converted to executable linux script

This commit is contained in:
2018-02-16 21:02:48 +01:00
parent a786528b85
commit c212673b88
Regular → Executable
+167 -165
View File
@@ -1,165 +1,167 @@
# You are free to use and/or change this code for #!/usr/bin/env python3.5
# your own needs.
# You are free to use and/or change this code for
# Original code (c)2018 Jan Lerking # your own needs.
# Program to test various scenarios of
# single thread, multi thread, pool and process # Original code (c)2018 Jan Lerking
# Program to test various scenarios of
import os # single thread, multi thread, pool and process
import sys
import multiprocessing import os
import time import sys
import io import multiprocessing
import cpuinfo import time
import queue import io
import threading import cpuinfo
import time import queue
import threading
cpu_info = cpuinfo.get_cpu_info() import time
num_cores = multiprocessing.cpu_count()
print(num_cores) cpu_info = cpuinfo.get_cpu_info()
num_cores = multiprocessing.cpu_count()
filelist = [] print(num_cores)
exitFlag = 0
queueLock = threading.Lock() filelist = []
workQueue = queue.Queue(0) exitFlag = 0
threads = [] queueLock = threading.Lock()
class myThread (threading.Thread): workQueue = queue.Queue(0)
def __init__(self, threadID, name, q): threads = []
threading.Thread.__init__(self) class myThread (threading.Thread):
self.threadID = threadID def __init__(self, threadID, name, q):
self.name = name threading.Thread.__init__(self)
self.q = q self.threadID = threadID
def run(self): self.name = name
print ("Starting " + self.name) self.q = q
process_data(self.name, self.q) def run(self):
print ("Exiting " + self.name) print ("Starting " + self.name)
process_data(self.name, self.q)
def process_data(threadName, q): print ("Exiting " + self.name)
while not exitFlag:
queueLock.acquire() def process_data(threadName, q):
if not workQueue.empty(): while not exitFlag:
data = q.get() queueLock.acquire()
process_file(data) if not workQueue.empty():
queueLock.release() data = q.get()
print ("%s processing %s" % (threadName, data)) process_file(data)
else: queueLock.release()
queueLock.release() print ("%s processing %s" % (threadName, data))
time.sleep(1) else:
queueLock.release()
def sourcedir_filecnt(sourcedir): time.sleep(1)
### Return the number of files, ending with '.h', in sourcedir - including subdirectories ###
cnt = 0 def sourcedir_filecnt(sourcedir):
global filelist ### Return the number of files, ending with '.h', in sourcedir - including subdirectories ###
for folderName, subfolders, files in os.walk(sourcedir): cnt = 0
for file in files: global filelist
if file.lower().endswith('.h'): for folderName, subfolders, files in os.walk(sourcedir):
cnt += 1 for file in files:
filelist += [folderName+'/'+file] if file.lower().endswith('.h'):
return cnt cnt += 1
filelist += [folderName+'/'+file]
def process_files(sourcedir, destdir): return cnt
global filelist
for f in filelist: def process_files(sourcedir, destdir):
process_file(f) global filelist
for f in filelist:
def process_file(data): process_file(f)
outfile = ''
inputfile = data def process_file(data):
encodings = ['utf-8', 'latin-1', 'windows-1250', 'windows-1252', 'ascii', outfile = ''
'big5', 'big5hkscs', 'cp037', 'cp273', 'cp424', 'cp437', 'cp500', inputfile = data
'cp720', 'cp737', 'cp775', 'cp850', 'cp852', 'cp855', 'cp856', encodings = ['utf-8', 'latin-1', 'windows-1250', 'windows-1252', 'ascii',
'cp857', 'cp858', 'cp860', 'cp861', 'cp862', 'cp863', 'cp864', 'cp865', 'big5', 'big5hkscs', 'cp037', 'cp273', 'cp424', 'cp437', 'cp500',
'cp866', 'cp869', 'cp874', 'cp875', 'cp932', 'cp949', 'cp950', 'cp1006', 'cp720', 'cp737', 'cp775', 'cp850', 'cp852', 'cp855', 'cp856',
'cp1026', 'cp1125', 'cp1140', 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp857', 'cp858', 'cp860', 'cp861', 'cp862', 'cp863', 'cp864', 'cp865',
'cp1255', 'cp1256', 'cp1257', 'cp1258', 'cp65001', 'euc-jp', 'euc-jis-2004', 'cp866', 'cp869', 'cp874', 'cp875', 'cp932', 'cp949', 'cp950', 'cp1006',
'euc-jisx0213', 'euc-kr', 'gb2312', 'gbk', 'gb18030', 'hz', 'iso2022-jp', 'cp1026', 'cp1125', 'cp1140', 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254',
'iso2022-jp-1', 'iso2022-jp-2', 'iso2022-jp-2004', 'iso2022-jp-3', 'cp1255', 'cp1256', 'cp1257', 'cp1258', 'cp65001', 'euc-jp', 'euc-jis-2004',
'iso2022-jp-ext', 'iso2022-kr', 'iso8859-2', 'iso8859-3', 'iso8859-4', 'euc-jisx0213', 'euc-kr', 'gb2312', 'gbk', 'gb18030', 'hz', 'iso2022-jp',
'iso8859-5', 'iso8859-6', 'iso8859-7', 'iso8859-8', 'iso8859-9', 'iso8859-10', 'iso2022-jp-1', 'iso2022-jp-2', 'iso2022-jp-2004', 'iso2022-jp-3',
'iso8859-11', 'iso8859-13', 'iso8859-14', 'iso8859-15', 'iso8859-16', 'johab', 'iso2022-jp-ext', 'iso2022-kr', 'iso8859-2', 'iso8859-3', 'iso8859-4',
'koi8-r', 'koi8-t', 'koi8-u', 'kz1048', 'mac-cyrillic', 'mac-greek', 'iso8859-5', 'iso8859-6', 'iso8859-7', 'iso8859-8', 'iso8859-9', 'iso8859-10',
'mac-iceland', 'mac-latin2', 'mac-roman', 'mac-turkish', 'ptcp154', 'iso8859-11', 'iso8859-13', 'iso8859-14', 'iso8859-15', 'iso8859-16', 'johab',
'shift-jis', 'shift-jis-2004', 'shift-jisx0213', 'utf-32', 'utf-32-be', 'koi8-r', 'koi8-t', 'koi8-u', 'kz1048', 'mac-cyrillic', 'mac-greek',
'utf-32-le', 'utf-16', 'utf-16-be', 'utf-16-le', 'utf-7', 'utf-8-sig'] 'mac-iceland', 'mac-latin2', 'mac-roman', 'mac-turkish', 'ptcp154',
for e in encodings: 'shift-jis', 'shift-jis-2004', 'shift-jisx0213', 'utf-32', 'utf-32-be',
try: 'utf-32-le', 'utf-16', 'utf-16-be', 'utf-16-le', 'utf-7', 'utf-8-sig']
fh = io.open(data, 'r', encoding=e) for e in encodings:
fh.readlines() try:
fh.seek(0) fh = io.open(data, 'r', encoding=e)
except UnicodeDecodeError: fh.readlines()
print('got unicode error with %s , trying different encoding' % e) fh.seek(0)
else: except UnicodeDecodeError:
print('opening the file with encoding: %s ' % e) print('got unicode error with %s , trying different encoding' % e)
break else:
print(os.path.basename(data)) print('opening the file with encoding: %s ' % e)
for lines in fh: break
outfile = outfile+lines print(os.path.basename(data))
fh.close() for lines in fh:
outputfile = os.path.splitext(inputfile)[0]+'.inc' outfile = outfile+lines
outputfile = str(outputfile).replace(sourcedir, destdir) fh.close()
print(outputfile) outputfile = os.path.splitext(inputfile)[0]+'.inc'
if not os.path.exists(os.path.dirname(outputfile)): outputfile = str(outputfile).replace(sourcedir, destdir)
try: print(outputfile)
os.makedirs(os.path.dirname(outputfile)) if not os.path.exists(os.path.dirname(outputfile)):
except OSError as exc: # Guard against race condition try:
if exc.errno != errno.EEXIST: os.makedirs(os.path.dirname(outputfile))
raise except OSError as exc: # Guard against race condition
newfile = open(outputfile, "w") if exc.errno != errno.EEXIST:
newfile.write(outfile) raise
newfile.close() newfile = open(outputfile, "w")
newfile.write(outfile)
def single_thread(): newfile.close()
t1 = time.time()
cnt = sourcedir_filecnt(sourcedir) def single_thread():
print('Files in: '+str(cnt)) t1 = time.time()
process_files(sourcedir, destdir) cnt = sourcedir_filecnt(sourcedir)
print('Single thread process time: '+str(time.time()-t1)) print('Files in: '+str(cnt))
process_files(sourcedir, destdir)
def multi_thread(): print('Single thread process time: '+str(time.time()-t1))
global filelist
global exitFlag def multi_thread():
global filelist
t1 = time.time() global exitFlag
cnt = sourcedir_filecnt(sourcedir)
threadList = ["thread 1", "thread 2", "thread 3", "thread 4", "thread 5"] t1 = time.time()
nameList = filelist cnt = sourcedir_filecnt(sourcedir)
threadID = 0 threadList = ["thread 1", "thread 2", "thread 3", "thread 4", "thread 5"]
nameList = filelist
# Create new threads threadID = 0
for tName in threadList:
thread = myThread(threadID, tName, workQueue) # Create new threads
thread.start() for tName in threadList:
threads.append(thread) thread = myThread(threadID, tName, workQueue)
threadID += 1 thread.start()
threads.append(thread)
# Fill the queue threadID += 1
queueLock.acquire()
for word in nameList: # Fill the queue
workQueue.put(word) queueLock.acquire()
queueLock.release() for word in nameList:
workQueue.put(word)
# Wait for queue to empty queueLock.release()
while not workQueue.empty():
pass # Wait for queue to empty
while not workQueue.empty():
# Notify threads it's time to exit pass
exitFlag = 1
# Notify threads it's time to exit
# Wait for all threads to complete exitFlag = 1
for t in threads:
t.join() # Wait for all threads to complete
print ("Exiting Main Thread") for t in threads:
print('Files in: '+str(cnt)) t.join()
print('Multi thread process time: '+str(time.time()-t1)) print ("Exiting Main Thread")
print('Files in: '+str(cnt))
#sourcedir = 'C:/Users/dksojlg/Documents/gtk+-3.22.26' print('Multi thread process time: '+str(time.time()-t1))
sourcedir = '/usr/include'
#destdir = 'C:/Users/dksojlg/Documents/include' #sourcedir = 'C:/Users/dksojlg/Documents/gtk+-3.22.26'
destdir = '/data_2/include' sourcedir = '/usr/include'
#destdir = 'C:/Users/dksojlg/Documents/include'
print(cpu_info) destdir = '/data_2/include'
single_thread() #2543 files - Single thread process time: 1.3732633590698242
print(cpu_info)
multi_thread() single_thread() #2543 files - Single thread process time: 1.3732633590698242
multi_thread()