asyncio is used as a foundation for multiple Python asynchronous frameworks that provide high-performance network and web-servers, database connection libraries, distributed task queues, etc.
I tested the following program. But this program didn’t work in TouchDesigner. It seems that the event loop does not return control to TD until the event loop has completed. That’s why I created TDAsyncIO.tox for using asyncio module in TouchDesigner.
import asyncio async def test(): await asyncio.sleep(3) print('hello world!') asyncio.run(test())
TDAsyncIO.tox is a Component for using asyncio module in TouchDesigner without blocking the TD’s main thread by running the event loop only once after every frame.
This is a sample project for using TDAsyncIO.tox. Please download it from here.
[Active] - the Event Loop can run asynchronous tasks while ‘Active’ is enabled.
[Cancel All Tasks] - Cancel all tasks you created.
AsyncIO COMP is set to ‘OP Global Shortcut’ parameter to TDAsyncIO so it can be reached anywhere by op.TDAsyncIO. It can call op.AsyncIO.Run() and op.AsyncIO.Cancel() by Extensions.
import asyncio async def test(): await asyncio.sleep(3) print('hello world') # Run coroutine coroutines = [test()] op.TDAsyncIO.Run(coroutines) # Cancel all tasks op.TDAsyncIO.Cancel()
Now you can easily use asyncio! For more information about coroutines, please refer to Python Coroutines and Tasks.
See also these helpful links.