You have a timeout function if a certain condition is not met on time

The problem is that my chat client must receive and print data from the server when the server sends it, and then allows the client to respond.

This works great, except that the whole process stops when the client is prompted to respond. Thus, messages are accumulated until you print something, and after that you can print all received messages.

I don’t know how to fix this , so I decided why the client does not have time to enter the response timeout after 5 seconds so that the answers can go regardless . This is pretty wrong, because the input will reset itself, but it works better anyway.

Here is the function that should have a timeout:

# now for outgoing data
def outgoing():
    global out_buffer
    while 1:
        user_input=input("your message: ")+"\n"
        if user_input:
            out_buffer += [user_input.encode()]
#       for i in wlist:
            s.send(out_buffer[0])
            out_buffer = []

How do I use a timeout? I thought to use time.sleep, but that just pauses the whole operation.

I tried looking for documentation. But I did not find anything that would help me make the program counted to a certain limit, and then continue.

Any idea on how to solve this? (No need to use the timeout, just need to stop the message overlay before the clients reply is sent) (Thanks to everyone who helped me get to this)

For Ionut Hulub:

from socket import *
import threading
import json
import select
import signal # for trying to create timeout


print("client")
HOST = input("connect to: ")
PORT = int(input("on port: "))

# create the socket
s = socket(AF_INET, SOCK_STREAM)
s.connect((HOST, PORT))
print("connected to:", HOST)

#--------- need 2 threads for handling incoming and outgoing messages--

#       1: create out_buffer:
out_buffer = []

# for incoming data
def incoming():
    rlist,wlist,xlist = select.select([s], out_buffer, [])
    while 1:
        for i in rlist:
            data = i.recv(1024)
            if data:
                print("\nreceived:", data.decode())

# now for outgoing data
def outgoing():
    global out_buffer
    while 1:
        user_input=input("your message: ")+"\n"
        if user_input:
            out_buffer += [user_input.encode()]
#       for i in wlist:
            s.send(out_buffer[0])
            out_buffer = []

thread_in = threading.Thread(target=incoming, args=())
thread_out = threading.Thread(target=outgoing, args=())
thread_in.start() # this causes the thread to run
thread_out.start()
thread_in.join()  # this waits until the thread has completed
thread_out.join()
+2
source share
1 answer

We can use signals for the same thing. I think the example below will be useful for you.

import signal

def timeout(signum, frame):
    raise Exception

#this is an infinite loop, never ending under normal circumstances
def main():
    print 'Starting Main ',
    while 1:
        print 'in main ',

#SIGALRM is only usable on a unix platform
signal.signal(signal.SIGALRM, timeout)

#change 5 to however many seconds you need
signal.alarm(5)

try:
    main()
except:
    print "whoops"
0
source

All Articles