python3.5的话await只能在async装饰的函数内部使用,而且必须对一个协程/生成器类型的函数使用.这点据说在3.6已经放宽了..
# -*- coding: utf-8 -*-
import requests
import random
import datetime
import threading
import time
import asyncio
async def asy1(name: str = "0"):
print("{} begin".format(name))
r = await asyncio.sleep(random.randint(1, 3))
print("{} end".format(name))
return "{} finish".format(name)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
tasks = [asyncio.ensure_future(asy1(str(i))) for i in range(10)]
loop.run_until_complete(asyncio.wait(tasks))
print("all task is finish")
loop.close()
pass