Thread: Queue Help!

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

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

    Queue Help!


    Ok, I'm very confused with my Queue. Before I shwo you my code, here is what I'm trying to get it to do. When data is added to the Queue, it will attempt to Send the data to battle.net, however, you can only send (about) 4 messages at the same time before getting ipbanned from the server. What I want to do is if it's the first data that gets added, Send it right away, same with the second third and fourth, if it sends atleats 4 at the same time, wait 5 seconds before sending any more. So it will know how many messages have been sent before it sends one, and it will determine how long to wait to send the data.
    Here is the code I've come up with so far.
    Code:
    def RunQueue():
        if len(q) == 0:
            pass
        else:
            if q[0] >= 224:
                # Data too large
                q.pop(0)
            else:
                if len(q) == 1:
                    if qt[0] >= time.strftime("%S") + 4:
                          Send(q[0])
                          q.pop(0)
                    elif qt[0] >= time.strftime("%S") + 4:
                          Send(q[0])
                          q.pop(0)
    def Queue(data):
        try:
            added = added + 1
            qt.append(time.strftime("%S"))
            q.append(data)
            RunQueue()
        except:
            print traceback.print_exc()
    Now, my main problem is that I don't know how to determine how many messages have already been sent, if i use a list, how will i know when 5 seconds is up if its sent 4 messages at about the same time? Can anyone help?
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    How about some pseudo code like this...
    Code:
    Set message count variable to zero
    Set "previous" time variable to zero.
    
    On posting a message
    Note the time
    
    Is the message count less than 5
        increment message count and send immediatley
        set "previous" time to current time.  
    
    else 
         if time difference less then 5 then
                 pause 5-diiference seconds
        set count to zero
        send message
        set previous time to current time
        increment count
    grimey
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Thanks a lot for the code. It really cleared things up for me. I'm a little confused, but I think I can finish it this time on my own. Thanks again.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by †Yegg†
    Code:
    def RunQueue():
        if len(q) == 0:
            pass
        else:
            if q[0] >= 224:
                # Data too large
                q.pop(0)
            else:
                if len(q) == 1:
                    if qt[0] >= time.strftime("%S") + 4:
                          Send(q[0])
                          q.pop(0)
                    elif qt[0] >= time.strftime("%S") + 4:
                          Send(q[0])
                          q.pop(0)
    def Queue(data):
        try:
            added = added + 1
            qt.append(time.strftime("%S"))
            q.append(data)
            RunQueue()
        except:
            print traceback.print_exc()
    Just a few notes on the current code: you appear to be using the same expression for your last if and elif statements, in this case you don't need the elif at all . You also seem to be adding 4 to the string returned by the strftime() function, which has me a little confused.

    Anyway, I've made a few changes to the flow of the program that you might be interested in.

    Code:
    def RunQueue():
        if len(q) > 0:
            if q[0] >= 224:
                del qt[0]
            if len(q) == 1 and qt[0] >= time.strftime("%S") + 4:
                Send(q.pop(0))
    ...
    Note: this is untested and may not work like you want it to. I've also made the section I mentioned above bold to make it easier to see.

    Hope this helps,

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

  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Ya, I know, my current code now is much better. I'm just trying to get by a referrenced before assignment problem.

IMN logo majestic logo threadwatch logo seochat tools logo