Why does my script runs multiple times instead of once

Hello everyone! I created a part of a Dynamo script that should be placed inside each script to collect usage statistics (name of the Project file, user name, execution time, errors, etc.) and export it to an Excel file. I was inspired by the following article:

and by the following topic (here is an example of how to collect all errors from a current Dynamo workspace by a WinForms app):

So, I created a dummy script for testing that:

  • performs some dummy calculations and gets its execution time,
  • then gathers other usage data and puts it into a list,
  • transfers the list of data to a Python script node that calls a hidden WinForms app,
  • that app retrieves all errors from Dynamo when a necessary event occurs,
  • appends error data to the usage data list,
  • exports all data from the list to an Excel file,
  • and finally closes and disposes of the WinForms app.

It works, but with some issues. The problem is that it runs and writes to an Excel file multiple times instead of once (through Dynamo Editor or Dynamo Player - it doesn’t matter). And each time I run my script, the number of its executions increases by one. So, it writes duplicated information from all previous runs from the current Dynamo Editor’s or Dynamo Player’s session. Could anyone have a look inside my Dynamo script and find what I did wrong?

Get all usage statistics and export to excel.dyn (52.5 KB)

1 Like

Can you post a screenshot of your graph with all the node preview bubbles pinned so we have an idea of what it’s doing?

If it runs an extra time with each execution and duplicates data then it would seem like somewhere you’re either reading that data each time you write it or you’re otherwise allowing each “push” to refresh an input in your graph and force a new run.

I have attached my script at the end of my post)
I suspect that the problem could be due to a memory leak. The issues might be with the unsubscribing from an event or transferring data into WinForms instance using global variables. But I’m not sure with that

30 minutes in and nothing to show.

Tried it in Dynamo sandbox and it errored out as you’re calling Dynamo for revit methods exclusively but I couldn’t have known that until I opened up your graph, and so your error doesn’t reproduce there.

So I tried in Revit, but I didn’t have Ironpython 2 on my Revit installation so that took a bit, and I don’t have what I think is supposed to be your excel file (no extension on the file path so i can only guess) so when it runs I can’t see any dataI can’t confirm double execution. All in I’m now 25 minutes into trying to make this work with nothing to show for it.

My gut tells me the error is in your winforms that is launching in hidden mode… which means users will get a window that will lock up excel but not actually be visible… and to make maters worse it doesn’t appear to close… ever. And as you have an event to ‘listen’ for the dynamo instance to complete executing, the win-form is going to hand open and continue to write out to the excel file over and over and over again. I can now trigger 10 write outs concurrently.

Python scripting isn’t a valid path for this IMO, doubly so with the permanent IronPython2 dependency you’ve baked in. If you want to stay in scripting languages, move to a ‘write to csv’ node and abandon tracking warnings by interrogating the graph (no real value in that anyway) and instead build your tools to report when they have a issue.

If you really want the ‘read the graph status’ you should transition to a C# view extension that you deploy locally or via package manager. That will ensure that Dynamo queries the data only once after the run complete event is triggered, and writes out from there - you can look up Binoculars for Dynamo on the blog for an example. Starting there and re-working the code will have the added benefit what you’re after won’t be limited to Dynamo for Revit when you’re done (Dynamo has it’s own username which inherits the same username as Revit, etc.).

1 Like