Added multi threading

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