标签:opp cio complete 并发 str python iter 装饰器 nis
#
# Generator with yield
#
astr = ‘ABC‘
alist = [1, 2, 3]
adict = dict(name=‘kct‘, age=18)
agen = (i for i in range(5))
def gen(*args):
for item in args:
for i in item:
yield i
new_list = gen(astr, alist, adict, agen)
print("use yield:", list(new_list))
#
# Generator with yield from
#
def fgen(*args):
for item in args:
yield from item
bgen = (i for i in range(5))
new_flist = fgen(astr, alist, adict, bgen)
print("use yield from:", list(new_flist))
#
# asyncio
#
from collections.abc import Coroutine
async def hello(name):
print(‘Hello, ‘, name)
# coroutine = hello(‘World‘)
# print(isinstance(coroutine, Coroutine))
#
# asyncio
#
import asyncio
from collections.abc import Generator
# 使用这个装饰器将该生成器标记为协程对象,本质还是生成器,所以内部不能使用await
@asyncio.coroutine
def hello2():
yield from asyncio.sleep(1)
coroutine = hello2()
print("Is generator:{}\nIs coroutine:{}\n".format(
isinstance(coroutine, Generator),
isinstance(coroutine, Coroutine)
))
#
# asyncio
#
coroutine = hello(‘World‘)
# 创建事件循环
loop = asyncio.get_event_loop()
# 将协程转换为任务
task = loop.create_task(coroutine)
# 将任务放入事件循环对象中触发
loop.run_until_complete(task)
# yield from 后可接iterable或future或Coroutine对象
# await 后必须接future或Coroutine对象
#
# asyncio
#
from asyncio.futures import Future
func = asyncio.sleep(2)
print("Is func Future:", isinstance(func, Future))
print("Is func Coroutine:", isinstance(func, Coroutine))
#
# 创建Future对象
#
task = asyncio.ensure_future(coroutine)
print("Is task Future:", isinstance(task, Future))
#
# 对象测试
#
import sys
async def f1():
await asyncio.sleep(2)
print(‘Hello, ‘, sys._getframe().f_code.co_name)
def f2():
yield from asyncio.sleep(2)
print(‘Hello, ‘, sys._getframe().f_code.co_name)
async def f3():
await asyncio.ensure_future(asyncio.sleep(2))
print(‘Hello, ‘, sys._getframe().f_code.co_name)
def f4():
yield from asyncio.ensure_future(asyncio.sleep(2))
print(‘Hello, ‘, sys._getframe().f_code.co_name)
#
# 回调函数获取结果
#
import time
async def _sleep(x):
time.sleep(x)
return ‘Stopped {} seconds!‘.format(x)
coroutine = _sleep(2)
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(coroutine)
loop.run_until_complete(task)
# 直接通过task获取任务结果
print(‘Result: {}‘.format(task.result()))
def callback(future):
print(‘Result: {}‘.format(future.result()))
coroutine = _sleep(2)
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(coroutine)
# 通过future获取结果
task.add_done_callback(callback)
loop.run_until_complete(task)
#
# 协程并发
#
# 协程函数
async def worker(n):
print(‘Waiting: {}‘.format(n))
await asyncio.sleep(n)
return ‘Done {}‘.format(sys._getframe().f_code.co_name)
# 协程对象
c1 = worker(1)
c2 = worker(2)
c3 = worker(4)
# 协程转换为task
tasks = [
asyncio.ensure_future(c1),
asyncio.ensure_future(c2),
asyncio.ensure_future(c3)
]
loop = asyncio.get_event_loop()
# 注册到事件循环(一)
# loop.run_until_complete(asyncio.wait(tasks))
# 注册到事件循环(二)
loop.run_until_complete(asyncio.gather(*tasks))
# 结果
for task in tasks:
print(‘Result: {}‘.format(task.result()))
标签:opp cio complete 并发 str python iter 装饰器 nis
原文地址:https://www.cnblogs.com/ikct2017/p/9534557.html