From: jyoung79 on
Currently, I have some scripts (in particular, applescript
'stay-open' scripts) that run continuously on a Mac through
the day. They look in a certain folder every 30 seconds and
perform the necessary work needed.

I was curious if anyone here on the list does anything similar
with Python? If so, do you use launchd, cron, etc in order to
start up your Python script at the appropriate time(s)? Or do
you just let your Python code run continuously? I'm curious of
the pros and cons with each of these. I'm assuming launchd (or
something similar) is probably the better option since if a
script broke it would start it back up again the next time
around. Launchd also probably doesn't use as much processing
power?

Thanks.

Jay

From: Ian Kelly on
On Fri, Jun 25, 2010 at 9:49 AM, <jyoung79(a)kc.rr.com> wrote:
> Currently, I have some scripts (in particular, applescript
> 'stay-open' scripts) that run continuously on a Mac through
> the day.  They look in a certain folder every 30 seconds and
> perform the necessary work needed.
>
> I was curious if anyone here on the list does anything similar
> with Python?  If so, do you use launchd, cron, etc in order to
> start up your Python script at the appropriate time(s)?  Or do
> you just let your Python code run continuously?  I'm curious of
> the pros and cons with each of these.  I'm assuming launchd (or
> something similar) is probably the better option since if a
> script broke it would start it back up again the next time
> around.  Launchd also probably doesn't use as much processing
> power?

I use cron.

Pro: You don't have to worry about an unusual exception crashing your
script and requiring a restart. You fix the exception when you can,
and in the meantime your script is still being run.

Con: Most cron implementations have a maximum frequency of once per minute..
Con: Starting a fresh Python interpreter is expensive, and doing it
once or twice per minute could add significantly if the system is
already under a heavy load.

When I do this, my scripts generally run once every 15 minutes or
thereabouts, so the cons don't really apply.

Cheers,
Ian
From: Tim Harig on

On 2010-06-25, <jyoung79(a)kc.rr.com> <jyoung79(a)kc.rr.com> wrote:
[order modified]
> I was curious if anyone here on the list does anything similar
> with Python? If so, do you use launchd, cron, etc in order to
> start up your Python script at the appropriate time(s)? Or do
> you just let your Python code run continuously? I'm curious of

I do both. I use cron for large time intervals so that the script is not
constantly memory resident and I run python continuously, either sleeping
or pausing for a signal, when the time interval is small enough that the
init overhead becomes significant.

> the pros and cons with each of these. I'm assuming launchd (or
> something similar) is probably the better option since if a
> script broke it would start it back up again the next time
> around. Launchd also probably doesn't use as much processing
> power?

You can do the same thing by implementing a supervisor process that
monitors your worker process so that the supervisor kills and restarts the
worker process if it doesn't appear to be functioning properly or crashes.

> Currently, I have some scripts (in particular, applescript
> 'stay-open' scripts) that run continuously on a Mac through
> the day. They look in a certain folder every 30 seconds and
> perform the necessary work needed.

It sounds to me, since your script is acting on an event, that it
would benefit from using something like inotify, or whatever your
system equivilant would be (FSEvents for Mac? FAM framework for general
POSIX. There are python modules available.), so that your script can
react when (and only when) it notices changes to the folder in question.
From: Lawrence D'Oliveiro on
In message <mailman.2077.1277482235.32709.python-list(a)python.org>, Ian Kelly
wrote:

> I use cron.
>
> Con: Most cron implementations have a maximum frequency of once per
> minute.

Another con is: what happens if a run takes longer than the invocation
frequency?
From: Peter H. Coffin on
On Sun, 27 Jun 2010 16:21:25 +1200, Lawrence D'Oliveiro wrote:
> In message <mailman.2077.1277482235.32709.python-list(a)python.org>, Ian Kelly
> wrote:
>
>> I use cron.
>>
>> Con: Most cron implementations have a maximum frequency of once per
>> minute.
>
> Another con is: what happens if a run takes longer than the invocation
> frequency?

Not cron's problem. Whatever mechanism you use to prevent
fumble-fingered keyboard actuators from running your thing more than
once simulaneously will suffice for cron as well.

--
Don't use this code for realtime control, for weapons systems, or for
anything else that may put life or limb at hazard. It isn't man-rated,
it isn't really thing-rated, and we don't claim that it's worth a good
G*dDamn for anything at all, at all. -- Mike Andrews, on Java compilers