Experimental:Python threading in TouchDesigner
Working with threads is challenging, and threading in TouchDesigner is difficult.
The main obstacle to overcome is that the main TouchDesigner thread needs to continuously run, as it is on the main thread that the editor runs and where the timeline moves forward and frames are being generated.
Blocking the main thread will result in frame drops or worse it could hang or crash TouchDesigner.
A general rule of thumb is to avoid accessing objects on the main TouchDesigner thread from other threads, whether reading or writing.
There are ways to work with Python's own threading library within secondary threads which allows for the use of while loops and other blocking methods. This often involves an Execute DAT, which checks the thread status and dequeues objects every frame.
As of TouchDesigner 2023.31500+, TouchDesigner includes a Thread Manager.
The Thread Manager simplifies threading and task handling in TouchDesigner for Python developers.
Advanced users can access the Thread Manager COMP directly using op.TDResources.ThreadManager. Alternatively, users who want an easier learning curve can look at the Thread Manager Client, available in the Palette. It’s a simple callback-oriented component that wraps around the Thread Manager.
Lastly, various debugging tools are available, such as the Threads Monitor COMP and advanced logging. The Thread Manager settings can be tweaked from the Threads Monitor COMP.