Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82606 > unrolled thread
| Started by | Juan Christian <juan0christian@gmail.com> |
|---|---|
| First post | 2014-12-17 19:10 +0000 |
| Last post | 2014-12-18 18:29 +0000 |
| Articles | 18 — 8 participants |
Back to article view | Back to comp.lang.python
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
| From | Juan Christian <juan0christian@gmail.com> |
|---|---|
| Date | 2014-12-17 19:10 +0000 |
| Subject | Is there a way to schedule my script? |
| Message-ID | <mailman.17010.1418843457.18130.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
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). What I really want is, for example: 24/7/365 9:00 AM -> Start 11:59 PM -> Stop 9:00 AM ~ 11:50 PM -> Running 12:00 AM ~ 8:59 AM -> Stopped I want my script to start at a given time and stop at another given time, is that possible?
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2014-12-17 19:41 +0000 |
| Message-ID | <m6sm9d$5es$1@reader1.panix.com> |
| In reply to | #82606 |
In <mailman.17010.1418843457.18130.python-list@python.org> Juan Christian <juan0christian@gmail.com> writes: > --047d7b874b2c1e67eb050a6e3cc4 > Content-Type: text/plain; charset=UTF-8 > 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). > What I really want is, for example: > 24/7/365 > 9:00 AM -> Start > 11:59 PM -> Stop > 9:00 AM ~ 11:50 PM -> Running > 12:00 AM ~ 8:59 AM -> Stopped > I want my script to start at a given time and stop at another given time, > is that possible? You could write a separate program whose only job is to send a STOP or CONTINUE signal to your main program, and then run that program from a scheduler. The standard system "kill" command would probably work for this purpose, assuming you have access to your main program's process ID. -- John Gordon Imagine what it must be like for a real medical doctor to gordon@panix.com watch 'House', or a real serial killer to watch 'Dexter'.
[toc] | [prev] | [next] | [standalone]
| From | Juan Christian <juan0christian@gmail.com> |
|---|---|
| Date | 2014-12-17 19:51 +0000 |
| Message-ID | <mailman.17012.1418845926.18130.python-list@python.org> |
| In reply to | #82607 |
[Multipart message — attachments visible in raw view] — view raw
On Wed Dec 17 2014 at 5:45:31 PM John Gordon <gordon@panix.com> wrote: You could write a separate program whose only job is to send a STOP or CONTINUE signal to your main program, and then run that program from a scheduler. The standard system "kill" command would probably work for this purpose, assuming you have access to your main program's process ID. There isn't any 'prettier' way? Such as a built-in or third-party module for something common like that?
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2014-12-17 20:23 +0000 |
| Message-ID | <m6soo5$mc3$1@reader1.panix.com> |
| In reply to | #82609 |
In <mailman.17012.1418845926.18130.python-list@python.org> Juan Christian <juan0christian@gmail.com> writes: > The standard system "kill" command would probably work for this purpose, > assuming you have access to your main program's process ID. > There isn't any 'prettier' way? Such as a built-in or third-party module > for something common like that? If you're on Unix, 'kill' and 'cron' are already built-in. If you want to solve your problem entirely within Python, look at the "scheduler" module. (Although even this isn't a complete solution, as you still have to make sure the program is running in the first place...) -- John Gordon Imagine what it must be like for a real medical doctor to gordon@panix.com watch 'House', or a real serial killer to watch 'Dexter'.
[toc] | [prev] | [next] | [standalone]
| From | Juan Christian <juan0christian@gmail.com> |
|---|---|
| Date | 2014-12-17 20:42 +0000 |
| Message-ID | <mailman.17017.1418849283.18130.python-list@python.org> |
| In reply to | #82613 |
[Multipart message — attachments visible in raw view] — view raw
On Wed Dec 17 2014 at 6:25:39 PM John Gordon <gordon@panix.com> wrote: If you want to solve your problem entirely within Python, look at the "scheduler" module. (Although even this isn't a complete solution, as you still have to make sure the program is running in the first place...) My script is running fine, Win/OSX/Linux and I don't want to ruin that using system specific things. I looked at sched doc and it's only for creating a delay, maybe a good approach would be to call the sched and check if time = 11:59PM, then set delay to 1h and when the time goes 9AM, it returns to my normal delay. Is there any kind of time calculation in Python that counts the time like 0, 1, 2, 3... so that 0AM would be 0, and 11:59PM would be let's say '645633332'? And everyday it gets a reset when the clock 'turns'?
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-12-18 08:34 +1100 |
| Message-ID | <mailman.17018.1418852082.18130.python-list@python.org> |
| In reply to | #82613 |
On Thu, Dec 18, 2014 at 7:42 AM, Juan Christian <juan0christian@gmail.com> wrote: > Is there any kind of time calculation in Python that counts the time like 0, > 1, 2, 3... so that 0AM would be 0, and 11:59PM would be let's say > '645633332'? And everyday it gets a reset when the clock 'turns'? time.time() % 86400 That's number of seconds since midnight UTC, ranging from 0 up to 86399. (I've no idea what 645633332 would mean. That's an awfully big number for a single day.) If you offset that before calculating, you can get that in your local time. Otherwise, just do the arithmetic directly. Time isn't all _that_ hard to work with. I don't see what's the big problem with just using sleep() though. Isn't that exactly what you're after? ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Juan Christian <juan0christian@gmail.com> |
|---|---|
| Date | 2014-12-17 21:47 +0000 |
| Message-ID | <mailman.17019.1418852858.18130.python-list@python.org> |
| In reply to | #82613 |
[Multipart message — attachments visible in raw view] — view raw
On Wed Dec 17 2014 at 7:35:10 PM Chris Angelico <rosuav@gmail.com> wrote: time.time() % 86400 That's number of seconds since midnight UTC, ranging from 0 up to 86399. (I've no idea what 645633332 would mean. That's an awfully big number for a single day.) If you offset that before calculating, you can get that in your local time. Otherwise, just do the arithmetic directly. Time isn't all _that_ hard to work with. I don't see what's the big problem with just using sleep() though. Isn't that exactly what you're after? This was a random number I invented.... So, I'm already using sleep to make my script execute some funcs in a defined interval, but when the time is 0AM-9AM I don't want the delay to be the normal one (randint(5,10) * 60) - 5~10min, I want it to be like 2hours. The script will be running 24/7, but from 0AM to 9AM it will "slowdown" a bit.
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2014-12-18 09:45 -0700 |
| Message-ID | <mailman.17034.1418921123.18130.python-list@python.org> |
| In reply to | #82613 |
On 12/17/2014 01:42 PM, Juan Christian wrote:
> On Wed Dec 17 2014 at 6:25:39 PM John Gordon <gordon@panix.com> wrote:
> If you want to solve your problem entirely within Python, look at the
> "scheduler" module. (Although even this isn't a complete solution, as you
> still have to make sure the program is running in the first place...)
>
>
> My script is running fine, Win/OSX/Linux and I don't want to ruin that
> using system specific things.
Wrong. You don't have to change or ruin your script. If your script is
done right, you put all the real work inside of callable functions
anyway, and probably have some sort of "main" function that gets called
in a manner similar to this:
if __name__=="__main__":
my_main()
If so, then you create platform-dependent code in another file that
simply imports your existing, working script as a module and runs that
main function. On Windows you write a service API wrapper. On Linux
you can run the script directly from cron. On Mac you can just bundle a
launchd control file (or use cron).
If your script is not coded in such a fashion as to make turning it into
an importable module easy, I highly recommend changing your to work in
this way. Once my python programs get halfway useful I always try to
reorganize my code in such a way that it can be used as a module.
Because invariable I find that I do want to add another layer, and the
modules are ideal for this. Nearly every script has the "if
__name__=='__main__'" block at the end of the file, where it provides
standalone features such as a command-line interface, or provides
testing of the module's features.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-12-18 10:39 +1100 |
| Message-ID | <54921444$0$12995$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #82606 |
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()
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Juan Christian <juan0christian@gmail.com> |
|---|---|
| Date | 2014-12-18 01:04 +0000 |
| Message-ID | <mailman.17023.1418864662.18130.python-list@python.org> |
| In reply to | #82620 |
[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!
[toc] | [prev] | [next] | [standalone]
| From | Juan Christian <juan0christian@gmail.com> |
|---|---|
| Date | 2014-12-18 12:37 +0000 |
| Message-ID | <mailman.17030.1418906581.18130.python-list@python.org> |
| In reply to | #82620 |
[Multipart message — attachments visible in raw view] — view raw
On Wed Dec 17 2014 at 11:04:16 PM Juan Christian <juan0christian@gmail.com> wrote: 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! I read the cron doc, it's really simple to use, but one think I didn't see out-of-the-box is a way to set a random time, like 'execute this in a 5~10 min interval', I can only set specific times like 'execute this each minute, each hour, each 10min' and so on. I found a 'fix' for that using $RANDOM in the crontab. Another workaround would be to set a fixed 5min interval in cron and inside my script have a random int (0 or 1), when 0, the script doesn't execute and 1 it execute, so that way I'd have a little 'randomness' that I need. Which approach would be the best?
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-12-18 09:23 -0700 |
| Message-ID | <mailman.17031.1418919835.18130.python-list@python.org> |
| In reply to | #82620 |
[Multipart message — attachments visible in raw view] — view raw
On Thu, Dec 18, 2014 at 5:37 AM, Juan Christian <juan0christian@gmail.com> wrote: > I read the cron doc, it's really simple to use, but one think I didn't see out-of-the-box is a way to set a random time, like 'execute this in a 5~10 min interval', I can only set specific times like 'execute this each minute, each hour, each 10min' and so on. > > I found a 'fix' for that using $RANDOM in the crontab. Another workaround would be to set a fixed 5min interval in cron and inside my script have a random int (0 or 1), when 0, the script doesn't execute and 1 it execute, so that way I'd have a little 'randomness' that I need. > > Which approach would be the best? What kind of random distribution of the time between executions are you looking for? A random sleep lends itself easily to a uniform distribution. The latter approach that you describe would result in a geometric distribution.
[toc] | [prev] | [next] | [standalone]
| From | Juan Christian <juan0christian@gmail.com> |
|---|---|
| Date | 2014-12-18 22:13 +0000 |
| Message-ID | <mailman.17041.1418940799.18130.python-list@python.org> |
| In reply to | #82620 |
[Multipart message — attachments visible in raw view] — view raw
On Thu Dec 18 2014 at 2:24:46 PM Ian Kelly <ian.g.kelly@gmail.com> wrote: What kind of random distribution of the time between executions are you looking for? A random sleep lends itself easily to a uniform distribution. The latter approach that you describe would result in a geometric distribution. I'm looking for a random, but controlled delay between executions. Let's say I execute the script now, then in 5~10 min I'll execute again, this time can be 5, 6, ... 10 minutes, this script pretends to do 'human actions' so I can't be doing these 'actions' with a specific and rigid times. The delay between the executions can't be the same always.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-12-19 12:32 +1100 |
| Message-ID | <mailman.17042.1418952785.18130.python-list@python.org> |
| In reply to | #82620 |
On Fri, Dec 19, 2014 at 9:13 AM, Juan Christian <juan0christian@gmail.com> wrote: > Let's say I execute the script now, then in 5~10 min I'll execute again, > this time can be 5, 6, ... 10 minutes, this script pretends to do 'human > actions' so I can't be doing these 'actions' with a specific and rigid > times. Why does this matter to you? Why am I getting the feeling that I should not be helping you? ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Juan Christian <juan0christian@gmail.com> |
|---|---|
| Date | 2014-12-19 01:55 +0000 |
| Message-ID | <mailman.17043.1418954132.18130.python-list@python.org> |
| In reply to | #82620 |
[Multipart message — attachments visible in raw view] — view raw
On Thu Dec 18 2014 at 11:35:11 PM Chris Angelico <rosuav@gmail.com> wrote: Why does this matter to you? Why am I getting the feeling that I should not be helping you? Because that's what my project is all about, I need to fake some 'human actions' inside the network to do some benchmarks and test internal stuffs. This need to be 'flexible'.
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2014-12-19 00:58 -0500 |
| Message-ID | <mailman.17044.1418968718.18130.python-list@python.org> |
| In reply to | #82620 |
On 12/18/2014 8:55 PM, Juan Christian wrote: > On Thu Dec 18 2014 at 11:35:11 PM Chris Angelico <rosuav@gmail.com > <mailto:rosuav@gmail.com>> wrote: > Why does this matter to you? Why am I getting the feeling that I > should not be helping you? > > Because that's what my project is all about, I need to fake some 'human > actions' inside the network to do some benchmarks and test internal > stuffs. This need to be 'flexible'. Tkinter's .after method makes it trivial to schedule and run a function at either regular or haphazardly variable intervals and add the result to a gui display. -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Josh English <Joshua.R.English@gmail.com> |
|---|---|
| Date | 2014-12-18 09:13 -0800 |
| Message-ID | <ffef7e7b-0a34-4139-8331-8ff6d86b81e2@googlegroups.com> |
| In reply to | #82606 |
On Wednesday, December 17, 2014 11:11:11 AM UTC-8, 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). > > > What I really want is, for example: > > > 24/7/365 > 9:00 AM -> Start > 11:59 PM -> Stop > > > 9:00 AM ~ 11:50 PM -> Running > 12:00 AM ~ 8:59 AM -> Stopped > > > I want my script to start at a given time and stop at another given time, is that possible? Windows comes with a Task Scheduler but as I'm playing with it, it only seems to allow starting a program, but not actually shutting it down. I would consider including a timed shutdown in your program or build another app, as is suggested below, to send SHUTDOWN commands to running apps. How you would link the running processes, I do not fully understand.
[toc] | [prev] | [next] | [standalone]
| From | Juan Christian <juan0christian@gmail.com> |
|---|---|
| Date | 2014-12-18 18:29 +0000 |
| Message-ID | <mailman.17039.1418927372.18130.python-list@python.org> |
| In reply to | #82641 |
[Multipart message — attachments visible in raw view] — view raw
Thanks, using cron here.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web