基于生成器的异步迭代算子
aiostream的Python项目详细描述
用于异步迭代的基于生成器的运算符
概要
aiostream提供了一组流运算符,可以组合这些运算符来创建 异步操作管道。
它可以看作是itertools的异步版本,尽管有些方面略有不同。 实际上,所有提供的运算符都返回一个称为流的统一接口。 流是增强的异步Iterable,提供以下功能:
- 操作员管道内衬-使用管道符号|
- 可重复性-每次迭代都创建不同的迭代器
- 安全的迭代上下文-使用async with和stream方法
- 简化执行-使用await 从流中获取最后一个元素
- 连接-使用加法符号+
流运算符
stream operators分为7类:
creation | iterate, preserve, just, empty, throw, never, repeat, count, range |
transformation | map, enumerate, starmap, cycle, chunks |
selection | take, takelast, skip, skiplast, getitem, filter, takewhile, dropwhile |
combination | map, zip, merge, chain, ziplatest |
aggregation | accumulate, reduce, list |
advanced | concat, flatten, switch, concatmap, flatmap, switchmap |
timing | spaceout, timeout, delay |
miscellaneous | action, print |
演示
下面的示例演示了大多数流功能:
importasynciofromaiostreamimportstream,pipeasyncdefmain():# Create a counting stream with a 0.2 seconds intervalxs=stream.count(interval=0.2)# Operators can be piped using '|'ys=xs|pipe.map(lambdax:x**2)# Streams can be slicedzs=ys[1:10:2]# Use a stream context for proper resource managementasyncwithzs.stream()asstreamer:# Asynchronous iterationasyncforzinstreamer:# Print 1, 9, 25, 49 and 81print('->',z)# Streams can be awaited and return the last valueprint('9² = ',awaitzs)# Streams can run several timesprint('9² = ',awaitzs)# Streams can be concatenatedone_two_three=stream.just(1)+stream.range(2,4)# Print [1, 2, 3]print(awaitstream.list(one_two_three))# Run main coroutineloop=asyncio.get_event_loop()loop.run_until_complete(main())loop.close()
文档的example section中提供了更多示例。
联系人
文森特·米歇尔:vxgmichel@gmail.com