# How to continuously run a Python script

I am running this script:

script = Import["!python  C:\\...python script", "String"];


but I need to run it a few times as it outputs data I need. I run it every time I change something so I would like to keep it running continuously or give me a 'live' feed of changes. I am trying to use Dynamic, but I am not sure how I can do it.

Edit:

My program is just a stream as I am using a sensor as a camera and the Python script that I run gives me the measurements that I need. The measurements change if I make the surroundings darker or lighter. Basically I'm taking pictures but I would like to be at a constant rate so it would work as a live-feed camera. Whenever I run the script it gives me different measurements so that is why I need to constantly run it but doing it manually seems like a hassle. I would like to keep running it without me doing it. Now I am using RunScheduleTask but it seems to be slow. Is there another way around this?

• Take a look at RunscheduledTask. Related/potential duplicate of mathematica.stackexchange.com/q/47263/131. – Yves Klett Dec 27 '15 at 3:58
• I think we need more information to help you. Do you want your python script to run at a fixed interval? Do you want it to run when you click on a button? Or do you want some other event to trigger it? Pleas do not answer these questions in a comment -- edit your question to add information about triggering the script. – m_goldberg Dec 27 '15 at 3:58
• StartProcess might be worth a look. – george2079 Dec 27 '15 at 4:12
• I'm not sure I understand. "I run it every time I change something" ... <- What do you change? The Python script? Do you want Mathematica to detect when the Python script has changed and re-run it automatically? – Szabolcs Dec 27 '15 at 12:01
• So my program is just a stream as I am using a sensor as a camera and the python script that I run gives me the measurements that I need. The measurements change if I make the surroundings darker or lighter. Basically I'm taking pictures but I would like to be at a constant rate so it would work as a live-feed camera. @Szabolcs – user35053 Dec 30 '15 at 23:35

Have you considered using SetDelayed (:=) to define script? Here is an example that is coded for Windows:

linPy = "'import datetime; print datetime.datetime.now()'";
winPy = "\"import datetime; print datetime.datetime.now()\"";
cmd = "!python -c " <> winPy ;
script := Import[cmd, "String"]

Linux users would use the "linPy" expression instead of "winPy". With the above definitions, script is updated whenever it is referenced. For example, execute the following command a few times and you will see an updated value each time:

First@StringTake[script, {18 ;;}]


Of course, you would substitute your own python command.

For a continuously updated display you could use something like the following:

Dynamic[x]
continueDisplay = True;
Button["Stop", continueDisplay = False]
While[continueDisplay,
x = First@StringTake[script, {18 ;;}];
]


The While-loop will be terminated when you press the "stop" button. The problem with continuous update is that you can't keep working in the notebook. Using the RunScheduledTask is a better solution:

Dynamic[x]
delaySeconds = 2;

The scheduled task waits 2 seconds, runs the Python script and updates the displayed value of x. It allows you to keep working in the same notebook. To terminate the scheduled task, evaluate RemoveScheduledTask[task];