具有异步IO的非阻塞控制台。python

具有异步IO的非阻塞控制台。python,python,console,nonblocking,python-asyncio,Python,Console,Nonblocking,Python Asyncio,我正在尝试使用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:

我正在尝试使用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\u for(client\u 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)

我写了一些类似的东西,并在一个名为。它包括了一个很好的例子和写文章的可能性。@Vincent非常有趣。。。验尸你的密码需要一些时间。但我仍然需要理解为什么我的代码不起作用?Asyncio已经在幕后使用了
select
,所以你应该改为使用,例如:
loop.add\u reader(sys.stdin.fileno(),on\u stdin)
@Vincent看起来只有在按下Enter键后才会调用回调,但在按下Enter键时不会调用回调。这不是我所期望的……那是因为
sys.stdin
是。你可以使用,但事情会变得一团糟。