How to generate a real-time stream of data?

12

4

I'm experimenting with Inversion-of-Control (callback) programming patterns, and I would like a way to generate streams of data at specified real-time intervals. I came up with the illustrated way to use Animate (or Manipulate) to do it, and that's fine for experiments with the notebook front end, but I would like to have pure-code solutions I could put in a .m package. As always, I will be grateful for any hints, advice, and solutions!

enter image description here

Reb.Cabin

Posted 2012-03-30T15:47:45.097

Reputation: 8 501

2Have you looked at RunScheduledTask and related functions? – Heike – 2012-03-30T16:04:52.397

Not till now :) Post this as answer and I'll mark it such! – Reb.Cabin – 2012-03-30T17:25:01.847

I've posted my answer. – Heike – 2012-03-30T20:15:04.090

Answers

14

You could use RunScheduledTask or its relatives for this. For example, to append a random integer to catch once every two seconds you could do something like

catch = {};
task = RunScheduledTask[AppendTo[catch, RandomInteger[10]], 2];

You could also use CreateScheduledTask which is similar to RunScheduledTask except that the task won't be started automatically after it's been created. You'll have to use StartScheduledTask to start it manually.

To start and stop the scheduled task you can use StartScheduledTask[task] and StopScheduledTask[task], respectively, and RemoveScheduledTask[task] will remove the scheduled task once you're done with it.

Additional information about these functions is available in the Documentation Center.

Heike

Posted 2012-03-30T15:47:45.097

Reputation: 34 748