Thread: Switching Loops

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

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

    Switching Loops


    I'm having a bit of a problem switching from one loop to another. Because of the way Python reads code, I had to start a loop for my program's queue, and end another loop that had been current running.
    Here's the loop that is normally running:
    Code:
    while True:
        try:
            parse_data(s.recv(1024))
        except:
            print traceback.print_exc()
    parse_data() just sends the data received by the socket to a definition that determines what to print onto the screen, etc. Now, when something gets added to the Queue the program will send the message it was given, it will not send another one until 3 seconds has passes, I've created some code that does this. Here it is:
    Code:
    def Queue(data):
        q.append(data)
        if len(q) == 0:
            pass
        else:
            if send_next[0] == True:
                Send(q[0])
                q.pop(0)
                send_next[0] = False
                send_time[0] = time.strftime("%S")
            else:
                nFirst = send_time[0]
                if nFirst >= '00':
                    if nFirst[1] == '0':
                        nFirst = nFirst.replace(nFirst[1], '3')
                    elif nFirst[1] == '1':
                        nFirst = nFirst.replace(nFirst[1], '4')
                    elif nFirst[1] == '2':
                        nFirst = nFirst.replace(nFirst[1], '5')
                    elif nFirst[1] == '3':
                        nFirst = nFirst.replace(nFirst[1], '6')
                    elif nFirst[1] == '4':
                        nFirst = nFirst.replace(nFirst[1], '7')
                    elif nFirst[1] == '5':
                        nFirst = nFirst.replace(nFirst[1], '8')
                    elif nFirst[1] == '6':
                        nFirst = nFirst.replace(nFirst[1], '9')
                    elif nFirst[1] == '7':
                        if nFirst[0] == '0':
                            nFirst = nFirst.replace(nFirst[0], '1')
                            nFirst = nFirst.replace(nFirst[1], '0')
                        elif nFirst[0] == '1':
                            nFirst = nFirst.replace(nFirst[0], '2')
                            nFirst = nFirst.replace(nFirst[1], '0')
                        elif nFirst[0] == '2':
                            nFirst = nFirst.replace(nFirst[0], '3')
                            nFirst = nFirst.replace(nFirst[1], '0')
                        elif nFirst[0] == '3':
                            nFirst = nFirst.replace(nFirst[0], '4')
                            nFirst = nFirst.replace(nFirst[1], '0')
                        elif nFirst[0] == '4':
                            nFirst = nFirst.replace(nFirst[0], '5')
                            nFirst = nFirst.replace(nFirst[1], '0')
                        elif nFirst[0] == '5':
                            nFirst = nFirst.replace(nFirst[0], '0')
                            nFirst = nFirst.replace(nFirst[1], '0')
                while True:
                    try:
                        parse_data(s.recv(1024))
                    except:
                        print traceback.print_exc()
                    if time.strftime("%S") >= nFirst:
                        Send(q[0])
                        q.pop(0)
                        send_next[0] = False
                        break
                    else:
                        pass
    send_next (incase you weren't sure) determines whether to send the next message or not. Of course the queue needs a little bit more work, but is there a way to end the other loop I posted, and let this one run, and when it breaks, start the other one back up again? Also, the reason you see parse_data again in the second queue is so that I can still receive data, and this way it wouldn't be like doing time.sleep().
    Last edited by †Yegg†; March 4th, 2005 at 01:43 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    Well, i know there are probably 100s of ways of doing this. One way would be to make a loop around the hole thing so like:

    so you are going to have to have nested loops. one main one and then put the first loop you did inside of that, then put query outside of the nested while loop so that it will call the query and query will do its thing. Untill you want the other loop to run then you will return int he function and the main loop starts up top

    if that doesn't make since let me know, i can spend some time and make a bit of code for ya. I just thought this could be done without having me do that.

    anouther method might be threads, or forking. I just wanted to put that out there. it may be overkill for what your trying to do. But it could do the trick.
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Lol, actually, I didn't understand how to do that. Before you continue to answer my question, would using a list and checking whether it is True or False be a good way in knowing whether to end one loop and start another one?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    what do you mean by a list?

    here is some code, i havn't tested it so the syntax may be wrong but the general idea should work

    Code:
     def Queue(data):
    	q.append(data)
    	if len(q) == 0:
    		pass
    	else:
    		if send_next[0] == True:
    			Send(q[0])
    			q.pop(0)
    			send_next[0] = False
    			send_time[0] = time.strftime("%S")
    		else:
    			nFirst = send_time[0]
    			if nFirst >= '00':
    				if nFirst[1] == '0':
    					nFirst = nFirst.replace(nFirst[1], '3')
    				elif nFirst[1] == '1':
    					nFirst = nFirst.replace(nFirst[1], '4')
    				elif nFirst[1] == '2':
    					nFirst = nFirst.replace(nFirst[1], '5')
    				elif nFirst[1] == '3':
    					nFirst = nFirst.replace(nFirst[1], '6')
    				elif nFirst[1] == '4':
    					nFirst = nFirst.replace(nFirst[1], '7')
    				elif nFirst[1] == '5':
    					nFirst = nFirst.replace(nFirst[1], '8')
    				elif nFirst[1] == '6':
    					nFirst = nFirst.replace(nFirst[1], '9')
    				elif nFirst[1] == '7':
    					if nFirst[0] == '0':
    						nFirst = nFirst.replace(nFirst[0], '1')
    						nFirst = nFirst.replace(nFirst[1], '0')
    					elif nFirst[0] == '1':
    						nFirst = nFirst.replace(nFirst[0], '2')
    						nFirst = nFirst.replace(nFirst[1], '0')
    					elif nFirst[0] == '2':
    						nFirst = nFirst.replace(nFirst[0], '3')
    						nFirst = nFirst.replace(nFirst[1], '0')
    					elif nFirst[0] == '3':
    						nFirst = nFirst.replace(nFirst[0], '4')
    						nFirst = nFirst.replace(nFirst[1], '0')
    					elif nFirst[0] == '4':
    						nFirst = nFirst.replace(nFirst[0], '5')
    						nFirst = nFirst.replace(nFirst[1], '0')
    					elif nFirst[0] == '5':
    						nFirst = nFirst.replace(nFirst[0], '0')
    						nFirst = nFirst.replace(nFirst[1], '0')
    			while 1:
    				   while True:
    					   try:
    						  parse_data(s.recv(1024))
    					   except:
    						  print traceback.print_exc()
    					   if time.strftime("%S") >= nFirst:
    						   Send(q[0])
    						   q.pop(0)
    						   send_next[0] = False
    						   break
    					  else:
    						   pass 
    				   Queue(whateveryou send for data)
    This will run the function after the loop(the one that receives) is finished. then after the fuction returns it will go back up to the other loop(the one that receives)

    i hope this helps out
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I just have one question (before I test the code), where you have,
    Code:
    while 1:
        while True:
    Is that the code that makes the loop wait for any other loop that is currently running?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    ya, that is basicly the only modification i made. I havn't tested that code, because i don't know everything that your trying to do. But it work just fine.

    so that just will just go through your first loop then to your function then back. It will contiuely do this infently. unlss you deside to change that.
  12. #7
  13. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Technically the code is working, but its taking a very long time to send messages. I'm sure I can fix this on my own. Thanks for all the help CyBerHigh.

IMN logo majestic logo threadwatch logo seochat tools logo