Function load timing

Hi, so I’m assuming there’s just something here I’m not aware of and its an easy solve, but here’s my problem:

I have a common function def that I’m storing in a file loaded by run_file. Reading up on run_file, it sounds like it stores variables and functions in a global timing thread which can be shared across all playback threads.

I have a playback function that is called to do a few things like transpose and use other globals to know what ring sequence to play in conjunction with MIDI triggers.

For the most part this function works, except when I load Sonic PI for the first time. When I run the script on a fresh Sonic PI boot, it can’t find the function and crashes, presumably because run_file is async, and when I tell it to use my loaded function, it’s not loaded or initialized yet.

On subsequent plays, it’s fine, presumably because it’s loaded into the timing thread from the first crash.

Whats the right way to allow run_file to finish loading before things happen? I tried a sleep 1 which works, but it’s an arbitrary amount of time, and I don’t know that this will actually work all the time

thanks!