Python 3.4 中的“异步”
“async with” in Python 3.4
asyncssh 入门文档给出了以下 hello 示例:
import asyncio, asyncssh, sys
async def run_client():
async with asyncssh.connect('localhost') as conn:
result = await conn.run('echo "Hello!"', check=True)
print(result.stdout, end='')
try:
asyncio.get_event_loop().run_until_complete(run_client())
except (OSError, asyncssh.Error) as exc:
sys.exit('SSH connection failed: ' + str(exc))
但是,这不会 运行,因为 Python 3.4 不支持异步:
async with asyncssh.connect('localhost', username='squirtle', password='xxxxxxxxxxxx') as conn:
^
我去做了,这对我有用。
@asyncio.coroutine
def run_client():
with(yield from asyncssh.connect('localhost', username='root', password='xxxxxxxx')) as conn:
result = yield from conn.run('ls', check=True)
print(result.stdout, end='')
async
关键字是在Python3.5中引入的,你应该在早期版本中使用asyncio.corutine
。
勾选PEP492 and Python 3.5 release notes
你也想看看这个
asyncssh 入门文档给出了以下 hello 示例:
import asyncio, asyncssh, sys
async def run_client():
async with asyncssh.connect('localhost') as conn:
result = await conn.run('echo "Hello!"', check=True)
print(result.stdout, end='')
try:
asyncio.get_event_loop().run_until_complete(run_client())
except (OSError, asyncssh.Error) as exc:
sys.exit('SSH connection failed: ' + str(exc))
但是,这不会 运行,因为 Python 3.4 不支持异步:
async with asyncssh.connect('localhost', username='squirtle', password='xxxxxxxxxxxx') as conn:
^
我去做了,这对我有用。
@asyncio.coroutine
def run_client():
with(yield from asyncssh.connect('localhost', username='root', password='xxxxxxxx')) as conn:
result = yield from conn.run('ls', check=True)
print(result.stdout, end='')
async
关键字是在Python3.5中引入的,你应该在早期版本中使用asyncio.corutine
。
勾选PEP492 and Python 3.5 release notes
你也想看看这个