Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82622

Re: Is there a way to schedule my script?

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 <juan0christian@gmail.com>
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; '&lt;': 0.19; 'thanks.': 0.20; 'memory': 0.22; 'rid': 0.24; 'question': 0.24; 'script': 0.25; '&gt;': 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 <mailman.17010.1418843457.18130.python-list@python.org> <54921444$0$12995$c3e8da3$5496439d@news.astraweb.com>
From Juan Christian <juan0christian@gmail.com>
Date Thu, 18 Dec 2014 01:04:18 +0000
Subject Re: Is there a way to schedule my script?
To "Steven D'Aprano" <steve+comp.lang.python@pearwood.info>, 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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17023.1418864662.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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!

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Is there a way to schedule my script? Juan Christian <juan0christian@gmail.com> - 2014-12-17 19:10 +0000
  Re: Is there a way to schedule my script? John Gordon <gordon@panix.com> - 2014-12-17 19:41 +0000
    Re: Is there a way to schedule my script? Juan Christian <juan0christian@gmail.com> - 2014-12-17 19:51 +0000
      Re: Is there a way to schedule my script? John Gordon <gordon@panix.com> - 2014-12-17 20:23 +0000
        Re: Is there a way to schedule my script? Juan Christian <juan0christian@gmail.com> - 2014-12-17 20:42 +0000
        Re: Is there a way to schedule my script? Chris Angelico <rosuav@gmail.com> - 2014-12-18 08:34 +1100
        Re: Is there a way to schedule my script? Juan Christian <juan0christian@gmail.com> - 2014-12-17 21:47 +0000
        Re: Is there a way to schedule my script? Michael Torrie <torriem@gmail.com> - 2014-12-18 09:45 -0700
  Re: Is there a way to schedule my script? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-18 10:39 +1100
    Re: Is there a way to schedule my script? Juan Christian <juan0christian@gmail.com> - 2014-12-18 01:04 +0000
    Re: Is there a way to schedule my script? Juan Christian <juan0christian@gmail.com> - 2014-12-18 12:37 +0000
    Re: Is there a way to schedule my script? Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-18 09:23 -0700
    Re: Is there a way to schedule my script? Juan Christian <juan0christian@gmail.com> - 2014-12-18 22:13 +0000
    Re: Is there a way to schedule my script? Chris Angelico <rosuav@gmail.com> - 2014-12-19 12:32 +1100
    Re: Is there a way to schedule my script? Juan Christian <juan0christian@gmail.com> - 2014-12-19 01:55 +0000
    Re: Is there a way to schedule my script? Terry Reedy <tjreedy@udel.edu> - 2014-12-19 00:58 -0500
  Re: Is there a way to schedule my script? Josh English <Joshua.R.English@gmail.com> - 2014-12-18 09:13 -0800
    Re: Is there a way to schedule my script? Juan Christian <juan0christian@gmail.com> - 2014-12-18 18:29 +0000

csiph-web