Scenario: There is a server A and a client B, and there is a socket connection.
What we are writing now is part B of the client, which is not controllable by the server.
It turns out that B sends a packet first, waits for A to return the specified content, and then B sends the next packet
def do():
s.send(...)
yield 1
s.send(...)
yield 2
# 接收到数据后的回调
def callback():
global f
next(f)
f=do()
next(f)
Now I want to implement a timeout and implement blocking. After B sends the data, it blocks until A returns the data (or raises an error if it does not receive a return from A within 5 seconds). Please tell me how to achieve this?
With Tornado, I can’t write more than a few lines of code.
Let’s make a simple Server first to facilitate demonstration:
Then, to implement Client, the basic logic is to close the connection when timeout occurs, and then re-establish the connection: