协程

import asyncio  # python3.5添加的
import time

def f1():
time.sleep(1)

def work1():
for i in range(5):
f1()

# 协同函数
# 异步的操作(不必排队、不必等待的操作)
async def f2():
asyncio.sleep(1)

# 协同函数
async def work2():
await f2()

import asyncio
async def compute(x, y):
print("Compute %s + %s ..." % (x, y))
await asyncio.sleep(1.0)
return x + y
async def print_sum(x, y):
result = await compute(x, y)
print("%s + %s = %s" % (x, y, result))
loop = asyncio.get_event_loop()
loop.run_until_complete(print_sum(1, 2))
loop.close()
posted @ 2019-02-21 10:51  loser_coder  阅读(126)  评论(0)    收藏  举报