Member Avatar for david56connor
david56connor

Hi there, I have a piece of code that I made to handle the joining of 'characters' in to a 'raid' on a game.
The program refreshes a page to check if the 'raid' is available, if it is, it 'forms' the raid and 'joins' the characters.
The joining process is what I am trying to make really fast, and I have done so sucessfully with the current method used as shown below, however it is not without its problems. For a start it does strange things when outputting text results as shown in the image below. That isn't a major issue however, although the next issue I have is that sometimes when the program is ran it stops half way through the 'joining' process ie when the threads are executing, and the program just hangs forever, I'm not great at debugging things like that so I'm not sure why it is happening.

http://desmond.imageshack.us/Himg401/scaled.php?server=401&filename=77235833.png&res=landing

class joinChar(threading.Thread):
    def __init__(self, charID):
        threading.Thread.__init__(self)
        self.CharID = charID
        self.result = "Empty"

    def getResult(self):
        return self.result

    def run(self):
        join = alo2('http://site=' + RAID_ID + '&suid=' + self.CharID + '&serverid=1', "join=1").read()
        x = 1
        while x < 6:

            if "error" in join:
                if "You must be a member" in join:
                    msg(self.CharID + " is not in the right crew")
                    x = 10
                elif "You do not see that mob here" in join:
                    msg(self.CharID + " is not in the right room")
                    x = 10
                else:                                       
                    join = alo2('http://site' + RAID_ID + '&suid=' + self.CharID + '&serverid=1', "join=1").read()
                    msg("Error joining " + self.CharID + " retrying..." + str(x))
                    x = x + 1
            else: 
                msg("Joined " + getCharName(join))
                self.result = join
                x = 10

def startJoining():
    def producer(q):
        for i in range(len(chars)):
            thread = joinChar(chars[i])
            thread.start()
            q.put(thread, True)

    global finished

    def consumer(q, total_chars):
        while len(finished) < total_chars:
            thread = q.get(True)
            thread.join(5)
            finished.append(thread.getResult())

    q = Queue(3)
    prod_thread = threading.Thread(target=producer, args=(q,))
    cons_thread = threading.Thread(target=consumer, args=(q, len(chars),))
    prod_thread.start()
    cons_thread.start()
    prod_thread.join()
    cons_thread.join()

    return finished

This is not the whole program there is more but I hope this gives you an idea of what I am doing with the producer and consumer (which I got somewhere else on the net!)

I added a 5 second timeout on the threads which isn't always ideal either in case it doesn't complete its tasks in that space of time.

Anyway if anyone knows of a better alternative or solution I would greatly appreciate any help with it!

Regards,
David.