The below function is not exiting automatically after 3 seconds. I am unable to figure out the error.
import rhinoscriptsyntax as rs
import time
def runFunction(Run):
count = 0
while Run:
print count
count += 1
a = 10
return a
Run = True
population = 1
if population == 0:
population = 1
else:
population = 1
timeout_seconds = 3* population
start_time = time.time()
try:
while time.time() - start_time <= timeout_seconds:
a = runFunction(Run)
except:
print("Timeout! The operation took more than {} seconds.".format(timeout_seconds))
a = 15
Hi Vijesh - I am not sure I am answering the right question here, but it looks like your while Run loop can never finish because Run=True and is never set to False.
Thanks this works. I could not think. If anyone if possible please suggest me a great python coding book to keep it to my reference collection.
import rhinoscriptsyntax as rs
import time
def runFunction(Run):
start_time = time.time()
timeout_seconds = 3
count = 0
while Run:
print count
count += 1
if time.time() - start_time >= timeout_seconds:
break
return count
Run = True
try:
a = runFunction(Run)
except:
print("Timeout! The operation took more than {} seconds.".format(timeout_seconds))
Another way to accomplish this, is by updating the component manually and storing the initial time and changing counter values either as global variables, like in my example, or in the sticky dictionary. This is necessary to make the variables stick, when the component recomputes.
Here’s the script:
"""Simple timed pythonic counter.
Inputs:
Max: Optinonal maximum time in seconds (default=infinity)
Run: True to iterate, or False to reset the stopwatch and counter.
Output:
Count: Counter value"""
ghenv.Component.Name = "Pythonic Timed Counter"
ghenv.Component.NickName = "TCounter"
__author__ = "diff-arch"
__version__ = "0.1 (2023-10-26)"
import Grasshopper as gh
import time
def update_component():
"""Updates this component, similar to using a Grasshopper timer."""
# written by Anders Deleuran (andersholdendeleuran.com)
def call_back(e):
ghenv.Component.ExpireSolution(False)
# Get the Grasshopper document
ghDoc = ghenv.Component.OnPingDocument()
# Schedule this component to expire
ghDoc.ScheduleSolution(
1, gh.Kernel.GH_Document.GH_ScheduleDelegate(call_back)
)
def lap(start):
"""Returns the elapsed time in seconds from start until now."""
elapsed = time.time() - start
return elapsed if elapsed > 0.001 else 0.0 # millisecond precision
if __name__ == "__main__":
if Max is None:
Max = float("inf")
if "start" not in globals():
start = time.time()
if "count" not in globals():
count = 0
if Run and lap(start) < Max:
count += 1
update_component()
if (not Run and lap(start) > Max) or not Run:
start = time.time()
count = 0
# Outputs
print "Time: {:.2f}".format(lap(start))
Count = count
Hi. I am trying to use your timed counter script but unfortunately I don’t get great results. Looks like the start it is somehow random, every time I do press the Run toggle button I do get a random start time instead of the 0.00 as expected. please can you correct this issue? Also it is possible to replace the Boolean Toggle button with a simple Button one to start the counter?