pythonexelive-update

How do I fix the delay and lagging when running the program through an EXE?


I am working on a sensor reading program where part of the program involves printing out live updates of the sensor's status when the metal target moves further or closer to the sensor. The "main_gui.py" file will be the first to run, and once user clicks the button "Start Data Retrieval", it will start the subprocess "IES2V2.py" where it does the sensor reading process and prints out.

The code below shows how the code, originally printing in the console, will be printed into the GUI instead.

    def start_data_retrieval(self):
        # Start a new thread for data retrieval
        threading.Thread(target=self.retrieve_data_thread).start()

    def retrieve_data_thread(self):
        selected_current = LoadCurrent[currentgrp.get()]
        selected_output = OutputType[outputgrp.get()]
        print(f"Selected Current: {selected_current}, Selected Output: {selected_output}")

        with subprocess.Popen(["python", "IES2V2.py", "--port", self._port, "--current", selected_current.name, "--output", selected_output.name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as process:
            for line in process.stdout:
                line = line.decode()  # defaulting to system encoding
                text_box.insert(END, line)
                text_box.update()
                text_box.see(END)
                process.poll()

When running the EXE, I faced delays when printing out of the statements onto GUI once "Start Data Retrieval" button is clicked, the printing data will come out as a huge chunk of data in a rush and have a long delay before printing again. I expected it to run smoothly like in PyCharm...

Note: I also faced issues when creating the EXE, having errors like this. I fixed it by copying the files it couldn't find into the path and the EXE runs ok from my side. Just an fyi in case this could be the cause of the delay...but i think it shouldn't be.

Edit: IES2V2.py code(some of it) Below is the part where it reads the sensor output and updates continuously.

print('\n---- Reading Data ----')
print("Live update of sensor data PROCESS_ADDR will begin. Press the 'Enter' key to stop the updates.")
time.sleep(5)

prev_process_addr = ies2.get_value(122)
while True:
    print(f'Update of PROCESS_ADDR : {ies2.get_value(122)}')  # SU
    new_process_addr = ies2.get_value(122)
    if new_process_addr != prev_process_addr:
        print(f"PROCESS_ADDR value changed! {prev_process_addr} to {new_process_addr}")
        prev_process_addr = new_process_addr

    if keyboard.is_pressed('enter'):
     print("\nKey pressed! Stopping the sensor updates.")
     break

Solution

  • As we discovered in the comments, the child process was not flushing its standard output buffer, which is what caused the delay. One way to flush the standard output is to add flush=True when you call the print function.