Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'indeed,': 0.09; 'subject:script': 0.09; "system's": 0.09; 'runs': 0.10; 'times,': 0.14; 'windows': 0.15; "(it's": 0.16; '(ubuntu': 0.16; 'possible?': 0.16; 'redo': 0.16; 'seconds.': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'true:': 0.16; 'unix/linux,': 0.16; 'you*.': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'all,': 0.19; 'bit': 0.19; 'module': 0.19; '<': 0.19; 'thanks.': 0.20; 'memory': 0.22; 'rid': 0.24; 'question': 0.24; 'script': 0.25; '>': 0.26; 'changes,': 0.26; 'this:': 0.26; 'least': 0.26; 'to:2**1': 0.27; "doesn't": 0.30; 'dec': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'container': 0.31; "d'aprano": 0.31; 'own,': 0.31; 'steven': 0.31; 'file': 0.32; 'probably': 0.32; 'critical': 0.32; 'stuff': 0.32; 'run': 0.32; 'another': 0.32; 'running': 0.33; 'checking': 0.33; 'except': 0.35; 'something': 0.35; 'no,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'controls': 0.36; 'scheduled': 0.36; 'doing': 0.36; "i'll": 0.36; 'subject:?': 0.36; 'should': 0.36; 'operating': 0.37; 'two': 0.37; 'christian': 0.38; 'system,': 0.38; 'server': 0.38; 'somebody': 0.38; 'work?': 0.38; 'handle': 0.38; 'whatever': 0.38; 'to:addr :python-list': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'is.': 0.60; 'manually': 0.60; 'matter': 0.61; 'times': 0.62; 'such': 0.63; 'great': 0.65; 'hours': 0.66; 'here': 0.66; 'jobs': 0.68; 'answer.': 0.68; 'subject:there': 0.68; 'killing': 0.84; 'scheduling': 0.84; 'ugly,': 0.84; 'approach.': 0.91; 'thing,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:from:date:message-id:subject:to :content-type; bh=/qkKsHekjzJWm0rolbqde7VkwhLRPe3+2c6viaMooFw=; b=ecbBrXKwsvgFAyeV7rSox0IEbHgmum3KuBh7rukC4X0SdkY0S764966BrYC94P8zSc aOo/ZNxDCli/8tu9Qu1yvIsisJwglCSHD5xMWTXO7C16b5tjckpk094eBYTE79gde1CQ WSAEBLrW01Ew5ANjhleTeLh5+xifh7EmvgB4gTHeISAbUYhiz90vs/rIIEIrLtNbSUbj x4nLAY4V15y2w0WzlewbblkMJ1R3eUMnjnTekSbx7BUllFPVUY1oZ5i+lm6D34XM7gKD ACsYK0X07zCsIsNLb8eAvu1Fn5AVCTyE4Nlqtdq2rghbcDpxiwBId97J3huJx+5tEf8/ mRFw== X-Received: by 10.107.136.92 with SMTP id k89mr43277592iod.43.1418864659098; Wed, 17 Dec 2014 17:04:19 -0800 (PST) MIME-Version: 1.0 References: <54921444$0$12995$c3e8da3$5496439d@news.astraweb.com> From: Juan Christian Date: Thu, 18 Dec 2014 01:04:18 +0000 Subject: Re: Is there a way to schedule my script? To: "Steven D'Aprano" , python-list@python.org Content-Type: multipart/alternative; boundary=001a113ecad4fd804d050a732bcb X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 164 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418864662 news.xs4all.nl 2926 [2001:888:2000:d::a6]:60777 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82622 --001a113ecad4fd804d050a732bcb Content-Type: text/plain; charset=UTF-8 On Wed Dec 17 2014 at 9:40:52 PM Steven D'Aprano < steve+comp.lang.python@pearwood.info> wrote: > Juan Christian wrote: > > > I know about the schedule modules and such but they work in situations > > like 'run this in a X hours/minutes/seconds interval', I already have my > > code in a while loop with sleep (it's a bit ugly, I'l change to a > > scheduler soon). > [...] > > I want my script to start at a given time and stop at another given time, > > is that possible? > > The right solution to this is probably to use your operating system's > scheduler to run your script at whatever time or times you want. Under > Unix/Linux, that is cron. I'm sure Windows will have it's own, but I don't > know what it is. > > Then your script then doesn't have to care about times at all, it just runs > and does its thing, and the OS controls when it runs. cron is amazingly > flexible. > > This is the proper separation of concerns. Your script doesn't have to deal > with memory management, or the file system, or scheduling times, that is > the operating system's job. The OS already has tools to do this, and can do > them *much better than you*. (What happens if your script dies? What about > when the time changes, say because of daylight savings?) > > Unless you are running on some primitive OS with no way to control when > jobs > run except to manually run them, the *wrong* solution is a busy-wait loop: > > while True: > # Wait forever, doing nothing. > sleep(0.1) # Yawn. > if now() == some_time(): > do_this() > > > It doesn't matter if you use the sched module to shift the time check to > another part of your code if the main loop does nothing. The critical > question here is this: > > While you are waiting for the scheduled time, does your main loop > continuously do any other work? > > If the answer is Yes, then using sched is the right approach. > > If the answer is No, then your main loop is just killing time, doing > nothing > but sleeping and waiting, like somebody checking their wristwatch every two > seconds. You should simplify your script by getting rid of the main loop > completely and let your OS handle the timing: > > # No loop at all. > do_this() > Thanks. That was a great answer. I'll redo my code. It's running and will only run in my Docker container (Ubuntu Server 14.04.1) so I'll use cron. Indeed, currently I'm using something like that: while True: if 9 < datetime.now().hour < 24: # do stuff sleep(randint(3, 6) * 60) else: # see you in 9 hours sleep(9 * 60 * 60) I knew it wasn't a good approach, but as least it was running as intended! --001a113ecad4fd804d050a732bcb Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

On Wed Dec 17 2014 at 9:40:52 PM Steven = D'Aprano <= steve+comp.lang.python@pearwood.info> wrote:
Juan Christian wrote:

> I know about the schedule modules and such but they work in situations=
> like 'run this in a X hours/minutes/seconds interval', I alrea= dy have my
> code in a while loop with sleep (it's a bit ugly, I'l change t= o a
> scheduler soon).
[...]
> I want my script to start at a given time and stop at another given ti= me,
> is that possible?

The right solution to this is probably to use your operating system's scheduler to run your script at whatever time or times you want. Under
Unix/Linux, that is cron. I'm sure Windows will have it's own, but = I don't
know what it is.

Then your script then doesn't have to care about times at all, it just = runs
and does its thing, and the OS controls when it runs. cron is amazingly
flexible.

This is the proper separation of concerns. Your script doesn't have to = deal
with memory management, or the file system, or scheduling times, that is the operating system's job. The OS already has tools to do this, and ca= n do
them *much better than you*. (What happens if your script dies? What about<= br> when the time changes, say because of daylight savings?)

Unless you are running on some primitive OS with no way to control when job= s
run except to manually run them, the *wrong* solution is a busy-wait loop:<= br>
while True:
=C2=A0 =C2=A0 # Wait forever, doing nothing.
=C2=A0 =C2=A0 sleep(0.1)=C2=A0 # Yawn.
=C2=A0 =C2=A0 if now() =3D=3D some_time():
=C2=A0 =C2=A0 =C2=A0 =C2=A0 do_this()


It doesn't matter if you use the sched module to shift the time check t= o
another part of your code if the main loop does nothing. The critical
question here is this:

While you are waiting for the scheduled time, does your main loop
continuously do any other work?

If the answer is Yes, then using sched is the right approach.

If the answer is No, then your main loop is just killing time, doing nothin= g
but sleeping and waiting, like somebody checking their wristwatch every two=
seconds. You should simplify your script by getting rid of the main loop completely and let your OS handle the timing:

# No loop at all.
do_this()

Thanks. That was a great answ= er. I'll redo my code. It's running and will only run in my Docker = container (Ubuntu Server 14.04.1) so I'll use cron.

Indeed, currently I'm using something like that:

while True:
=C2=A0 =C2=A0 if=C2=A09 < datetime.now().= hour < 24:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 # do stuff
= =C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0sleep(randint(3, 6) * 60)
=C2=A0= =C2=A0 else:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 # see you in 9 hours
=C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0sleep(9 * 60 * 60)

<= /div>
I knew it wasn't a good approach, but as least it was running= as intended!
--001a113ecad4fd804d050a732bcb--