I have a script that spawns two threads, one for incoming data off a socket and one for outgoing. The series of execution is currently as so:
Connects to server
Outgoing thread sends login information
Incoming thread recieves some information back
Outgoing thread enters / leaves critical section
Outgoing thread enters / leaves critical section
Outgoing thread enters / leaves critical section
Incoming thread enters critical section
...and that's it.

The script seems to be pausing at the recv() function (in the incoming thread) and not allowing the other thread to run from here.
The thought I had about this is that recv() is blocking waiting for input off the socket and causing the script to just sit at that point. I'm new to threads in python and at this point I'm not 100% on what the exact problem is.

Code for the thread init function:

def start(self):
        xmlsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        xmlsocket.connect((self.server,self.port))
        print "Connected to server."
        #self.lock=thread.allocate_lock()
        #thread.start_new_thread(self.incXML,())
        #thread.start_new_thread(self.outXML,())
        #while not self.done:
        #    time.sleep(0.1)

        self.login()
        cond = Condition()
        ixml = incXML(cond,xmlsocket,self)
        oxml = outXML(cond,xmlsocket,self)

        ixml.start()
        oxml.start()

        while not self.done:
            pass

        sys.exit(0)

For outgoing thread:

class incXML(Thread):
    # send login information (maybe)
        # xmlsocket.send(self.xmlstr)
    def __init__(self,condition,xmlsocket,net):
        Thread.__init__(self)
        self.cond = condition
        self.xmlsocket = xmlsocket
        self.net = net
        self.alive = 1

    def run(self):
        cond = self.cond
        xmlsocket = self.xmlsocket
        net = self.net
        data = None

        while self.alive:
            time.sleep(2)
            cond.acquire()
            print "inc thread lock acquired"
            try:
                data = xmlsocket.recv(1024)
            except:
                pass
            #while data == None:
            #    cond.wait()
            #print "From server: " + data
            # parse incoming data
            print "check for incdata"
            index = 0
            found = 0
            while found >= 0:
                if not data: break
                found = data.find("<?xml version=\"1.0\"?>",index + 1)
                if found >= 0:
                    odata = data[index:found]
                else:
                    odata = data[index:]
                odata = odata.strip()
                odata = odata.replace('\x00','')
                #print odata
                net.processXML(odata)
                index = found
            data = None
            cond.release()
            print "inc thread lock released"
            time.sleep(2)
        return
    def kill(self):
        self.alive = 0

For outgoing thread:

class outXML(Thread):

    def __init__(self,condition,xmlsocket,net):
        Thread.__init__(self)
        self.cond = condition
        self.xmlsocket = xmlsocket
        self.net = net
        self.alive = 1

    def run(self):
        cond = self.cond
        xmlsocket = self.xmlsocket
        net = self.net
        data = None

        while self.alive:
            cond.acquire()
            print "out thread lock acquired"
            data = net.getXML()
            #while data == None:
            #    cond.wait()
            # xmlstr has a message for the server, engage!
            print "check for outdata"
            if data:
                xmlsocket.send(data)
                print "Sending to server: " + data
            cond.release()
            print "out thread lock released"
            time.sleep(2)
    def kill(self):
        self.alive = 0

Any help or insight would be greatly appreciated. A full listing of the code can be found here: http://www.tcnj.edu/~bolling3/code/rMusicCnet.py
Thanks in advance!

I removed the lock and it runs fine now, but I still have a feeling having both of them using the same socket isn't kosher. Or at least using the same socket without some sort of synchronization.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.