python異步回調轉為同步并實現超時
問題描述
場景:一個服務端A,一個客戶端B,存在一個socket連接。現在寫的是客戶端B部分,服務端不可控。原來是 B先發送一個包,等待A返回指定內容,B再發送下一個包
def do(): s.send(...) yield 1 s.send(...) yield 2# 接收到數據后的回調def callback(): global f next(f) f=do()next(f)
現在想實現一個timeout,并且實現阻塞。B發送數據后阻塞,直到A返回數據(或5秒內未接受到來自A的返回raise一個錯誤),請教如何實現?
問題解答
回答1:用 Tornado 的話,寫不了幾行代碼吧。
先作個簡單的 Server ,以方便演示:
# -*- coding: utf-8 -*-from tornado.ioloop import IOLoopfrom tornado.tcpserver import TCPServerfrom tornado import genclass Server(TCPServer): @gen.coroutine def handle_stream(self, stream, address):while 1: data = yield stream.read_until(’n’) if data.strip() == ’exit’:stream.close()break if data.strip() == ’5’:IOLoop.current().call_at(IOLoop.current().time() + 5, lambda: stream.write(’ok 5n’)) else:stream.write(’okn’)if __name__ == ’__main__’: Server().listen(8000) IOLoop.current().start()
然后,來實現 Client ,基本邏輯是,超時就關閉連接,然后再重新建立連接:
# -*- coding: utf-8 -*-import functoolsfrom tornado.ioloop import IOLoopfrom tornado.tcpclient import TCPClientfrom tornado import gendef when_error(stream): print ’ERROR’ stream.close() main()@gen.coroutinedef main(): client = TCPClient() stream = yield client.connect(’localhost’, 8000) count = 0 IL = IOLoop.current() while 1:count += 1stream.write(str(count) + ’n’)print count, ’...’timer = IL.call_at(IL.time() + 4, functools.partial(when_error, stream))try: data = yield stream.read_until(’n’)except: breakIL.remove_timeout(timer)print datayield gen.Task(IL.add_timeout, IOLoop.current().time() + 1)if __name__ == ’__main__’: main() IOLoop.current().start()
相關文章:
1. css - 如何使用 vue transition 實現 ios 按鈕一樣的平滑切換效果2. docker安裝后出現Cannot connect to the Docker daemon.3. 給變量賦值的時候是不是也要加//4. nignx - docker內nginx 80端口被占用5. angular.js - angular內容過長展開收起效果6. java - EventBus 3.0 nullPoint異常?7. css - 關于Flex布局的問題8. javascript - vue mint-ui 用不了。9. html - css 文件內,有的樣式沒調用, 有沒有工具刪除這部分沒調用的內容?10. javascript - 一個字符串轉換成數字,例子就是a="2,322.222",b=’1,211.21’,如何在angualr中執行相減
