Python Forum
Waiting for heavy functions question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Waiting for heavy functions question
#11
(Apr-27-2022, 06:58 PM)Gribouillis Wrote:
philipbergwerf Wrote:What happens if there is a python error inside the render() function because I did not fix all bugs?
A simple solution for this is to call instead a new function render_no_error() that cannot raise an exception. This function would call render()
def render_no_error():
    try:
        render()
    except Exception as e:
        pass  # handle the exception the way you want
The drawback of this technique is to silently suppress exceptions, which makes debugging hard. You will need to leave a trace of the exceptions somewhere, perhaps on stderr or better in a log file or perhaps display it in a tkinter window. You could also catch specific exception types before the Exception type.
philipbergwerf Wrote:it is becoming annoying because it does this drawing every time you press a key
I have been using for a long time a Python program to write mathematical documents. I type in an editor and a subprocess parses what I type and calls other programs to create a Pdf file. I see two windows: the editor and the pdf viewer. The event that triggers the recalculation ot the pdf file is not every keystroke. Instead I recalculate the pdf file every time I type Ctrl-S in the editor to save the source file. This is a very fluid workflow. I suggest that you bind a sequence such as Ctrl-S to the Text or the root widget that would simultaneously save your work and redraw the canvas. It allows you to type as many characters as you want without triggering a redraw.

I'm not a very good tkinter programmer because I don't use it very often but I can imagine solutions to your idea of hiding the canvas when you don't want to see the computer drawing. For example the canvas could be on a page of a Notebook widget and you would simply display another empty page.
Instead of another new function I implemented the try except block inside the ThreadAutoRender class like so:
class QuitThread(Exception):
    pass
 
class ThreadAutoRender(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.needs_to_render = False
        self.condition = Condition()
    def run(self):
        try:
            while True:
                self.wait_and_render()
        except QuitThread:
            pass
         
    def wait_and_render(self):
        with self.condition:
            if not program_is_running:
                raise QuitThread
            while not self.needs_to_render:
                self.condition.wait()
                if not program_is_running:
                    raise QuitThread
            self.needs_to_render = False
        try:
            render()
        except Exception as e:
            print('error: ', e)
 
thread_auto_render = ThreadAutoRender()
thread_auto_render.start()
Reply
#12
(Apr-29-2022, 10:37 AM)philipbergwerf Wrote: print('error: ', e)
This statement may display very little debugging information. Consider using traceback.print_exc() instead.
Reply
#13
(Apr-29-2022, 11:52 AM)Gribouillis Wrote:
(Apr-29-2022, 10:37 AM)philipbergwerf Wrote: print('error: ', e)
This statement may display very little debugging information. Consider using traceback.print_exc() instead.
I don't understand... where is 'traceback' coming from? What is the correct way to print the default python error message e.g. Traceback (most recent call last):
blahblahblah?
Reply
#14
traceback is a module in the standard Python library. "import traceback" creates a variable named "traceback" that references the module. traceback.print_exc() calls a function in the module.

https://docs.python.org/3/library/traceback.html
Reply
#15
(Apr-29-2022, 06:43 PM)deanhystad Wrote: traceback is a module in the standard Python library. "import traceback" creates a variable named "traceback" that references the module. traceback.print_exc() calls a function in the module.

https://docs.python.org/3/library/traceback.html
Ah thank you for explaining. I want to thank everyone because I learned some neat tricks from this forum thread. I think the orginal question(how to handle a cpu intensive function) is answered fully now. You need to run it in a thread and there are some useful short code examples in this thread on how to call this thread function if you press a key.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Waiting for input from serial port, then move on KenHorse 3 1,081 Apr-17-2024, 07:21 AM
Last Post: DeaD_EyE
  pip stops waiting for python walker 6 1,061 Nov-28-2023, 06:55 PM
Last Post: walker
  Is chromedriver detectable by heavy seured websites? kolarmi19 4 1,490 Mar-29-2022, 08:14 PM
Last Post: Larz60+
  How to create waiting process? samuelbachorik 4 1,983 Sep-02-2021, 05:41 PM
Last Post: bowlofred
  Question about functions(built-in) Jinja2Exploitation 2 1,949 Nov-15-2020, 02:13 PM
Last Post: jefsummers
  Waiting and listening test 2 2,152 Nov-13-2020, 04:43 PM
Last Post: michael1789
  beginner question about lists and functions sudonym3 5 2,748 Oct-17-2020, 12:31 AM
Last Post: perfringo
  waiting for barcode scanner output, while main program continues to run lightframe109 3 4,664 Sep-03-2020, 02:19 PM
Last Post: DeaD_EyE
  waiting to connect Skaperen 9 3,584 Aug-17-2020, 05:58 AM
Last Post: Skaperen
  Launch another python command without waiting for a return. SpongeB0B 13 10,981 Jun-18-2020, 10:45 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020