How do I resolve lag and lag issues when running programs through an EXE?

王林
Release: 2024-02-06 08:30:11
forward
607 people have browsed it

如何解决通过 EXE 运行程序时的延迟和滞后问题?

Question content

I am developing a sensor reading program where part of the program involves printing out the sensor status when a metal target moves further or closer to the sensor real-time updates. The "main_gui.py" file will be run first and once the user clicks the "Start Data Retrieval" button, it will launch the sub-process "ies2v2.py" where the sensor reading process will be performed and printed out.

The code below shows how code originally printed in the console is printed into the gui.

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()
Copy after login

When running the exe, I am experiencing delays in printing statements onto the gui. Once the "Start Data Retrieval" button is clicked, the print data is printed out in big chunks of data in a hurry, and there is a long time before delay to print again. I hope it runs as smoothly as pycharm...

Note: I also encountered problems when creating the exe and got this error. I fixed it by copying the not found file into the path and the exe ran fine from my side. FYI in case this might be the reason for the delay...but I don't think it should be.

EDIT: Below the ies2v2.py code (some of it) is the part that reads the sensor output and continuously updates it.

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
Copy after login

Correct answer


As we discovered in the comments, the child process is not flushing its standard output buffer and that's what's causing the delay. One way to flush standard output is to add flush=True when calling the print function.

The above is the detailed content of How do I resolve lag and lag issues when running programs through an EXE?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template