threading python bind several ports
Question: I want to do a simple thing: just bind two ports to wait for incoming
connections and continous with the application code. This is the code.

    
    
    import socket
    import threading
    import Queue
    
    q = Queue.Queue()
    q2 = Queue.Queue()
    
    def escucha_9003():
    
        s = socket.socket()
        s.bind(("localhost",9023))
        s.listen(10) 
        sc, address = s.accept()
        q.put(sc)
    
    
    
    def escucha_9004():
    
        s2 = socket.socket()
        s2.bind(("localhost",9024))
        s2.listen(10) 
        sc2, address2 = s2.accept()
        q.put(sc2)
    
    hilo = threading.Thread(target=escucha_9003).start()
    sc2=q.get()
    hilo2 = threading.Thread(target=escucha_9004).start()
    sc=q2.get()
    
    print "i never arrive here"
    

I need to get some parameters that are returned in each function. I use Queue
library to do it. But the problem is that i want to keep working with the code
and I never arrive to the line: print "i never arrive here". At least it is
never printed.

How need I do it to continue developing after launch those two threads and
work with the incoming connections.

Thank you very much


Answer: Both `.get()` calls are blocking calls. Also, the `.accept()` calls in your
threads are blocking. Technically you should never even get past the two
`.accept()` calls if no connections are coming in from clients. Because the
main thread is waiting on `get()` calls from the queues, but the queues never
put anything because the threads are waiting on accepting a future connection.

What you would need to do is handle your queues from within threads instead of
inside of the main thread. Otherwise, you will need to use
`.get(timeout=someSeconds)` inside of an event loop where you keep checking
for more stuff in your queues for a certain amount of time, and then move on
to do more continuous processing.

You may want to explain exactly what you are trying to achieve, and then
restructure these threads to handle a bit more of their own work before
blocking the main thread to receive data.

