Loop Behavior in a Routine

Hi there -
I am a little confused about a basic feature of using a routine.
I’m hoping the following illustrates my point. Essentially, I don’t know if the “wait” time will need to be 6 seconds or 20 seconds, depending on the function nested in there. Is there a way to adjust the wait based on whether there is still a process running?

//a bunch of code that takes a different amount of time each time it is executed. 

.wait is just a method call defined on a number, so you can store that number in a variable if you want and change the contents of the variable, e.g.

fork {
	20.do {
		var waitfor = 1.rrand(10);
		("waiting for" + waitfor + "seconds").postln;

You don’t want to wait on a number here, but on some kind of condition (CondVar perhaps).

A little while ago I made a quark to help with parallel tasks.

The help docs are out of date buts the basic use is like this.

r = Routine {
	var barrier = Barrier.do(
		{1.0.rand.wait; \a.postln},
		{1.0.rand.wait; \b.postln},
		{1.0.rand.wait; \c.postln},
		{1.0.rand.wait; \d.postln}


1 Like

Thanks @jordan for this explanation and the Quark.
One thing seems confusing to me still, though…
Let’s say I have something like:


Is it possible to have some sort of flag inside of ~abstractFunction that would indicate to a CondVar or Barrier when it was time to move on to the next iteration of the Do Loop?

It seems like that with both of these examples, the condition is set from outside the operating function…

I’m a little confused as to what you want to do now. The previous example will evaluate ~abSTRACTfunction 10 times, running each in series (waiting for the previous to finish).

If you want to run them in parallel (at the same time) and need to wait for all of them to be finished, you should use Barrier.

You mean something like that maybe:

		//a bunch of code that takes a different amount of time each time it is executed. 
		var tps;
		tps=10.0.rand.postln; // this means you have to grab the executed time of your function into the variable tps...
		tps.wait; // the time it takes to evaluate your function
		(10-tps).wait; // then make the difference to get the expected duration as the remaining time ...
		// this supposes that your function should not exceed 10 seconds in this case ...

Hi all -

It turns out Condition is what I was looking for…

1 Like