Python recursive limit and how to keep monitoring something - TagMerge
4Python recursive limit and how to keep monitoring somethingPython recursive limit and how to keep monitoring something

Python recursive limit and how to keep monitoring something

Asked 1 years ago
0
4 answers

Recursion is inadequate in this use-case, use a loop instead.

Tkinter in particular has got a method which allows you to execute a function in an interval without disrupting the GUI's event loop.

Quick example:

from tkinter import *

root = Tk()
INTERVAL = 1000 # in milliseconds

def get_temp()
   # ...
   root.after(INTERVAL, get_temp) 

get_temp()
root.mainloop()

Source: link

0

It needs loop. It should be:

def update():
    get_temp()#the function that get the computer temperature value, like cpu temperature.
    ...

def upd():
    while True:#needs a while true here and don't call upd() in this function.
        update()
        time.sleep(0.3)

upd()#this upd() is outside the function upd(),it used to start the function.

Thanks to everyone who helped me.

Source: link

0

I am working on a python tkinter program that monitoring computer temperature, and I want it to update the temperature value after a fixed time. the following function is what I used to do that:
def update():
        get_temp()#the function that get the computer temperature value, like cpu temperature.
        ...
    def upd():
        update()
        time.sleep(0.3)
        upd()#recursive call function.

    upd()
You can run the update loop in another thread. Maybe try something like this:
import threading

def update():
    get_temp()
    time.sleep(0.3)

updateThread = threading.Thread(target=update)
updateThread.start()

Source: link

0

The same holds true if multiple instances of the same function are running concurrently. For example, consider the following definition:
def function():
    x = 10
    function()
>>>
>>> function()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in function
  File "<stdin>", line 3, in function
  File "<stdin>", line 3, in function
  [Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
>>>
>>> from sys import getrecursionlimit
>>> getrecursionlimit()
1000
>>>
>>> from sys import setrecursionlimit
>>> setrecursionlimit(2000)
>>> getrecursionlimit()
2000
>>>
>>> def countdown(n):
...     print(n)
...     if n == 0:
...         return             # Terminate recursion
...     else:
...         countdown(n - 1)   # Recursive call
...

>>> countdown(5)
5
4
3
2
1
0

Source: link

Recent Questions on python

    Programming Languages