AttributeError:“模块”对象没有属性“ensure_future”
Posted
技术标签:
【中文标题】AttributeError:“模块”对象没有属性“ensure_future”【英文标题】:AttributeError: 'module' object has no attribute 'ensure_future' 【发布时间】:2015-11-02 08:07:56 【问题描述】:您好,我正在使用 python asyncio 编写一个 n/w 绑定的服务器应用程序,它可以接受一个发布请求。
在发布请求中我接受一个符号参数
请告诉我处理 n/w 绑定应用程序的最佳方法。我在哪里通过向其他 web api 发送发布请求来收集数据。
以下是代码:
import asyncio
import aiohttp
import json
import logging
# async def fetch_content(url, symbols):
# yield from aiohttp.post(url, symbols=symbols)
@asyncio.coroutine
def fetch_page(writer, url, data):
response = yield from aiohttp.post(url, data=data)
resp = yield from response.read_and_close()
print(resp)
writer.write(resp)
return
@asyncio.coroutine
def process_payload(writer, data, scale):
tasks = []
data = data.split('\r\n\r\n')[1]
data = data.split('\n')
data = [x.split(':') for x in data]
print(data)
data = x[0]: x[1] for x in data
print(data)
# data = data[0].split(':')[1]
data = data['symbols']
print(data)
data = data.split(',')
data_len = len(data)
data_first = 0
data_last = scale
url = 'http://xxxxxx.xxxxxx.xxx/xxxx/xxxx'
while data_last < data_len:
tasks.append(asyncio.ensure_future(fetch_page(writer, url,'symbols': ",".join(data[data_first:data_last]))))
data_first += scale
data_last += scale
tasks.append(asyncio.ensure_future(fetch_page(writer, url,'symbols': ",".join(data[data_first:data_last]))))
loop.run_until_complete(tasks)
return
@asyncio.coroutine
def process_url(url):
pass
@asyncio.coroutine
def echo_server():
yield from asyncio.start_server(handle_connection, 'xxxxxx.xxxx.xxx', 3000)
@asyncio.coroutine
def handle_connection(reader, writer):
data = yield from reader.read(8192)
if data:
message = data.decode('utf-8')
print(message)
yield from process_payload(writer, message, 400)
writer.write_eof()
writer.close()
#url = 'http://XXXXXXX.xxxxx.xxx/xxxx/xxxxxx/xxx'
data = 'symbols': 'GD-US,14174T10,04523Y10,88739910,03209R10,46071F10,77543110,92847N10'
loop = asyncio.get_event_loop()
loop.run_until_complete(echo_server())
try:
loop.run_forever()
finally:
loop.close()
但我收到以下错误:
future: <Task finished coro=<handle_connection() done, defined at fql_server_async_v2.py:53> exception=AttributeError("'module' object has no attribute 'ensure_future'",)>
Traceback (most recent call last):
File "/home/user/anugupta/lib/python3.4/asyncio/tasks.py", line 234, in _step
result = coro.send(value)
File "fql_server_async_v2.py", line 60, in handle_connection
yield from process_payload(writer, message, 400)
File "/home/user/anugupta/lib/python3.4/asyncio/coroutines.py", line 141, in coro
res = func(*args, **kw)
File "fql_server_async_v2.py", line 41, in process_payload
tasks.append(asyncio.ensure_future(fetch_page(writer, url, 'symbols':",".join(data[data_first:data_last]))))
AttributeError: 'module' object has no attribute 'ensure_future'
^CTraceback (most recent call last):
File "fql_server_async_v2.py", line 72, in <module>
loop.run_forever()
File "/home/user/anugupta/lib/python3.4/asyncio/base_events.py", line 236, in run_forever
self._run_once()
File "/home/user/anugupta/lib/python3.4/asyncio/base_events.py", line 1017, in _run_once
event_list = self._selector.select(timeout)
File "/home/user/anugupta/lib/python3.4/selectors.py", line 424, in select
fd_event_list = self._epoll.poll(timeout, max_ev)
【问题讨论】:
当我通过对符号参数的发布请求发送大量数据时,无法一次读取全部数据。所以如果批量数据接收到空数据,请告诉我从发布请求中读取批量数据的最佳方法 asyncio.ensure_future 出现在 3.4.4 版本中,我猜你运行的是旧版本。 【参考方案1】:ensure_future
是在 asyncio 3.4.4 中添加的,对于早期版本,请使用 async
。
虽然async
现在已被弃用,但它会在未来得到支持。
【讨论】:
嗨,andrew,你能建议我一些从发布请求中读取批量数据的方法吗? data = symbols= '56633010,17290810,41308610,05945F10,51384710,74838J10,45168K30' 这里符号包含超过 20000 个值(逗号分隔),这里 data = yield from reader.read(8192) 如果符号给出空数据包含超过 200 个值。请建议我编写批量数据的方法,这些数据是我收到的另一个 Web api 对作者的响应。以上是关于AttributeError:“模块”对象没有属性“ensure_future”的主要内容,如果未能解决你的问题,请参考以下文章
AttributeError:“模块”对象没有属性“百分位”
无法安装模块 - AttributeError: 'NoneType' 对象没有属性 'loader'