Thread: Do When?

    #1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    Do When?


    I couldn't think of a good title. In Python is there a way I can do something when the current time equals a specified time? For example, wait like 2 seconds to send a message? Is there a way to do something like this without using time.sleep()?
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You could create an infinite (while True) loop, then check the time to see if it is time to preform a given action i.e. like an alarm clock. Some details on what you want to do might help here .

    I wrote a cron class a while back that you might be interested in: http://forums.devshed.com/t113931/s.html

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    The while Loop should probably work, I didn't think about that, but I'm also using that Loop for parsing the data I receive from battle.net,
    while True:
    parse_data(s.recv())
    How can I run both Loops?
    I also wondering if I could use a list, and update listname[0] every time I needed to, this way I could determine when the last message was sent so that I wouldn't get ipbanend from the server, would this work?
    Another update:I just thought of this as well, I may just edit the code where the commands are so that if it's sending one message, the added amount will be 1, if its sending 4, the added list will be 4, and so on, this way it will know how much to pause (time.sleep()). Because of my while True loop I decided to just use time.sleep().
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,592
    Rep Power
    4207
    All OSs have some way to schedule a job. In *nix and Mac OSX, there's the cron job. In Windows, you can use the AT command (type "help AT" at the DOS prompt for more). It is better to use these commands rather than looping and waiting for a certain time to occur. Reason: Sys admins take a rather dim view of long running daemons that they don't know about and will usually kill the process first and ask questions later.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Whats wrong with time.sleep? If you write a loop that does nothing but continually check the current time then you are likely to slow your computer to a crawl since it will be chewing up all the processing time. calling time.sleep suspends your code so that other programs and threads can get on with their jobs.

    Dave - The Developers' Coach
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Correct me if Im wrong, but doesn't time.sleep() use a loop itself? I've seen this done in a few C books/tutorials to stall for time *shugs* . Would be interesting to know anyway.

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,592
    Rep Power
    4207
    Whether time.sleep uses a loop or a suspend syscall internally is besides the point. Most sysadmins panic when they see an unknown process running for a while. It doesn't matter if it uses up any CPU cycles or not, they'll gun it first and slap you around a bit later.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo