Python Forum
Multiproccessing ValueError: Pool not running when running parallel functions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Multiproccessing ValueError: Pool not running when running parallel functions (/thread-27342.html)



Multiproccessing ValueError: Pool not running when running parallel functions - Gilush - Jun-03-2020

[Image: gUGml.jpg]

I get this error when clicking the submit button the 2nd time.
My main target is to print the output to screen while writing the output to file, then delete the first line by creating a tmp file and save the new file and only delete the tmp file when the job is finished.

where do I need to place the pool.close() and pool.join()?

[Image: XjsZW.jpg]

Code snippet:

def handler_clr_yes_fn_no(self, host, filenameInputBox):

self.pool.apply(self.clr_yes_fn_no_writefile, args=(host,))
self.pool.apply(self.clr_yes_fn_no_print_output, args=(host,))
self.pool.map(self.delete_default_lines, filenameInputBox)

# proc1 = mp.Process(name="Clear + No Filename + WriteFile",
#                    target=self.clr_yes_fn_no_writefile, args=(host,))
# proc2 = mp.Process(name="Clear + No Filename + PrintOutput",
#                   target=self.clr_yes_fn_no_print_output, args=(host,))
# proc3 = mp.Process(name="Clear + No Filename + Generate PrintOutput to GUI",
#                    target=self.generate_clr_yes_fn_no_print_output_to_gui, args=(host,))
# proc4 = mp.Process(name="Clear + No Filename + PrintOutput to GUI",
#                    target=self.clr_yes_fn_no_print_output_to_gui, args=(host,))
proc5 = mp.Process(name="Remove first line + Write new default file",
                   target=self.delete_default_lines)


# startprocs = []
# nextprocs = []
lastprocs = []

lastprocs.append(proc5)

# for s in startprocs:
#     s.start()

for l in lastprocs:
    l.start()

self.pool.close()
self.pool.join()

# startprocs.append(proc1)
# startprocs.append(proc2)
# startprocs.append(proc3)
# startprocs.append(proc4)

# nextprocs.append(proc1)

# for s2 in startprocs:
#     s2.join()

# for n in nextprocs:
#     n.start()
#
# for p in nextprocs:
#     p.join()
Full code:

import tkinter as tk
from PIL import Image, ImageTk
import subprocess as sub
import multiprocessing as mp
import time
import io
import os

class GUI(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.geometry("640x478")
        self.title("Gil Shwartz GUI Project")
        self.resizable(0, 0)

        menu = tk.Frame(self, height=250, width=10, relief="solid")
        container = tk.Frame(self, relief="flat", height=400, bg="black")

        menu.pack(side=tk.LEFT, fill="y", anchor="w")
        container.pack(side=tk.TOP, fill="x", expand=True)
        menu.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(1, weight=1)

        self.frames = ["Menu", "MainWelcome", "testPing", "PageOne", "UptimeCheck"]

        self.frames[0] = Menu(parent=menu, controller=self)
        self.frames[1] = MainWelcome(parent=container, controller=self)
        self.frames[2] = testPing(parent=container, controller=self)
        self.frames[3] = PageOne(parent=container, controller=self)
        self.frames[4] = UptimeCheck(parent=container, controller=self)

        self.frames[0].grid(row=0, column=0, sticky="nsew")
        self.frames[1].grid(row=0, column=0, sticky="nsew")
        self.frames[2].grid(row=0, column=0, sticky="nsew")
        self.frames[3].grid(row=0, column=0, sticky="nsew")
        self.frames[4].grid(row=0, column=0, sticky="nsew")

        self.show_frame(1)



    def show_frame(self, page_name):
        frame = self.frames[page_name]
        print(frame)
        frame.tkraise()
        frame.grid(row=0, column=0, sticky="nsew")

class Menu(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        button1 = tk.Button(self, text="Ping Test", bg="royalblue2",
                            command=lambda: controller.show_frame(2))
        button2 = tk.Button(self, text="Uptime Check", bg="dark violet",
                            command=lambda: controller.show_frame(4))
        buttun3 = tk.Button(self, text="Home", bg="pale goldenrod",
                            command=lambda : controller.show_frame(1))
        button4 = tk.Button(self, text="Quit", bg="gray40",
                            command=lambda: Menu.terminate(self))

        button1.pack(fill="both", expand=True)
        button2.pack(fill="both", expand=True)
        buttun3.pack(fill="both", expand=True)
        button4.pack(fill="both", expand=True)

    def terminate(self):
        exit()

class MainWelcome(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        canvas = tk.Canvas(self, bg="black")
        canvas.pack(expand=True, fill="both")

        img = Image.open(fr'c:/users/{os.getlogin()}/Desktop/gil.jpg')
        photo = ImageTk.PhotoImage(img)
        # self.img = tk.Image.open(fr'c:/users/{os.getlogin()}/Desktop/gil.jpg')
        # self.photo = ImageTk.PhotoImage(self.img)
        canvas.create_image((0, 0), image=photo, anchor="center")

class testPing(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent, bg="skyblue1")
        self.controller = controller

        self.output = tk.LabelFrame(self, text="Output", height=325, width=580, padx=3, pady=3)
        self.output.pack(side=tk.BOTTOM, fill="x")
        # self.output.update()
        self.textbox = tk.Text(self, height=320, width=550, pady=3, padx=3, bg="lightgrey")
        self.textbox.pack(side=tk.BOTTOM, fill="y")
        self.pool = mp.Pool()
        self.lock = mp.Lock()
        self.queue = mp.Queue()

        clearFile = tk.BooleanVar()
        clearFile.set(False)
        urlLabel = tk.Label(self, text="Enter URL : ", padx=7, pady=5, bg="skyblue1")
        urlInputBox = tk.Entry(self)
        urlInputBox.grid_columnconfigure(0, weight=0)
        fileNameLabel = tk.Label(self, text="Enter Filename: ", bg="skyblue1")
        fileNameInputBox = tk.Entry(self)
        clearFileLabel = tk.Label(self, text="Clear File?", padx=5, pady=5, bg="skyblue1")
        clearFileRadioYes = tk.Radiobutton(self, text="yes", value=True, var=clearFile, bg="skyblue1",
                                           command=lambda: self.callback(clearFile.get()))
        clearFileRadioNo = tk.Radiobutton(self, text="no", value=False, var=clearFile, bg="skyblue1",
                                          command=lambda: self.callback(clearFile.get()))
        urlSubmitButton = tk.Button(self, text="Submit", width=10, height=2,
                                    command=lambda: self.pingURL(urlInputBox.get(),
                                                                 clearFile.get(), fileNameInputBox.get()))
        clearFieldsButton = tk.Button(self, text="Clear Fields", padx=5, pady=5,
                                      command=lambda: self.clear_boxes(urlInputBox, fileNameInputBox))

        urlLabel.pack(anchor="w")
        urlInputBox.pack(anchor="w", padx=10)
        fileNameLabel.pack(anchor="w", padx=7, pady=5)
        fileNameInputBox.pack(anchor="w", padx=10)
        clearFileLabel.pack(anchor="w")
        clearFileRadioYes.pack(anchor="w")
        clearFileRadioNo.pack(anchor="w")
        urlSubmitButton.pack(anchor="w", pady=1)
        clearFieldsButton.pack(anchor="w")

    @classmethod
    def clear_boxes(self, urlInputBox, fileNameInputBox):
        urlInputBox.delete(0, "end")
        fileNameInputBox.delete(0, "end")

    @classmethod
    def callback(self, clearFile):
        print(f'Clear file = {clearFile}')  # Debugging Mode - check Radio box Var.

    def pingURL(self, host, clearFile, filenameInputBox):

        print(clearFile, filenameInputBox)  # Debugging - Input Validation
        if clearFile is True and filenameInputBox == '':
            self.handler_clr_yes_fn_no(host, filenameInputBox)
        elif clearFile is False and filenameInputBox == '':
            self.handler_clr_no_fn_no(host)
        elif clearFile is True and filenameInputBox != '':
            self.handler_clr_yes_fn_yes(host, filenameInputBox)
        elif clearFile is False and filenameInputBox != '':
            self.handler_clr_no_fn_yes(host, filenameInputBox)

    def handler_clr_yes_fn_no(self, host, filenameInputBox):

        self.pool.apply(self.clr_yes_fn_no_writefile, args=(host,))
        self.pool.apply(self.clr_yes_fn_no_print_output, args=(host,))
        self.pool.map(self.delete_default_lines, filenameInputBox)

        # proc1 = mp.Process(name="Clear + No Filename + WriteFile",
        #                    target=self.clr_yes_fn_no_writefile, args=(host,))
        # proc2 = mp.Process(name="Clear + No Filename + PrintOutput",
        #                   target=self.clr_yes_fn_no_print_output, args=(host,))
        # proc3 = mp.Process(name="Clear + No Filename + Generate PrintOutput to GUI",
        #                    target=self.generate_clr_yes_fn_no_print_output_to_gui, args=(host,))
        # proc4 = mp.Process(name="Clear + No Filename + PrintOutput to GUI",
        #                    target=self.clr_yes_fn_no_print_output_to_gui, args=(host,))
        proc5 = mp.Process(name="Remove first line + Write new default file",
                           target=self.delete_default_lines)


        # startprocs = []
        # nextprocs = []
        lastprocs = []

        lastprocs.append(proc5)

        # for s in startprocs:
        #     s.start()

        for l in lastprocs:
            l.start()

        self.pool.close()
        self.pool.join()

        # startprocs.append(proc1)
        # startprocs.append(proc2)
        # startprocs.append(proc3)
        # startprocs.append(proc4)

        # nextprocs.append(proc1)



        # for s2 in startprocs:
        #     s2.join()

        # for n in nextprocs:
        #     n.start()
        #
        # for p in nextprocs:
        #     p.join()

    def handler_clr_no_fn_no(self, host):

        procs = []
        nextprocs = []

        proc1 = mp.Process(name="Append to default file",
                           target=self.clr_no_fn_no_writefile, args=(host,))
        proc2 = mp.Process(name="Print Output", target=self.clr_no_fn_no_printoutput, args=(host,))

        procs.append(proc1)
        procs.append(proc2)

        for proc in procs:
            proc.start()
        for proc in procs:
            proc.join()

        for p in nextprocs:
            p.start()

    def handler_clr_yes_fn_yes(self, host, filenameInputBox):

        procs = []
        nextprocs = []

        proc1 = mp.Process(name="Clear file + userFilename + Write to file",
                           target=self.clr_yes_fn_yes_writefile, args=(host, filenameInputBox,))
        proc2 = mp.Process(name="Clear file + user filename + Print output",
                           target=self.clr_yes_fn_yes_printoutput, args=(host,))
        proc3 = mp.Process(name="Remove Empty Lines from user filename",
                           target=self.delete_userfile_lines, args=(filenameInputBox,))

        procs.append(proc1)
        procs.append(proc2)
        nextprocs.append(proc3)

        for proc in procs:
            proc.start()

        for p in procs:
            p.join()

        for np in nextprocs:
            np.start()

    def handler_clr_no_fn_yes(self, host, filenameInputBox):

        procs = []
        nextprocs = []

        proc1 = mp.Process(name="Keep File + Userfilename + Append to Userfile",
                           target=self.clr_no_fn_yes_writefile, args=(host, filenameInputBox,))
        proc2 = mp.Process(name="Keep File + Userfilename + Print Output",
                           target=self.clr_no_fn_yes_printoutput, args=(host,))

        proc3 = mp.Process(name="Remove first line + Write new default file",
                           target=self.delete_userfile_lines, args=(filenameInputBox,))

        procs.append(proc1)
        procs.append(proc2)

        nextprocs.append(proc3)

        for p in procs:
            p.start()

        for p2 in procs:
            p2.join()

        for n in nextprocs:
            n.start()


    @classmethod
    def delete_default_lines(cls):

        # time.sleep(2)
        print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')
        file = fr'c:/users/{os.getlogin()}/Desktop/default-tmp.txt'
        newfile = fr'c:/users/{os.getlogin()}/Desktop/default.txt'

        with open(file, 'r') as inp, open(newfile, 'w+') as out:
            for line in inp:
                if not line.isspace():
                    out.write(line.lstrip())
                    out.write('')
            inp.close()
            out.close()
        os.remove(file)

    @classmethod
    def delete_userfile_lines(cls, filename):

        time.sleep(2)
        print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')
        file = fr'c:/users/{os.getlogin()}/Desktop/{filename}-tmp.txt'
        newfile = fr'c:/users/{os.getlogin()}/Desktop/{filename}.txt'

        with open(file, 'r') as inp, open(newfile, 'w+') as out:
            for line in inp:
                if not line.isspace():
                    out.write(line.lstrip())
                    out.write('')
            inp.close()
            out.close()

    @classmethod
    def clr_yes_fn_no_print_output(self, host):

        print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')
        with sub.Popen(["ping", "-n", "4", f'{host}'], stdout=sub.PIPE,
                       bufsize=1, universal_newlines=True, stderr=sub.STDOUT) as p:
            for line in p.stdout:
                print(line, end=' ')

    @classmethod
    def generate_clr_yes_fn_no_print_output_to_gui(self, host):
        print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')

        sub.run(f"ping {host}", shell=True, capture_output=True)

    def clr_yes_fn_no_print_output_to_gui(self):
        tk.Frame.__init__(self, parent)
        self.textbox.insert(tk.END, proc.stdout.decode())

    @classmethod
    def clr_yes_fn_no_writefile(self, host):

        print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')
        file = fr'c:/users/{os.getlogin()}/Desktop/default-tmp.txt'
        ping = sub.Popen(["ping", "-n", '4', f'{host}'], stdout=sub.PIPE)

        with open(file, 'w+') as output:
            data = output.read()
            for line in ping.stdout.readlines():
                data += str(line.decode())
            ping.stdout.close()
            output.seek(0)
            output.write(data.lstrip())

    @classmethod
    def clr_no_fn_no_printoutput(self, host):
        print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')

        with sub.Popen(["ping", "-n", '4', f'{host}'], stdout=sub.PIPE,
                       bufsize=1, universal_newlines=True, stderr=sub.STDOUT) as p:
            for line in p.stdout:
                print(line, end=' ')

    @classmethod
    def clr_no_fn_no_writefile(self, host):
        print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')

        with open(fr'c:/users/{os.getlogin()}/Desktop/default.txt', 'a') as output:
            sub.call(["ping", "-n", '4', f'{host}'], stdout=output)

    @classmethod
    def clr_yes_fn_yes_printoutput(self, host):
        print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')

        with sub.Popen(["ping", "-n", '4', f'{host}'], stdout=sub.PIPE,
                       bufsize=1, universal_newlines=True, stderr=sub.STDOUT) as p:
            for line in p.stdout:
                print(line, end=' ')

    @classmethod
    def clr_yes_fn_yes_writefile(self, host, filename):
        print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')

        file = fr'c:/users/{os.getlogin()}/Desktop/{filename}-tmp.txt'
        with open(file, 'w') as output:
            sub.call(["ping", "-n", '4', f'{host}'], stdout=output)

    @classmethod
    def clr_no_fn_yes_printoutput(self, host):
        print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')

        with sub.Popen(["ping", "-n", '4', f'{host}'], stdout=sub.PIPE,
                       bufsize=1, universal_newlines=True, stderr=sub.STDOUT) as p:
            for line in p.stdout:
                print(line, end=' ')

    @classmethod
    def clr_no_fn_yes_writefile(self, host, filename):
        print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')

        with open(fr'c:/users/{os.getlogin()}/Desktop/{filename}.txt', 'a') as output:
            sub.call(["ping", "-n", '4', f'{host}'], stdout=output)

class UptimeCheck(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        frame = tk.LabelFrame(self, text="Uptime Check", bg="honeydew3", relief="flat")
        frame.pack(fill="both", expand=True)



class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 1", bg="red")
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to page 2",
                           command=lambda: controller.show_frame(2))
        button.pack()



if __name__ == "__main__":
    app = GUI()
    app.mainloop()



RE: Multiproccessing ValueError: Pool not running when running parallel functions - Gilush - Jun-06-2020

Well, turns out it had nothing to do with the processes.
the function itself was buggy as f! (i'm a noob i'm allowed to!)
when I semi fixed it (target not accomplished yet) it lunched fine.