Click here to Skip to main content
15,949,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What I'm looking to do is wait for a user input to send commands to a server. However, as input() is a blocking statement, I want to exit out of that statement if an incoming message from the server is received.

Currently I have two threads.

Thread 1:
Python
def user_input():
    global terminate
    global cmd

    while True:
        time.sleep(10)
        tLock.acquire()
        uInput = input("Enter: ")
        if(uInput == "1"):
            terminate = 1
            break
        if uInput == "2":
            mySock.mysend(cmd.getShutdown())
        if uInput == "3":
            mySock.mysend(cmd.getStartUp())
        if uInput == "4":
            mySock.mysend(cmd.getReset())

        tLock.release()


Thread 2:

Python
tLock.acquire()
    mySock = MySocket()
    mySock.connect()
    tLock.release()
    s1 = threading.Thread(target=user_input)
    s1.start()

    while(1):

        if mySock.dataCheck():
            tLock.acquire()
            incoming = mySock.myreceive()

            if len(incoming) > 0:
                
                print("Incoming: ", incoming)
                tLock.release()
                out = cmd.parse(incoming)

                if len(out) > 0:
                    tLock.acquire()
                    mySock.mysend(out)
                    tLock.release()

        if(terminate):
            break


What I have tried:

I know I can generate a signal using signal() to create a timeout for input, but I would like to generate a signal when

Python
if mySock.dataCheck():


is tripped. This function checks if there is any data is incoming. At that point I want to exit the input() statement and then lock out the input thread. I want to get rid of the 10 second delay I'm using to wait for any incoming messages.

Any insight?
Posted
Updated 5-Dec-19 5:25am
v2

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900