我尝试了python请求库文档中提供的示例:
http://docs.python-requests.org/en/latest/user/advanced/#asynchronous-requests
与async.map(rs)我得到的响应代码,但我想请求每一页的内容.
out = async.map(rs)
print out[0].content
Run Code Online (Sandbox Code Playgroud)
例如,只是不工作.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
async def foo():
await time.sleep(1)
foo()
Run Code Online (Sandbox Code Playgroud)
我无法使这个简单的例子运行:
RuntimeWarning: coroutine 'foo' was never awaited foo()
Run Code Online (Sandbox Code Playgroud) 我一直在尝试创建一个服务器进程,该进程从客户端进程异步接收输入文件路径和输出路径。服务器执行一些依赖于数据库的转换,但为了简单起见,我们假设它只是将所有内容都设为大写。这是服务器的一个玩具示例:
import asyncio
import aiofiles as aiof
import logging
import sys
ADDRESS = ("localhost", 10000)
logging.basicConfig(level=logging.DEBUG,
format="%(name)s: %(message)s",
stream=sys.stderr)
log = logging.getLogger("main")
loop = asyncio.get_event_loop()
async def server(reader, writer):
log = logging.getLogger("process at {}:{}".format(*ADDRESS))
paths = await reader.read()
in_fp, out_fp = paths.splitlines()
log.debug("connection accepted")
log.debug("processing file {!r}, writing output to {!r}".format(in_fp, out_fp))
async with aiof.open(in_fp, loop=loop) as inp, aiof.open(out_fp, "w", loop=loop) as out:
async for line in inp:
out.write(line.upper())
out.flush()
writer.write(b"done")
await writer.drain()
log.debug("closing")
writer.close()
return
factory = asyncio.start_server(server, *ADDRESS) …Run Code Online (Sandbox Code Playgroud) asynchronous ×2
python ×2
python-3.5 ×2
async-await ×1
coroutine ×1
httprequest ×1
python-3.x ×1