GUI unable to send messages through Manager().Queue() to the processes

Please guide me in the right direction if this is already answered. I have a GUI script that has several buttons and when I click the start button, it needs to send a message to a process which is using multiprocessing.Process(). This process is started in the main function. I am using a Queue object to send messages to this process. As an example, the process is an infinite while loop that terminates when it receives a ‘True’ value and ‘runs’ constantly for every ‘False’ value that it gets from the queue. There are two buttons, ‘start’ and ‘stop’. The ‘start’ button is what initiates this while loop that constantly sends ‘False’ to keep the process running. The ‘stop’ button sends a ‘True’ value that terminates the while loop, ending the process and eventually the program comes to an end. I read a few posts on something similar where people have recommended using asyncio to run the GUI and the process concurrently. The following is the class that creates the GUI:

import os
import threading
import tkinter as tk
from tkinter import *
from tkinter import filedialog, ttk
from tkinter.constants import DISABLED, NORMAL
from PIL import ImageTk, Image
import glob
import time
import multiprocessing
from multiprocessing import Queue, Process, Manager
import asyncio
import numpy as np
import queue

class App(tk.Tk):
    """
    This class creates the GUI with the desired buttons and operations.
    asyncio is used to run the GUI and the processes concurrently.
    """
    def __init__(self):
        global detection_queue, hit
        super().__init__()
        # self.attributes('-fullscreen', True)
        # global detection_queue
        self.geometry("750x600")
        self.title('My_GUI')

        self.setup_frame()
        self.operate_frame()
        self.main_frame()
        self.detection_queue = detection_queue  # Manager().Queue()

        self.user_pressed_enter = False
        self.hit = hit  #Manager().Queue()
        self.start = True

    def setup_frame(self):
        """
        Creates the first frame with functionalities to create a directory or browse folder where the
        data needs to be stored.
        """
        setup = tk.Frame(self, bg='grey')
        setup.pack(padx=2, pady=2, ipadx=2, ipady=2, side=TOP)

        self.b1 = Button(setup, text="Create destination directory", command=self.enter_dir)
        self.b1.pack(padx=15, pady=15, ipadx=15, ipady=15, side='left')

        browse_button = Button(setup, text='Browse Folder', command=self.browse_folder)
        browse_button.pack(padx=15, pady=15, ipadx=15, ipady=15, side='left')

    def operate_frame(self):
        """
        This frame creates buttons that control the entire operation and execution of the processes.
        """
        operate = tk.Frame(self, bg='grey')
        operate.pack(side=BOTTOM)

        # The start button runs the start_example method on a separate thread. Use .start instead of .start()
        # b2 = Button(operate, text="Start", command=Thread(target=self.start_example).start, state='normal')
        # running the following line on a separate thread did not work.
        b2 = Button(operate, text="Start", command=lambda: [self.start_async_task, self.hit.put(False)],
                    state='normal')  # lambda: [self.example(self.hit)]
        b2.pack(padx=15, pady=15, ipadx=15, ipady=15, side='left')

        b3 = Button(operate, text="Stop", command=lambda: [self.hit.put(True)],
                    state='normal')  # lambda: [self.example(self.hit=True)]
        b3.pack(padx=15, pady=15, ipadx=15, ipady=15, side='left')

        b4 = Button(operate, text='Save', command=self.save_files, state='normal')
        b4.pack(padx=15, pady=15, ipadx=15, ipady=15, side='left')

        b5 = Button(operate, text='Close', command=self.close_app, state='normal')
        b5.pack(padx=15, pady=15, ipadx=15, ipady=15, side='left')

        open_button = tk.Button(self, text="Display Images", command=self.display_images, state='normal')
        open_button.pack(padx=20, pady=10)

    def start_async_task(self):
        print('This is in "start_async_task"')
        threading.Thread(target=self.run_async_task).start()

    def run_async_task(self):
        print('this is in "run_async_task"')

        async def task():
            """
            All this task is doing is passing booleans through the detection queue.
            This task controls the processes created.
            """
            global user_pressed_enter
            print(f'This is user pressed enter: {user_pressed_enter}')
            _hit = self.hit.get()
            print(f'got hit as: {_hit}')
            while not user_pressed_enter:
                print('in the while loop of task function')
                self.detection_queue.put(False)
                print('detection queue sent False')
                if _hit:
                    user_pressed_enter = True
                    self.detection_queue.put(True)
                    print('detection queue sent True')

        # async def inner_task():
        #     await task()

        asyncio.run(task())
        print('ran the inner_task()...')
        # loop = asyncio.new_event_loop()
        # asyncio.set_event_loop(loop)
        # asyncio.run_coroutine_threadsafe(inner_task(), loop)

    def close_app(self):
        self.destroy()
        
if __name__ == "__main__":
    detection_queue = Manager().Queue()
    hit = Manager().Queue()
    print('created queues...')
    user_pressed_enter = False

    app = App()
    my_p= Process(target=my_process, args=(detection_queue,))
    my_p.start()
    print('started the process...')

    app.mainloop()

I have removed some methods as it does not pertain to the problem. The problem I face is that when the GUI launches and I click the ‘start’ button, there is NO response in the terminal. The close button works in ‘destroying’ the GUI window. When the start button is pushed, the following function should run and print random numbers:

def my_process(detection_queue):
    shutdown_loop = False
    while not shutdown_loop:
        exit_queue_read = False
        while not exit_queue_read:
            print('Inside the inner while loop...')
            try:
                shutdown_loop_flag = detection_queue.get()
                print(f"read from queue: {shutdown_loop_flag}")
                if shutdown_loop_flag:
                    shutdown_loop = True
                    # break
            except queue.Empty:
                print('went into exception')
                exit_queue_read = True

            finally:
                if queue.Empty and shutdown_loop:
                    print('went into finally block')
                    exit_queue_read = True

            # Simulate something
            a = np.random.randint(0, 100)
            print(f'Number: {a}')
            time.sleep(0.05)
    print('process exit()')

to check exactly what’s happening, I included a print statement inside the inner while loop of ‘my_process’ and this is executed only once. It could be that the terminal freezes right after this print statement when it reaches ‘detection_queue.get()’.

Here’s what I tried and what I want this code to do. I know that the GUI runs on the main thread and the process is started on another. When I did not use asyncio, the GUI would hang as soon as I hit the ‘start’ button. But now, my problem seems to be sending messages using the queue system. There is something fundamental about multiprocessing/threading and the queue system that I am failing to understand as I am a beginner in this. Overall, I want the GUI to control the process depending on which button I press. This is the final traceback which is because of shutting down the process forcefully. My concern is not with the output but rather the operation of the GUI. I look forward to your suggestions.

created queues...
started the process...
Inside the inner while loop...
Process Process-4:
Traceback (most recent call last):
  File "C:UsersRAOKAppDataLocalanaconda3Libmultiprocessingconnection.py", line 328, in _recv_bytes
    nread, err = ov.GetOverlappedResult(True)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
BrokenPipeError: [WinError 109] The pipe has been ended

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:UsersRAOKAppDataLocalanaconda3Libmultiprocessingprocess.py", line 314, in _bootstrap
    self.run()
  File "C:UsersRAOKAppDataLocalanaconda3Libmultiprocessingprocess.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "C:My_GUI_v3.py", line 290, in my_process
    shutdown_loop_flag = detection_queue.get()
                         ^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 2, in get
  File "C:UsersRAOKAppDataLocalanaconda3Libmultiprocessingmanagers.py", line 822, in _callmethod
    kind, result = conn.recv()
                   ^^^^^^^^^^^
  File "C:UsersRAOKAppDataLocalanaconda3Libmultiprocessingconnection.py", line 250, in recv
    buf = self._recv_bytes()
          ^^^^^^^^^^^^^^^^^^
  File "C:UsersRAOKAppDataLocalanaconda3Libmultiprocessingconnection.py", line 337, in _recv_bytes
    raise EOFError
EOFError

Thank you for your time on this! I highly appreciate it.

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật