# Setting a timer to limit Dynamo script runtime

**URL:** <https://forum.dynamobim.com/t/setting-a-timer-to-limit-dynamo-script-runtime/89975>\
**Category:** Uncategorized\
**Created:** [May 24, 2023, 5:53am UTC](https://forum.dynamobim.com/t/setting-a-timer-to-limit-dynamo-script-runtime/89975 "2023-05-24T05:53:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![theshysnail](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/theshysnail/32/146653_2.png) [@theshysnail](https://forum.dynamobim.com/u/theshysnail)\
**Post date:** [May 24, 2023, 5:53am UTC](https://forum.dynamobim.com/t/setting-a-timer-to-limit-dynamo-script-runtime/89975/1 "2023-05-24T05:53:03Z")

</div>

Hi, When I attempt to `Solid.ByUnion` thousands of simple solids (cuboids) into a single solid in order to export it to an SAT file, Dynamo freezes and doesn’t complete the operation. I think I’ll have to create the solids in Revit from Dynamo and export them from there.

However, if I try to add a timer it doesn’t work for me. It runs until completion and does not respect the time.

**Method #1**

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/1/f/1f55b5d94e8d002c1790620e188081e436f98d36.jpeg)

```auto
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

import time

# Set the maximum number of seconds the script should run
max_seconds = 10

# Function to perform the union operation on a list of solids
def union_solids(solids):
    result = solids[0]
    for i in range(1, len(solids)):
        result = result.Union(solids[i])
    return result

# Get all solids from the current Dynamo document
solids = IN[0]

# Start the timer
start_time = time.time()

# Perform the union operation within the time limit
result = None
while True:
    result = union_solids(solids)
    elapsed_time = time.time() - start_time
    if elapsed_time >= max_seconds:
        break

# Output the resulting solid or None if the time limit was reached
OUT = result

```

**Method #2**

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/e/ae394ca3484257d3eb9b6a4bb19c90a2d047b374.png)

> [@Stop running script after a certain amount of time](https://forum.dynamobim.com/t/stop-running-script-after-a-certain-amount-of-time/29978):
>
> Hi, when I program scripts, it often happens that I create a loop that runs infinitely. The script can not be canceled. The only solution is to completely close Revit. For large Revit projects, restarting Revit and Dynamo consumes a lot of time. That’s why I’ve built a script that runs only a predetermined working time and then breaks off. The idea is not new, but I did not find a good solution in this forum. import clr clr.AddReference('ProtoGeometry') from Autodesk.DesignScript.G…

> [@Wait for specific amount of time](https://forum.dynamobim.com/t/wait-for-specific-amount-of-time/25761):
>
> Is it to make a node that allows user to input some amount of time in seconds,then an input to passthrough? Currently using passthrough rom clockwork ,But its fast.

---

<div class="post-metadata">

**Author:** ![Draxl\_Andreas](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/draxl_andreas/32/145744_2.png) [@Draxl\_Andreas](https://forum.dynamobim.com/u/Draxl_Andreas)\
**Post date:** [May 24, 2023, 6:00am UTC](https://forum.dynamobim.com/t/setting-a-timer-to-limit-dynamo-script-runtime/89975/2 "2023-05-24T06:00:39Z")

</div>

@theshysnail ,

i use ranges f.e. i have 10.000 elements. i do

```auto
x[0..1000];
x[1001..2000];
...

```

KR  
Andreas

---

<div class="post-metadata">

**Author:** ![theshysnail](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/theshysnail/32/146653_2.png) [@theshysnail](https://forum.dynamobim.com/u/theshysnail)\
**Post date:** [May 24, 2023, 7:17am UTC](https://forum.dynamobim.com/t/setting-a-timer-to-limit-dynamo-script-runtime/89975/3 "2023-05-24T07:17:15Z")

</div>

Although there have been some improvements with the below code to prevent the session from freezing, the script still does not adhere to the specified timer. It doesn’t matter if you enter 2 seconds or 10 seconds. The script may complete running in anywhere between 20 seconds to a couple of minutes.

TuneUp is also causing confusion up by showing about 10x more runtime. Instead of 68 seconds it shows 686 seconds (686084 ms).

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/5/95d8d0cb326923efd99888692c39a872565b3f28.png)

```auto
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import Solid
import time

# Set the maximum number of seconds the script should run
max_seconds = 10

# Function to perform the union operation on a list of solids within the specified time limit
def union_solids(solids, max_time):
    start_time = time.time()
    result = solids[0]
    index_unioned = 0 # Initialize the counter
    for i in range(1, len(solids)):
        if time.time() - start_time >= max_time:
            break
        result = result.Union(solids[i])
        index_unioned += 1 # Increment the counter
    return result, index_unioned

# Get all solids from the current Dynamo document
solids = IN[0]

# Perform the union operation within the time limit
result, index_unioned = union_solids(solids, max_seconds)

# Output the resulting solid, the number of solids unioned, or None if the time limit was reached
OUT = result, index_unioned

```
