Re: Is there a way to schedule my script?
On 12/18/2014 8:55 PM, Juan Christian wrote: On Thu Dec 18 2014 at 11:35:11 PM Chris Angelico mailto:ros...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
On Thu Dec 18 2014 at 11:35:11 PM Chris Angelico 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'. -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
On Fri, Dec 19, 2014 at 9:13 AM, Juan Christian 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
On Thu Dec 18 2014 at 2:24:46 PM Ian Kelly 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. -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
Thanks, using cron here. -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
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. -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
On 12/17/2014 01:42 PM, Juan Christian wrote: > On Wed Dec 17 2014 at 6:25:39 PM John Gordon 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. -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
On Thu, Dec 18, 2014 at 5:37 AM, Juan Christian 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. -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
On Wed Dec 17 2014 at 11:04:16 PM Juan Christian 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? -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
On Wed Dec 17 2014 at 9:40:52 PM Steven D'Aprano < steve+comp.lang.pyt...@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! -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
Hi Juan, I don't know what platform you're on, but you've got several options. Mac: setup a launchd job, I use http://www.soma-zone.com/LaunchControl/ to do the setups Linux/unix: setup a cron job, depending on your distro launchd may also be an option. Windows: setup a scheduled job in ?? (I don't have a windows box around any more, but there was a "Scheduled Jobs" section in windows explorer back in the XP days. I assume it's still around. In all cases, you'll need to add a little code in your script to STOP at 11:59, but the OS can handle starting the script. The launchd option can also act as a watchdog to also restart the script if it fails for some reason. Hope this helps! > On Dec 17, 2014, at 2:11 PM, Juan Christian wrote: > > Ops, sorry. > > It's: 9:00 AM ~ 11:59 PM -> Running > > ... and not 9:00 AM ~ 11:50 PM -> Running > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
On Wed Dec 17 2014 at 7:35:10 PM Chris Angelico wrote: time.time() % 86400 That's number of seconds since midnight UTC, ranging from 0 up to 86399. (I've no idea what 64562 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. -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
On Thu, Dec 18, 2014 at 7:42 AM, Juan Christian 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 > '64562'? 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 64562 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
On Wed Dec 17 2014 at 6:25:39 PM John Gordon 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 '64562'? And everyday it gets a reset when the clock 'turns'? -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
In Juan Christian 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 gor...@panix.comwatch 'House', or a real serial killer to watch 'Dexter'. -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
Ops, sorry. It's: 9:00 AM ~ 11:59 PM -> Running ... and not 9:00 AM ~ 11:50 PM -> Running -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
On Wed Dec 17 2014 at 5:45:31 PM John Gordon 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? -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to schedule my script?
In Juan Christian 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 gor...@panix.comwatch 'House', or a real serial killer to watch 'Dexter'. -- https://mail.python.org/mailman/listinfo/python-list
Is there a way to schedule my script?
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? -- https://mail.python.org/mailman/listinfo/python-list