I created a function that prints every sample value from a buffer to the console but it is taking a really long time to execute. I cannot execute ‘s.quit’ or ‘b.free’ for instance because this process is still executing. Is there anyway to just quickly quit a process while executing in the case of an endless loop like you can from unix with most programming languages?
sclang is “non-preemptive,” meaning: once a Thread takes control of the interpreter, it has control until it either finishes, or explicitly yields a value (from a Routine). If it’s a long-running loop without any yields, there’s nothing sclang can do at that point to stop it.
So – this is somewhat irritating but currently necessary – it’s better to write long processes as Routines, with periodic short wait
calls. The wait
allows opportunities for the interpreter to stop the process. (fork
implicitly creates a Routine.)
r = fork {
1000000000.do { |i|
if(i % 1000 == 0) { 0.00001.wait };
... do stuff...
};
};
// sometime later:
r.stop;
Otherwise, you can do killall sclang
in the terminal (not within sclang). The IDE has menu option to stop or reboot the interpreter, you could try that too.
hjh
You can make your function check an external/global stop flag on every big-enough iteration. Such a wrapper exists already as the SkipJack
class, to which you pass the “inside-the-loop” function that you want executed repeatedly. The SkipJack
takes a stop flag as (extra) input.
This is functionally equivalent to the Routine solution suggested by James, but with Routines you have the extra feature that Ctrl/Cmd+. also stops them, which is probably more suitable in your use case (i.e. CPU hogs). (Yes, you could also make a custom stop flag get set by a CmdPeriod
action, as an alternative approach, but Routines basically already encapsulate all this functionality.)