AttributeError:'模块'对象没有属性' ensure_future'

时间:2015-11-02 08:07:56

标签: python-3.x asynchronous asyncsocket python-asyncio

您好我正在使用python asyncio编写一个n / w绑定服务器应用程序,它可以接受发布请求。

在帖子请求中,我接受符号参数

请告诉我处理n / w绑定应用程序的最佳方法。我通过向他们发送帖子请求从其他网络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)

1 个答案:

答案 0 :(得分:2)

asyncio 3.4.4 中添加了

ensure_future,对于早期版本使用async

虽然async已被弃用,但现在它将在过度的支持下得到支持。