#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    12
    Rep Power
    0

    While loop repeat every two seconds


    How to make a while loop which repeats every two seconds. Something like:

    while 1:
    Do something
    Wait two seconds and do it again
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    Python Code:
    import time
     
    while 1:
        print "I'm doing something"
        time.sleep(2)


    Regards, mawe

    Comments on this post

    • monkeyman23555 agrees : I agree
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    this will not do it excatly every two seconds
    this will do it then wait to seconds, to really make something, you need threading, but this is an advanced topic
    Those people who think they know everything are a great annoyance to those of us who do.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    12
    Rep Power
    0
    threading?

    Can you give an example?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    Code:
    import threading, time
    
    text = "long thing"
    
    def myFunc():
        c = 0
        
        global text
        
        time.sleep(1)
    
        for x in text:
            c += 1
    
        print c
        
    while True:
        time.sleep(2)
        t = threading.Thread(target=myFunc,args=())
        t.start()
    Well this sort of explains it...

    The while True: loop is already there and the sleep function then we make a thread, to split execution it to two branches and then the loop continues.

    The myFunc() is suppost to be really long to show really, but i did not get any ideas of how i could do that

    The declaration of the thread object will take a few milliseconds or not even...

    My point is the user is the slowest part of the program
    Those people who think they know everything are a great annoyance to those of us who do.
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I disagree with the use of threading in such a situation. Use the time module in a more respectful way. I'm very tired at the moment, so I won't do the code for you. But you could store the current epoch time (get this value by using time() of the time module, the return value is a float, so convert it to an integer by instead doing int(time()). The return value now as an integer is also in seconds, you can run a while loop and do a continuous check to see if the value has increased by 2. I hope you get the idea.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    lol I dont fully get the idea but owell

    I just wanted to outline a situtation:

    Were the loop continues and the function is called as a branch that it does not take any time from the 2 sec loop thing, which would make it longer.
    Those people who think they know everything are a great annoyance to those of us who do.
  14. #8
  15. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    More into detail of what I saw saying. You can use the time module to read the current seconds, wait until it has increased by 2, do something, wait until it increases by 2, do something, and so on. Surely you understand it better now?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    okay well that would waste time in between the the testing for the 2 seconds
    Those people who think they know everything are a great annoyance to those of us who do.
  18. #10
  19. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    It would still be less than 100 milliseconds off. Is that any bad?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    no :P, and i just gave another way
    Those people who think they know everything are a great annoyance to those of us who do.
  22. #12
  23. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Just for some variation - how about a slow for loop too
    python Code:
    import time
     
    def slowlist(alist = []):
        for n in alist:
            time.sleep(2)
            yield n
     
    for item in slowlist([23,12,6,4433,233]):
        print item


    Of course you can also use Tkinter and wxPython to generate timer events every (approx) 2 seconds as well.


    grim

IMN logo majestic logo threadwatch logo seochat tools logo