From a642073d3a5f5c7546604337af29c4f22dc1f7d8 Mon Sep 17 00:00:00 2001 From: Lerking <33354709+Lerking@users.noreply.github.com> Date: Tue, 20 Feb 2018 11:34:52 +0100 Subject: [PATCH] Add files via upload --- Multiprocessing test/calc_pi.py | 163 ++++++++++++++++++++++++++ Multiprocessing test/calc_pi_linux.py | 133 +++++++++++++++++++++ Multiprocessing test/calc_pi_win.py | 134 +++++++++++++++++++++ Multiprocessing test/gui_mp.py | 43 +++++++ Multiprocessing test/mp_counter.py | 4 +- Multiprocessing test/tkinter_mp.py | 51 +++++++- 6 files changed, 520 insertions(+), 8 deletions(-) create mode 100644 Multiprocessing test/calc_pi.py create mode 100644 Multiprocessing test/calc_pi_linux.py create mode 100644 Multiprocessing test/calc_pi_win.py create mode 100644 Multiprocessing test/gui_mp.py diff --git a/Multiprocessing test/calc_pi.py b/Multiprocessing test/calc_pi.py new file mode 100644 index 0000000..b56848e --- /dev/null +++ b/Multiprocessing test/calc_pi.py @@ -0,0 +1,163 @@ +#!/usr/bin/env python3 + +GUI_WIN = 0 +GUI_LINUX = 1 + +import sys +if sys.platform == 'win32': + GUI = GUI_WIN +else: + GUI = GUI_LINUX + +from tkinter import (Tk, BOTH, Text, E, W, S, N, END, + NORMAL, DISABLED, StringVar) +from tkinter.ttk import Frame, Label, Button, Progressbar, Entry +from tkinter import scrolledtext + +if GUI == GUI_WIN: + from multiprocessing import Process, Manager, Queue + from queue import Empty +else: + from multiprocessing import Queue, Process + import queue + +from decimal import Decimal, getcontext + +DELAY1 = 80 +DELAY2 = 20 + +if GUI == GUI_WIN: + # Queue must be global + q = Queue() + +class commonUI(Frame): + def __init__(self, parent): + Frame.__init__(self, parent) + self.parent = parent + self.initUI() + + def initUI(self): + + self.parent.title("Pi computation") + self.pack(fill=BOTH, expand=True) + + self.grid_columnconfigure(4, weight=1) + self.grid_rowconfigure(3, weight=1) + + lbl1 = Label(self, text="Digits:") + lbl1.grid(row=0, column=0, sticky=E, padx=10, pady=10) + + self.ent1 = Entry(self, width=10) + self.ent1.insert(END, "4000") + self.ent1.grid(row=0, column=1, sticky=W) + + lbl2 = Label(self, text="Accuracy:") + lbl2.grid(row=0, column=2, sticky=E, padx=10, pady=10) + + self.ent2 = Entry(self, width=10) + self.ent2.insert(END, "100") + self.ent2.grid(row=0, column=3, sticky=W) + + self.startBtn = Button(self, text="Start", command=self.onStart) + self.startBtn.grid(row=1, column=0, padx=10, pady=5, sticky=W) + + self.pbar = Progressbar(self, mode='indeterminate') + self.pbar.grid(row=1, column=1, columnspan=3, sticky=W+E) + + self.txt = scrolledtext.ScrolledText(self) + self.txt.grid(row=2, column=0, rowspan=4, padx=10, pady=5, columnspan=5, sticky=E+W+S+N) + + def onStart(self): + + self.startBtn.config(state=DISABLED) + self.txt.delete("1.0", END) + + digits = int(self.ent1.get()) + accuracy = int(self.ent2.get()) + + self.p1 = Process(target=generatePi, args=(q, digits, accuracy)) + self.p1.start() + self.pbar.start(DELAY2) + self.after(DELAY1, self.onGetValue) + + + def onGetValue(self): + + if (self.p1.is_alive()): + + self.after(DELAY1, self.onGetValue) + return + else: + + try: + + self.txt.insert('end', q.get(0)) + self.txt.insert('end', "\n") + self.pbar.stop() + self.startBtn.config(state=NORMAL) + + except Empty: + print("queue is empty") + +class win_Example(Frame): + + def __init__(self, parent): + Frame.__init__(self, parent, name="frame") + commonUI(parent) + +if GUI == GUI_WIN: + # Generate function must be a top-level module funtion + def generatePi(q, digs, acc): + + getcontext().prec = digs + + pi = Decimal(0) + k = 0 + n = acc + + while k < n: + pi += (Decimal(1)/(16**k))*((Decimal(4)/(8*k+1)) - \ + (Decimal(2)/(8*k+4)) - (Decimal(1)/(8*k+5))- \ + (Decimal(1)/(8*k+6))) + k += 1 + + q.put(pi) + +class linux_Example(Frame): + + def __init__(self, parent, q): + Frame.__init__(self, parent) + + self.queue = q + commonUI(parent) + + def generatePi(self, queue): + + getcontext().prec = self.digits + + pi = Decimal(0) + k = 0 + n = self.accuracy + + while k < n: + pi += (Decimal(1)/(16**k))*((Decimal(4)/(8*k+1)) - \ + (Decimal(2)/(8*k+4)) - (Decimal(1)/(8*k+5))- \ + (Decimal(1)/(8*k+6))) + k += 1 + print (self.p1.is_alive()) + + queue.put(pi) + print("end") + +def main(): + if GUI == GUI_LINUX: + q = Queue() + + root = Tk() + root.geometry("400x350+300+300") + app = win_Example(root) if GUI == GUI_WIN else linux_Example(root) + root.mainloop() + + +if __name__ == '__main__': + main() diff --git a/Multiprocessing test/calc_pi_linux.py b/Multiprocessing test/calc_pi_linux.py new file mode 100644 index 0000000..b6ef69d --- /dev/null +++ b/Multiprocessing test/calc_pi_linux.py @@ -0,0 +1,133 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" +ZetCode Tkinter e-book + +This script produces a long-running task of calculating +a large Pi number, while keeping the GUI responsive. + +Author: Jan Bodnar +Last modified: January 2016 +Website: www.zetcode.com +""" + +from tkinter import (Tk, BOTH, Text, E, W, S, N, END, + NORMAL, DISABLED, StringVar) +from tkinter.ttk import Frame, Label, Button, Progressbar, Entry +from tkinter import scrolledtext + +from multiprocessing import Queue, Process +import queue +from decimal import Decimal, getcontext + +DELAY1 = 80 +DELAY2 = 20 + +class Example(Frame): + + def __init__(self, parent, q): + Frame.__init__(self, parent) + + self.queue = q + self.parent = parent + self.initUI() + + + def initUI(self): + + self.parent.title("Pi computation") + self.pack(fill=BOTH, expand=True) + + self.grid_columnconfigure(4, weight=1) + self.grid_rowconfigure(3, weight=1) + + lbl1 = Label(self, text="Digits:") + lbl1.grid(row=0, column=0, sticky=E, padx=10, pady=10) + + self.ent1 = Entry(self, width=10) + self.ent1.insert(END, "4000") + self.ent1.grid(row=0, column=1, sticky=W) + + lbl2 = Label(self, text="Accuracy:") + lbl2.grid(row=0, column=2, sticky=E, padx=10, pady=10) + + self.ent2 = Entry(self, width=10) + self.ent2.insert(END, "100") + self.ent2.grid(row=0, column=3, sticky=W) + + self.startBtn = Button(self, text="Start", + command=self.onStart) + self.startBtn.grid(row=1, column=0, padx=10, pady=5, sticky=W) + + self.pbar = Progressbar(self, mode='indeterminate') + self.pbar.grid(row=1, column=1, columnspan=3, sticky=W+E) + + self.txt = scrolledtext.ScrolledText(self) + self.txt.grid(row=2, column=0, rowspan=4, padx=10, pady=5, + columnspan=5, sticky=E+W+S+N) + + + def onStart(self): + + self.startBtn.config(state=DISABLED) + self.txt.delete("1.0", END) + + self.digits = int(self.ent1.get()) + self.accuracy = int(self.ent2.get()) + + self.p1 = Process(target=self.generatePi, args=(self.queue,)) + self.p1.start() + self.pbar.start(DELAY2) + self.after(DELAY1, self.onGetValue) + + + def onGetValue(self): + + if (self.p1.is_alive()): + + self.after(DELAY1, self.onGetValue) + return + else: + + try: + self.txt.insert('end', self.queue.get(0)) + self.txt.insert('end', "\n") + self.pbar.stop() + self.startBtn.config(state=NORMAL) + + except queue.Empty: + print("queue is empty") + + + def generatePi(self, queue): + + getcontext().prec = self.digits + + pi = Decimal(0) + k = 0 + n = self.accuracy + + while k < n: + pi += (Decimal(1)/(16**k))*((Decimal(4)/(8*k+1)) - \ + (Decimal(2)/(8*k+4)) - (Decimal(1)/(8*k+5))- \ + (Decimal(1)/(8*k+6))) + k += 1 + print (self.p1.is_alive()) + + queue.put(pi) + print("end") + + +def main(): + + q = Queue() + + root = Tk() + root.geometry("400x350+300+300") + app = Example(root, q) + root.mainloop() + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/Multiprocessing test/calc_pi_win.py b/Multiprocessing test/calc_pi_win.py new file mode 100644 index 0000000..b712964 --- /dev/null +++ b/Multiprocessing test/calc_pi_win.py @@ -0,0 +1,134 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" +ZetCode Tkinter e-book + +This script produces a long-running task of calculating +a large Pi number, while keeping the GUI responsive. +This is an example written for Windows. + +Author: Jan Bodnar +Last modified: January 2016 +Website: www.zetcode.com +""" + +from tkinter import (Tk, BOTH, Text, E, W, S, N, END, + NORMAL, DISABLED, StringVar) +from tkinter.ttk import Frame, Label, Button, Progressbar, Entry +from tkinter import scrolledtext + +from multiprocessing import Process, Manager, Queue +from queue import Empty +from decimal import Decimal, getcontext + + +DELAY1 = 80 +DELAY2 = 20 + +# Queue must be global +q = Queue() + +class Example(Frame): + + def __init__(self, parent): + Frame.__init__(self, parent, name="frame") + + self.parent = parent + self.initUI() + + + def initUI(self): + + self.parent.title("Pi computation") + self.pack(fill=BOTH, expand=True) + + self.grid_columnconfigure(4, weight=1) + self.grid_rowconfigure(3, weight=1) + + lbl1 = Label(self, text="Digits:") + lbl1.grid(row=0, column=0, sticky=E, padx=10, pady=10) + + self.ent1 = Entry(self, width=10) + self.ent1.insert(END, "4000") + self.ent1.grid(row=0, column=1, sticky=W) + + lbl2 = Label(self, text="Accuracy:") + lbl2.grid(row=0, column=2, sticky=E, padx=10, pady=10) + + self.ent2 = Entry(self, width=10) + self.ent2.insert(END, "100") + self.ent2.grid(row=0, column=3, sticky=W) + + self.startBtn = Button(self, text="Start", + command=self.onStart) + self.startBtn.grid(row=1, column=0, padx=10, pady=5, sticky=W) + + self.pbar = Progressbar(self, mode='indeterminate') + self.pbar.grid(row=1, column=1, columnspan=3, sticky=W+E) + + self.txt = scrolledtext.ScrolledText(self) + self.txt.grid(row=2, column=0, rowspan=4, padx=10, pady=5, + columnspan=5, sticky=E+W+S+N) + + + def onStart(self): + + self.startBtn.config(state=DISABLED) + self.txt.delete("1.0", END) + + digits = int(self.ent1.get()) + accuracy = int(self.ent2.get()) + + self.p1 = Process(target=generatePi, args=(q, digits, accuracy)) + self.p1.start() + self.pbar.start(DELAY2) + self.after(DELAY1, self.onGetValue) + + + def onGetValue(self): + + if (self.p1.is_alive()): + + self.after(DELAY1, self.onGetValue) + return + else: + + try: + + self.txt.insert('end', q.get(0)) + self.txt.insert('end', "\n") + self.pbar.stop() + self.startBtn.config(state=NORMAL) + + except Empty: + print("queue is empty") + +# Generate function must be a top-level module funtion +def generatePi(q, digs, acc): + + getcontext().prec = digs + + pi = Decimal(0) + k = 0 + n = acc + + while k < n: + pi += (Decimal(1)/(16**k))*((Decimal(4)/(8*k+1)) - \ + (Decimal(2)/(8*k+4)) - (Decimal(1)/(8*k+5))- \ + (Decimal(1)/(8*k+6))) + k += 1 + + q.put(pi) + + +def main(): + + root = Tk() + root.geometry("400x350+300+300") + app = Example(root) + root.mainloop() + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/Multiprocessing test/gui_mp.py b/Multiprocessing test/gui_mp.py new file mode 100644 index 0000000..01001b0 --- /dev/null +++ b/Multiprocessing test/gui_mp.py @@ -0,0 +1,43 @@ +# Test Code for Tkinter with threads +import tkinter as Tk +import multiprocessing +from Queue import Empty, Full +import time + +class GuiApp(object): + def __init__(self,q): + self.root = Tk.Tk() + self.root.geometry('300x100') + self.text_wid = Tk.Text(self.root,height=100,width=100) + self.text_wid.pack(expand=1,fill=Tk.BOTH) + self.root.after(100,self.CheckQueuePoll,q) + + def CheckQueuePoll(self,c_queue): + try: + str = c_queue.get(0) + self.text_wid.insert('end',str) + except Empty: + pass + finally: + self.root.after(100, self.CheckQueuePoll, c_queue) + +# Data Generator which will generate Data +def GenerateData(q): + for i in range(10): + print("Generating Some Data, Iteration %s" %(i)) + time.sleep(2) + q.put("Some Data from iteration %s \n" %(i)) + + +if __name__ == '__main__': +# Queue which will be used for storing Data + + q = multiprocessing.Queue() + q.cancel_join_thread() # or else thread that puts data will not term + gui = GuiApp(q) + t1 = multiprocessing.Process(target=GenerateData,args=(q,)) + t1.start() + gui.root.mainloop() + + t1.join() + t2.join() \ No newline at end of file diff --git a/Multiprocessing test/mp_counter.py b/Multiprocessing test/mp_counter.py index b4b112c..4dac468 100644 --- a/Multiprocessing test/mp_counter.py +++ b/Multiprocessing test/mp_counter.py @@ -15,8 +15,8 @@ class Counter(object): return self.val.value def func(counter): - for i in range(50): - time.sleep(0.01) + for i in range(500): + time.sleep(0.001) counter.increment() if __name__ == '__main__': diff --git a/Multiprocessing test/tkinter_mp.py b/Multiprocessing test/tkinter_mp.py index f132443..a4a290d 100644 --- a/Multiprocessing test/tkinter_mp.py +++ b/Multiprocessing test/tkinter_mp.py @@ -1,18 +1,57 @@ from multiprocessing import Process, Value, Lock from tkinter import Tk 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 +import os +import sys + +dirname = 'C:\\Users\\dksojlg\\Documents' + +platform = sys.platform +print(platform) class testGUI: def __init__(self, master, initval=0): self.progress = Value('i', initval) + self.dircount = Value('i', initval) + self.foldercnt = 'Folders: {}'.format(self.dircount.value) self.lock = Lock() - def read_dir(dirname): - - def get_directories(self): - p = Process(target=read_dir, args=dirname) - p.start() \ No newline at end of file + self.master = master + self.master.title('Multiprocess progressbar test!') + self.master.grid_columnconfigure(1, weight=1) + + self.frame = LabelFrame(master, text='Folder count') + 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.sourcelabel = Label(self.frame, text=self.foldercnt) + self.sourcelabel.grid(row=0, column=0, sticky=E, padx=(5, 1), pady=5) + + self.sourcedir_button = Button(self.frame, text="Count folders...", command= lambda: get_directories(self)) + self.sourcedir_button.grid(row=0, column=2, sticky=W, padx=(3, 5), pady=5) + + def increment_dir(self): + with self.lock: + self.dircount.value += 1 + self.sourcelabel.config(text=self.foldercnt) + +def read_dir(testgui, dirname): + for folderName, subfolders, files in os.walk(dirname): + if subfolders: + for subfolder in subfolders: + read_dir(testgui, subfolder) + #testgui.increment_dir() + +def get_directories(testgui): + p = Process(target=read_dir, args=(testgui, dirname,)) + p.start() + +if __name__ == "__main__": + root = Tk() + root.update() + root.resizable(False, False) + testgui = testGUI(root) + root.mainloop() \ No newline at end of file