Thread: Loop Trouble

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

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

    Loop Trouble


    I'm having a bit of a problem with organizing my loops. I have one main loop, which is always receiving bytes (through a socket) from Battle.net. Meanwhile, if a user who has access on the bot (my program) requests the bot to run a certain command, the bot adds this command to the Queue (a basic Queue which waits 3-5 seconds to send a message, depending on how long it is), I will then start a loop to determine when the current time is 3-5 seconds after the time that the item got added to the queue, I do not want to use time.sleep() because I still wish to receiving data from Battle.net. Here is what I currently have:

    The Queue:
    Code:
    while 1:
        #code code code
    The non stop loop receiving data from Battle.net
    Code:
    while True:
        s.recv(1024) # briefly description what the code looks like
    I've noticed if I do it this way, the Queue will not send the message until the while True: loop receives 1024 bytes, is there a way to run the other loop first? And then continue the while True: loop? Or should the queue have while True: and the data received from Battle.net be using while 1:? Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    No idea on the blocking read - possibly need to insert a sleep call...

    As far as while loops go, you can stick anything which evaluates as True (1, True, "False", [False], etc). using "while True:" is customary in modern python code, though.

    --OH.
  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
    Well I understand that you mean running one loop and then the other. Just do something like:

    Code:
    while True:
    code code code
       while True:
          ...recv(1024)...
    The only bad thing about that way is to add a "break" in the first loop after the second.

    Hope it helps tell me if it is not the way you want it
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    I'm sorry if the first time I wasn't clear enough. I do not want to use sleep(), I wish to still receive data from Battle.net. I want the loop that receives data from Battle.net to keep running, when I go to start the next loop, when the second loop starts, at the given time, it will send the message to Battle.net, and break itself. Then the original loop which receives data from Battle.net should continue going. This is why I was wondering if I should use while 1: for receiving data, and while True: for the sending of the message to Battle.net. If they are both while True:, it creates a problem.
  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
    Ya since <-1,1> = True and 0 = False
    They are identical
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    I know this, but someone once told me that while 1: will (I can't remember word for word, just the basis of the idea) will stop the while True: loop and run its own loop, once it is broken, the other will continue on with its work.

IMN logo majestic logo threadwatch logo seochat tools logo