Added multiprocessing testing

This commit is contained in:
2018-02-19 04:36:37 +01:00
parent 7685b8c6a8
commit e311c278e7
6 changed files with 181 additions and 8 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -27,7 +27,7 @@ class h2incGUI:
self.filecnt = 0
self.infofolder = 'Number of folders: 0'
self.infofile = 'Number of headers: 0'
self.currentfile = 'Current file: '
#self.currentfile = 'Current file: '
self.totprogress = 'Total progress: '
self.sourcedir.set('Select source directory!')
self.destdir.set('Select destination directory!')
@@ -91,13 +91,13 @@ class h2incGUI:
self.progressframe.grid(row=4, rowspan=2, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5)
self.progressframe.grid_columnconfigure(1, weight=1)
self.cfilelabel = Label(self.progressframe, text=self.currentfile)
self.cfilelabel.grid(row=6, column=0, sticky=W, padx=5, pady=5)
self.cfilelabel.config(state=DISABLED)
#self.cfilelabel = Label(self.progressframe, text=self.currentfile)
#self.cfilelabel.grid(row=6, column=0, sticky=W, padx=5, pady=5)
#self.cfilelabel.config(state=DISABLED)
self.cfileprogress = ttk.Progressbar(self.progressframe, orient='horizontal', mode='determinate')
self.cfileprogress.grid(row=7, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5)
self.cfileprogress.config(variable=self.cfilevar, maximum=3)
#self.cfileprogress = ttk.Progressbar(self.progressframe, orient='horizontal', mode='determinate')
#self.cfileprogress.grid(row=7, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5)
#self.cfileprogress.config(variable=self.cfilevar, maximum=3)
self.totallabel = Label(self.progressframe, text=self.totprogress)
self.totallabel.grid(row=8, column=0, sticky=W, padx=5, pady=5)
@@ -154,7 +154,7 @@ class h2incGUI:
raise
destdir.set(dest)
print ('Destination directory: ', destdir.get())
process_files(source, dest)
process_files(self, source, dest)
def cfileprogress_update(cnt):
self.cfilevar = cnt

29
mp_counter.py Normal file
View File

@@ -0,0 +1,29 @@
import time
from multiprocessing import Process, Value, Lock
class Counter(object):
def __init__(self, initval=0):
self.val = Value('i', initval)
self.lock = Lock()
def increment(self):
with self.lock:
self.val.value += 1
def value(self):
with self.lock:
return self.val.value
def func(counter):
for i in range(50):
time.sleep(0.01)
counter.increment()
if __name__ == '__main__':
counter = Counter(0)
procs = [Process(target=func, args=(counter,)) for i in range(10)]
for p in procs: p.start()
for p in procs: p.join()
print (counter.value())

40
mp_test.py Normal file
View File

@@ -0,0 +1,40 @@
import tkinter as tk
from tkinter import ttk
import tkinter.messagebox
import time # to simulate long-running function
from threading import Thread
class RedactionSolutions:
def __init__(self, master):
tk.Button(master, text="Run RS Wizard", fg='green', command=self.run).grid(row=5)
def long_running_function(self):
#self.working = tk.Label(root, text='In Progress. Please Wait...')
self.working = ttk.Progressbar(root, orient='horizontal', mode='determinate')
self.working.config(value=0, maximum=10)
self.working.grid(row=6, columnspan=2)
for x in range(10):
print(x)
self.working.config(value=x)
time.sleep(1)
restart = tkinter.messagebox.askquestion('RS Wizard', 'Redaction complete! See file location for result.\nWould you like to redact another file?')
if restart == 'yes':
#self.reset()
self.working.grid_forget()
else:
root.destroy()
def run(self):
Thread(target=self.long_running_function).start()
root = tk.Tk()
root.wm_title("RS Wizard")
RedactionSolutions(root)
root.mainloop()

View File

@@ -19,6 +19,110 @@ import time
import random
import string
import shutil
from tkinter import Tk, ttk, Label, Button, LabelFrame
from tkinter import filedialog, Entry, Checkbutton
from tkinter import Grid, StringVar, DoubleVar
from tkinter import N, E, S, W
from tkinter import DISABLED, NORMAL
from os import errno
import os
from os.path import expanduser
import platform
class file_progress:
def __init__(self, master):
self.sourcedir = StringVar()
self.destdir = StringVar()
self.addinc = StringVar()
self.cfilevar = DoubleVar()
self.totalvar = 0
self.filecnt = 0
self.infofolder = 'Number of folders: 0'
self.infofile = 'Number of headers: 0'
self.currentfile = 'Current file: '
self.totprogress = 'Total progress: '
self.sourcedir.set('Select source directory!')
self.destdir.set('Select destination directory!')
self.master = master
self.master.title('File progress')
self.master.grid_columnconfigure(1, weight=1)
self.frame = LabelFrame(master, text='Progress')
self.frame.grid(row=0, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5)
self.frame.grid_columnconfigure(1, weight=1)
self.totallabel = Label(self.frame, text=self.totprogress)
self.totallabel.grid(row=8, column=0, sticky=W, padx=5, pady=5)
self.totallabel.config(state=DISABLED)
self.totalprogress = ttk.Progressbar(self.frame, orient='horizontal', mode='determinate')
self.totalprogress.grid(row=9, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5)
self.totalprogress.config(variable=self.totalvar, maximum=self.filecnt)
def select_sourcedir(self, sourcedir):
root.directory = os.path.abspath(filedialog.askdirectory())
if root.directory:
sourcedir.set(root.directory)
filecnt = sourcedir_filecnt(root.directory)
if filecnt > 0:
tempstr = 'Number of headers: '+str(filecnt)
temptot = 'Total progress: 0 of '+str(filecnt)
print ('Source directory: ', sourcedir.get())
self.destlabel.config(state=NORMAL)
self.destentry.config(state=NORMAL)
self.destdir_button.config(state=NORMAL)
self.infofiles.config(text=tempstr)
self.totallabel.config(text=temptot)
self.filecnt = filecnt
foldercnt = sourcedir_foldercnt(root.directory)
if foldercnt > 0:
tempstr = 'Number of folders: '+str(foldercnt)
self.infofolders.config(text=tempstr)
def select_destdir(self, destdir):
root.directory = filedialog.askdirectory()
if root.directory:
destdir.set(root.directory)
print ('Destination directory: ', destdir.get())
self.incchkbox.config(state=NORMAL)
self.infofolders.config(state=NORMAL)
self.infofiles.config(state=NORMAL)
self.translate_button.config(state=NORMAL)
self.cfilelabel.config(state=NORMAL)
self.totallabel.config(state=NORMAL)
def translate(self, destdir, sourcedir, addinc):
doinc = addinc.get()
dest = destdir.get()
source = sourcedir.get()
if doinc == 'yes':
dest = dest+'/include'
print(os.path.exists(os.path.dirname(dest)))
if not os.path.exists(os.path.dirname(dest)):
try:
os.makedirs(os.path.dirname(dest))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
destdir.set(dest)
print ('Destination directory: ', destdir.get())
process_files(source, dest)
def cfileprogress_update(cnt):
self.cfilevar = cnt
def currentfile_update(current):
self.currentfile = 'Current file: '+current
root = Tk()
root.update()
#root.minsize(350, 210)
#width = (root.winfo_screenwidth()/2)-(350/2)
#height = (root.winfo_screenheight()/2)-(210/2)
#root.geometry('+%d+%d' % (width, height))
root.resizable(False, False)
f_progress = file_progress(root)
root.mainloop()
cpu_info = cpuinfo.get_cpu_info()
num_cores = mp.cpu_count()