Thread DAT - for simplified python scripts in parallel without hanging the main program

I think it would be exceptionally convenient if there was a DAT that was already formatted to run some python script outside of the main loop. Reasons for this request mainly come from often testing python code line by line, and occasionally running into a loop/function that takes forever and hangs the program, and often ends up crashing.

For those like myself who know how to do a little bit of python code, and want to run maybe one or two functions in parallel, without creating and designing and connecting an entirely separate .toe, this would be extremely helpful. Or even if there is already a .tox formatted in some way to “fill in your code here” and “press button to run thread”, id love to know!

I know theres a way to do it with subprocess module, and as well with threading library but its also difficult to manage and be aware of all threads since they can’t be used to affect operators in the main program, sometimes don’t send all messages to the textport depending what code youre using, and unless the touch_text_console is open I find it difficult to visually indicate if a thread is alive or finished, or actively running, etc.

So if anyone has a suggestion for how to manage this parallel processing, in a way that’s not overcomplicated for some newbies to just run a couple parallel while(True) loops, I’d love to learn!

I’ll +1 this RFE, TouchDesigner already warns you when accessing its members outside of the main thread so it would be less of a footgun to offer some abstraction to enter a critical section and return data to the main thread. When the need arises I use a tox module that was built using an asyncio event loop running in its own thread, offering the following callbacks:

# Prepare your data for parallel processing and return the payload
def onPrepareForCriticalSection():
    return None

# Thread-safe code here ONLY, don't touch any OPs, return the results of parallel computation
def onEnterCriticalSection(payload):    
    return None

# Uncatched exceptions are returned in the Main Thread, feel free to update your OPs here
def onException(payload, exception):

# The result is returned in the Main Thread, feel free to update your OPs here
def onExitCriticalSection(payload, result):

This is one of many possible abstractions to keep users safe in the confines of the pen.