Information on event handlers & global handlers list?

Information on event handlers & global handlers list?

j.han97
Advocate Advocate
745 Views
3 Replies
Message 1 of 4

Information on event handlers & global handlers list?

j.han97
Advocate
Advocate

Hi all,

 

I found that I have been writing add-ins without really understanding it. Here is my question: What is this global handlers list? 

 

_handlers = []

 

I understand that I need to append my event handlers to it, or else the events won't be triggered. But I don't understand why is it necessary, or how does it work. (I am not very familiar with Python as well)

 

Now that I want to arrange my functions across different modules, but I am not sure how to manage the handlers in such cases. Do I create a handlers list for each module (if required), or I need to store them all in one handlers list (let's say, in the main file)?

 

Is there any documentation regarding this (managing handlers), either Fusion 360-specific or Python-general? I am happy to read and study more.  

0 Likes
Accepted solutions (2)
746 Views
3 Replies
Replies (3)
Message 2 of 4

BrianEkins
Mentor
Mentor
Accepted solution

The handlers list is declared outside the scope of any function so it is global for that module, which means its lifetime is while the program is running. The lifetime of a variable declared within a function is only while that function is running and when that function finishes, those variables are cleaned up by Python. When you create an event handler, you use the add method of the event and the result is returned, which you assign to a variable. To keep this event alive, it needs to exist outside the context of the function where it was created or Python will clean it up as soon as that function finishes. Assigning it to the global list keeps it alive. This is how Python works and isn't anything specific to the Fusion API.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 3 of 4

pludikar
Collaborator
Collaborator
Accepted solution

@j.han97 

 

Just to add to @BrianEkins reply.

 

handlers are basically transient - that means if you create a handler it only exists for as long as another object has a reference to it.  If there isn't a reference, then Python garbage collector will reclaim it, and your handler will disappear.

 

for example:

cmd.execute.add(handler_method) #will exist for as long as it takes python to recognise it's not referenced and will gc it.

h = cmd.execute.add(handler) # will exist for as long as h exists

if you make h a global variable, then h will exist for as long as the add-in or script is running

 

I hope that adds to Brian's already good explanation

 

Peter

I'm not an expert, but I know enough to be very, very dangerous.

Life long R&D Engineer (retired after 30+ years in Military Communications, Aerospace Robotics and Transport Automation).
Message 4 of 4

j.han97
Advocate
Advocate

Thanks @BrianEkins  and @pludikar. Your explanations are clear and easy to understand. 

0 Likes