diff --git a/multiprocessing_test.py b/multiprocessing_test.py new file mode 100644 index 0000000..577db25 --- /dev/null +++ b/multiprocessing_test.py @@ -0,0 +1,69 @@ +# You are free to use and/or change this code for +# your own needs. + +# Original code (c)2018 Jan Lerking +# Program to test various scenarios of +# single thread, multi thread, pool and process + +import os +import sys +import multiprocessing +import time +import magic + +num_cores = multiprocessing.cpu_count() +print(num_cores) + +filelist = [] + +def sourcedir_filecnt(sourcedir): + ### Return the number of files, ending with '.h', in sourcedir - including subdirectories ### + cnt = 0 + global filelist + for folderName, subfolders, files in os.walk(sourcedir): + for file in files: + if file.lower().endswith('.h'): + cnt += 1 + filelist += [folderName+'/'+file] + print(folderName+'/'+file) + #print(filelist) + return cnt + +def process_files(sourcedir, destdir): + global filelist + outfile = '' + for f in filelist: + inputfile = f + blob = open(f).read() + m = magic.open(magic.MAGIC_MIME_ENCODING) + m.load() + encoding = m.buffer(blob) # "utf-8" "us-ascii" etc + open(f, encoding=str(encoding)) + print(os.path.basename(f)) + for lines in filehandle: + print(lines) + outfile = outfile+lines + fh.close() + outputfile = os.path.splitext(inputfile)[0]+'.inc' + outputfile = str(outputfile).replace(sourcedir, destdir) + print(outputfile) + if not os.path.exists(os.path.dirname(outputfile)): + try: + os.makedirs(os.path.dirname(outputfile)) + except OSError as exc: # Guard against race condition + if exc.errno != errno.EEXIST: + raise + newfile = open(outputfile, "w") + newfile.write(outfile) + newfile.close() + +def single_thread(): + t1 = time.time() + sourcedir_filecnt(sourcedir) + process_files(sourcedir, destdir) + print('Single thread process time: '+str(time.time()-t1)) + +sourcedir = 'C:/Users/dksojlg/Documents/gtk+-3.22.26' +destdir = 'C:/Users/dksojlg/Documents/include' + +single_thread() \ No newline at end of file diff --git a/thread_priority_queue.py b/thread_priority_queue.py new file mode 100644 index 0000000..08daa57 --- /dev/null +++ b/thread_priority_queue.py @@ -0,0 +1,59 @@ +from Queue import Queue +import threading +import time + +exitFlag = 0 + +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() + queueLock.release() + print ("%s processing %s" % (threadName, data)) + else: + queueLock.release() + time.sleep(1) + +threadList = ["Thread-1", "Thread-2", "Thread-3"] +nameList = ["One", "Two", "Three", "Four", "Five"] +queueLock = threading.Lock() +workQueue = Queue.Queue(10) +threads = [] +threadID = 1 + +# 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") \ No newline at end of file