Long tasks in Fusion

Though the main concepts are already part of the Fusion API documentation, they can be easy to miss.

Whenever you have some longer tasks to do, then:

1) Place whatever you can into a separate thread. Do note that all calls to Fusion API need to be done on the main thread – see Working in a Seperate Thread

2) Split multiple operations into smaller parts. One option is to call DoEvents in between each part. That might be enough. However, the safest option is to place each part inside a Custom Event and fire the event after each part to continue with the execution. Something like this:

import traceback
import adsk.core
import adsk.fusion

# Initialize the global variables
app = adsk.core.Application.get()
ui  = app.userInterface
handlers = []
i = 1
n = 5
processPart = None

# Implement custom event
class ProcessPart(adsk.core.CustomEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        i = int(args.additionalInfo)
        print("Processing part #" + str(i))

        if i >= n:
            print("Finished processing")
            adsk.terminate()
            return
        
        app.fireCustomEvent("ProcessPart", str(i+1))

# Main part
def run(context):
    global processPart
    processPart = app.registerCustomEvent("ProcessPart")
    onThreadEvent = ProcessPart()
    processPart.add(onThreadEvent)
    handlers.append(onThreadEvent)

    app.fireCustomEvent("ProcessPart", str(1)) 

    adsk.autoTerminate(False)

3) If you are calling web services, including MFG DM API, then do that from a separate thread or at least use the asynchronous execute() function so you are not blocking the main thread

Also worth mentioning that certain information can be obtained faster using the above mentioned MFG DM API than using directly the Fusion API, e.g. Component.partNumber and description
See Gets all properties using GraphQL