# Using python to do a counter in dynamo

**URL:** <https://forum.dynamobim.com/t/using-python-to-do-a-counter-in-dynamo/80467>\
**Category:** Developers\
**Created:** [August 30, 2022, 5:26am UTC](https://forum.dynamobim.com/t/using-python-to-do-a-counter-in-dynamo/80467 "2022-08-30T05:26:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![yangshilun2012](https://avatars.discourse-cdn.com/v4/letter/y/22d042/32.png) [@yangshilun2012](https://forum.dynamobim.com/u/yangshilun2012)\
**Post date:** [August 30, 2022, 5:26am UTC](https://forum.dynamobim.com/t/using-python-to-do-a-counter-in-dynamo/80467/1 "2022-08-30T05:26:24Z")

</div>

Hi, everyone  
I want to do a counter which i can custom period for example , 10ms , 100ms or 1000ms.

i wrote a short script, it can not run as a counter, always show 1.

Thanks in advance.

 ![dynamo python periodic](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/5/25b5e22e2eccb866ba10c03e34369094bf656f63.png)

---

<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:** [August 30, 2022, 6:44am UTC](https://forum.dynamobim.com/t/using-python-to-do-a-counter-in-dynamo/80467/2 "2022-08-30T06:44:02Z")

</div>

@yangshilun2012 ,

i search also for that… something similar, intressting part is also how to stop this process…!

```auto
#Created By Brendan Cassidy

import clr

#Imports date/time info
#See https://docs.python.org/2/library/time.html
from time import gmtime, strftime
#Applying time Day-Month-Year at Hour:min
time_info1=strftime("%d %b %Y at %H:%M", gmtime())

# OR

#See https://docs.python.org/2/library/datetime.html
from System import DateTime
time_info2=DateTime.Now

OUT = time_info2

```

 ![grafik](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/8/b/8bb302c72565883e5f5ccb8524c4ddcb6030dda0.png)

---

<div class="post-metadata">

**Author:** ![GavinNicholls](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gavinnicholls/32/190442_2.png) [@GavinNicholls](https://forum.dynamobim.com/u/GavinNicholls)\
**Post date:** [August 30, 2022, 7:21am UTC](https://forum.dynamobim.com/t/using-python-to-do-a-counter-in-dynamo/80467/3 "2022-08-30T07:21:25Z")

</div>

Try the sleep() method from the time library (import time). If your code is all running in Python, you can integrate the waits into your overall outcome, then loop it in the python block n number of times instead of running once.

---

<div class="post-metadata">

**Author:** ![yangshilun2012](https://avatars.discourse-cdn.com/v4/letter/y/22d042/32.png) [@yangshilun2012](https://forum.dynamobim.com/u/yangshilun2012)\
**Post date:** [August 30, 2022, 7:50am UTC](https://forum.dynamobim.com/t/using-python-to-do-a-counter-in-dynamo/80467/4 "2022-08-30T07:50:33Z")

</div>

Hi, GavinCrump Thanks, I am a beginner of dynamo python. would you like to write down the script?

---

<div class="post-metadata">

**Author:** ![yangshilun2012](https://avatars.discourse-cdn.com/v4/letter/y/22d042/32.png) [@yangshilun2012](https://forum.dynamobim.com/u/yangshilun2012)\
**Post date:** [August 30, 2022, 8:03am UTC](https://forum.dynamobim.com/t/using-python-to-do-a-counter-in-dynamo/80467/5 "2022-08-30T08:03:13Z")

</div>

I am a new user, so I am not sure this python script is as same as DateTime.Now Node in dynamo. Anyhow using DateTime.Now Node, then switch dynamo to Periodic Mode can make it run. In this way I only can get a second counter, right?

---

<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:** [August 30, 2022, 8:05am UTC](https://forum.dynamobim.com/t/using-python-to-do-a-counter-in-dynamo/80467/6 "2022-08-30T08:05:46Z")

</div>

@yangshilun2012 ,

yes…

i wanted to manage a task here via an intervall of time

> [@How to "increment" my task?](https://forum.dynamobim.com/t/how-to-increment-my-task/80266):
>
> Hello, I sorted my walls by base constrains, so i have 81 base constrains, i want to run my script level by level, how? right no i do it manual frowning

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [August 30, 2022, 1:58pm UTC](https://forum.dynamobim.com/t/using-python-to-do-a-counter-in-dynamo/80467/7 "2022-08-30T13:58:46Z")

</div>

> [@yangshilun2012](#):
>
> I wrote a short script, it can not run as a counter, always show 1.

Hello  
because the node and the variables are re-initialized at each instance/launch  
some solutions:

- save the variable outside the script (txt file, pickle, …)
- use a node to get/set the variable (see [this example](https://forum.dynamobim.com/t/counter-of-runs-in-dynamo-script/74684/4))

---

<div class="post-metadata">

**Author:** ![yangshilun2012](https://avatars.discourse-cdn.com/v4/letter/y/22d042/32.png) [@yangshilun2012](https://forum.dynamobim.com/u/yangshilun2012)\
**Post date:** [August 31, 2022, 5:29am UTC](https://forum.dynamobim.com/t/using-python-to-do-a-counter-in-dynamo/80467/9 "2022-08-31T05:29:00Z")

</div>

thanks, it works. I use date.now node as input, switch to periodic mode, but it is stuck in 10ms, only works fluently in 100ms 1000ms, probably there will be a better way.
