我尝试使用asyncio为网络客户端创建非阻塞控制台。
import sys
import select
import tty
import termios
import asyncio
def isData():
return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])
@asyncio.coroutine
def load():
n = 0
while True:
print(n)
n += 1
yield from asyncio.sleep(1)
@asyncio.coroutine
def get_input():
old_settings = termios.tcgetattr(sys.stdin)
try:
tty.setcbreak(sys.stdin.fileno())
while True:
if isData():
c = sys.stdin.read(1)
print(c)
if c == '\x1b':
break
if c == '\x0a':
print('Enter pressed')
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
loop = asyncio.get_event_loop()
asyncio.ensure_future(load())
asyncio.ensure_future(get_input())
loop.run_forever()
load()模拟一些协同程序。它将被替换为与服务器的通信。
因此,使用get_input()进行非阻塞输入。它单独运行良好,但由于某种原因它阻止load()的执行。因为c = sys.stdin.read(1)
看起来像它可以使它像
data = yield from asyncio.wait_for(client_reader.readline(), timeout=TIMEOUT)
我用来从服务器读取数据。
或者也许用asyncio制作非阻塞控制台的直接方法在哪里?
更新: 可能的解决方案:
@asyncio.coroutine
def get_input():
old_settings = termios.tcgetattr(sys.stdin)
try:
tty.setcbreak(sys.stdin.fileno())
while True:
check = yield from loop.run_in_executor(None, isData)
if check:
c = sys.stdin.read(1)
print(c)
if c == '\x1b':
break
if c == '\x0a':
print('Enter pressed')
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)