Listing live_loop names OR How to get rid of ghost live_loops

Is it possible to list all the live_loops that are currently running
OR
Is there a way to list all the cues that are being in the pool for execution
OR
Is there a way to get rid of a ghost live_loop?

I couldn’t find any resources / pointers on how to access internals of sonic pi during the execution.
And I know it’s possible to see the running live loops using the standard GUI (on the cues section) but I’m using vscode + sonic-pi-tool, so no access to this information

By ghost live_loop I mean, a live_loop that started with name X but it was later renamed to Y, now both loops are running at the same time and the only way to stop this is by either stopping X (I have to remember the name X in order to stop it) or stop all executions

Thanks for the help!

Hello @armando! welcome to in_thread :grinning:

As you have seen, there are little to no documents anywhere about accessing Sonic Pi internals. I myself would love to see some support for things like using introspection - but whether it will ever happen depends mostly on whether there is a 10-year-old-child friendly way to do it, and then on having the resources to add the features in!

Here is an example showing a way you can find out the currently running live_loops:

define :live_loop_names do
  @named_subthreads.map do |name, thread|
     name.to_s[10..-1] if name.to_s.start_with?('live_loop')
  end.compact
end

live_loop :test do
  sleep 1
end

Run it, and change the name of the loop to say :test2, then run:

puts live_loop_names

You should see the following result:

["test2", "test"]

However, there is one more thing to note:
If you have something roughly similar to the following:

live_loop :test do
  stop
end

puts live_loop_names

… and run it, you will see:

["test"]

As the loop has not yet been terminated by the time we look up the names. In order to make sure it has been stopped by the time we look up thread names, we’d need to either delay the call to live_loop_names, or maybe have a use_real_time or with_real_time in there:

define :live_loop_names do
  @named_subthreads.map do |name, thread|
    name.to_s[10..-1] if name.to_s.start_with?('live_loop')
  end.compact
end

with_real_time do # or just have use_real_time above the loop somewhere instead of using the block version
  live_loop :test do
    stop
  end
end

puts live_loop_names

(Though if you don’t really care about seeing names of threads that are still in the process of stopping, it’s not important of course).

3 Likes

Thank you so much, @ethancrawford! It’s exactly what I was looking for. Fantastic answer.
Thanks for the kindness.

1 Like