Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82622
| References | <mailman.17010.1418843457.18130.python-list@python.org> <54921444$0$12995$c3e8da3$5496439d@news.astraweb.com> |
|---|---|
| From | Juan Christian <juan0christian@gmail.com> |
| Date | 2014-12-18 01:04 +0000 |
| Subject | Re: Is there a way to schedule my script? |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.17023.1418864662.18130.python-list@python.org> (permalink) |
[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
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