RxPY

RxPY 是 ReactiveX 的 Python语言实现。

# 安装 RxPY
$ pip3 install rx
Successfully installed rx-1.6.1

Basics

from rx import Observable
source = Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon")
source.subscribe(on_next=lambda value: print("Received {0}".format(value)),
on_completed=lambda: print("Done!"),
on_error=lambda error: print("Error Occurred: {0}".format(error))
)
'''
Received Alpha
Received Beta
Received Gamma
Received Delta
Received Epsilon
Done!
'''

Operators

from rx import Observable
Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon") \
.map(lambda s: len(s)) \
.filter(lambda i: i >= 5) \
.subscribe(lambda value: print("Received {0}".format(value)))
'''
Received 5
Received 5
Received 5
Received 7
'''

Events

from rx import Observable
Observable.interval(1000) \
.map(lambda i: "{0} Mississippi".format(i)) \
.subscribe(lambda s: print(s))
input("Press any key to quit\n")
'''
Press any key to quit
0 Mississippi
1 Mississippi
2 Mississippi
3 Mississippi
4 Mississippi
...
'''

Multicasting

from rx import Observable
from random import randint
three_emissions = Observable.range(1, 3)
three_random_ints = three_emissions.map(lambda i: randint(1, 100000))
three_random_ints.subscribe(lambda i: print("Subscriber 1 Received: {0}".format(i)))
three_random_ints.subscribe(lambda i: print("Subscriber 2 Received: {0}".format(i)))
'''
Subscriber 1 Received: 76795
Subscriber 1 Received: 19486
Subscriber 1 Received: 96809
Subscriber 2 Received: 79148
Subscriber 2 Received: 33423
Subscriber 2 Received: 3319
'''
from rx import Observable
from random import randint
three_emissions = Observable.range(1, 3)
three_random_ints = three_emissions.map(lambda i: randint(1, 100000)).publish()
three_random_ints.subscribe(lambda i: print("Subscriber 1 Received: {0}".format(i)))
three_random_ints.subscribe(lambda i: print("Subscriber 2 Received: {0}".format(i)))
three_random_ints.connect()
'''
Subscriber 1 Received: 83159
Subscriber 2 Received: 83159
Subscriber 1 Received: 82025
Subscriber 2 Received: 82025
Subscriber 1 Received: 72397
Subscriber 2 Received: 72397
'''
from rx import Observable
from random import randint
three_emissions = Observable.range(1, 3)
three_random_ints = three_emissions.map(lambda i: randint(1, 100000)).publish().auto_connect(2)
three_random_ints.subscribe(lambda i: print("Subscriber 1 Received: {0}".format(i)))
three_random_ints.subscribe(lambda i: print("Subscriber 2 Received: {0}".format(i))) # second subscriber triggers firing
'''
Subscriber 1 Received: 72527
Subscriber 2 Received: 72527
Subscriber 1 Received: 53066
Subscriber 2 Received: 53066
Subscriber 1 Received: 32249
Subscriber 2 Received: 32249
'''

Combining

from rx import Observable
letters = Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon")
intervals = Observable.interval(1000)
Observable.zip(letters, intervals, lambda s, i: (s, i)) \
.subscribe(lambda t: print(t))
input("Press any key to quit\n")
'''
Press any key to quit
('Alpha', 0)
('Beta', 1)
('Gamma', 2)
('Delta', 3)
('Epsilon', 4)
'''

Concurrency

import multiprocessing
import random
import time
from threading import current_thread
from rx import Observable
from rx.concurrency import ThreadPoolScheduler
def intense_calculation(value):
# sleep for a random short duration between 0.5 to 2.0 seconds to simulate a long-running calculation
time.sleep(random.randint(5, 20) * .1)
return value
# calculate number of CPU's, then create a ThreadPoolScheduler with that number of threads
optimal_thread_count = multiprocessing.cpu_count()
pool_scheduler = ThreadPoolScheduler(optimal_thread_count)
# Create Process 1
Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon") \
.map(lambda s: intense_calculation(s)) \
.subscribe_on(pool_scheduler) \
.subscribe(on_next=lambda s: print("PROCESS 1: {0} {1}".format(current_thread().name, s)),
on_error=lambda e: print(e),
on_completed=lambda: print("PROCESS 1 done!"))
# Create Process 2
Observable.range(1, 10) \
.map(lambda s: intense_calculation(s)) \
.subscribe_on(pool_scheduler) \
.subscribe(on_next=lambda i: print("PROCESS 2: {0} {1}".format(current_thread().name, i)),
on_error=lambda e: print(e), on_completed=lambda: print("PROCESS 2 done!"))
# Create Process 3, which is infinite
Observable.interval(1000) \
.map(lambda i: i * 100) \
.observe_on(pool_scheduler) \
.map(lambda s: intense_calculation(s)) \
.subscribe(on_next=lambda i: print("PROCESS 3: {0} {1}".format(current_thread().name, i)),
on_error=lambda e: print(e))
input("Press any key to exit\n")
'''
Press any key to exit
PROCESS 1: ThreadPoolExecutor-1_0 Alpha
PROCESS 1: ThreadPoolExecutor-1_0 Beta
PROCESS 2: ThreadPoolExecutor-1_1 1
PROCESS 3: ThreadPoolExecutor-1_2 0
PROCESS 2: ThreadPoolExecutor-1_1 2
PROCESS 1: ThreadPoolExecutor-1_0 Gamma
PROCESS 3: ThreadPoolExecutor-1_2 100
PROCESS 2: ThreadPoolExecutor-1_1 3
PROCESS 1: ThreadPoolExecutor-1_0 Delta
PROCESS 3: ThreadPoolExecutor-1_4 200
PROCESS 1: ThreadPoolExecutor-1_0 Epsilon
PROCESS 1 done!
PROCESS 2: ThreadPoolExecutor-1_1 4
PROCESS 2: ThreadPoolExecutor-1_1 5
PROCESS 3: ThreadPoolExecutor-1_5 300
PROCESS 2: ThreadPoolExecutor-1_1 6
PROCESS 3: ThreadPoolExecutor-1_2 400
PROCESS 3: ThreadPoolExecutor-1_2 500
PROCESS 2: ThreadPoolExecutor-1_1 7
PROCESS 3: ThreadPoolExecutor-1_2 600
PROCESS 2: ThreadPoolExecutor-1_1 8
PROCESS 3: ThreadPoolExecutor-1_2 700
PROCESS 2: ThreadPoolExecutor-1_1 9
PROCESS 3: ThreadPoolExecutor-1_2 800
PROCESS 2: ThreadPoolExecutor-1_1 10
PROCESS 2 done!
PROCESS 3: ThreadPoolExecutor-1_2 900
PROCESS 3: ThreadPoolExecutor-1_2 1000
PROCESS 3: ThreadPoolExecutor-1_2 1100
PROCESS 3: ThreadPoolExecutor-1_2 1200
PROCESS 3: ThreadPoolExecutor-1_2 1300
PROCESS 3: ThreadPoolExecutor-1_2 1400
PROCESS 3: ThreadPoolExecutor-1_2 1500
PROCESS 3: ThreadPoolExecutor-1_2 1600
PROCESS 3: ThreadPoolExecutor-1_2 1700
PROCESS 3: ThreadPoolExecutor-1_2 1800
...
'''

Alignment

from rx import Observable, Observer

xs = Observable.of(1,2,3)
ys = Observable.of(4,5,6)
zs = xs + ys # Concatenate observables
zs.to_list().subscribe(lambda value: print(value)) xs = Observable.of(1,2,3)
ys = xs * 4
ys.to_list().subscribe(lambda value: print(value)) xs = Observable.of(1,2,3)
ys = xs[1:-1]
ys.to_list().subscribe(lambda value: print(value)) xs = Observable.of(1,2,3,4,5,6)
ys = xs.to_blocking()
zs = (x*x for x in ys if x > 3)
for x in zs:
print(x)
'''
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
[2]
16
25
36
'''

ReactiveX 学习笔记(16)RxPY的更多相关文章

  1. ReactiveX 学习笔记(0)学习资源

    ReactiveX 学习笔记 ReactiveX 学习笔记(1) ReactiveX 学习笔记(2)创建数据流 ReactiveX 学习笔记(3)转换数据流 ReactiveX 学习笔记(4)过滤数据 ...

  2. Ext.Net学习笔记16:Ext.Net GridPanel 折叠/展开行

    Ext.Net学习笔记16:Ext.Net GridPanel 折叠/展开行 Ext.Net GridPanel的行支持折叠/展开功能,这个功能个人觉得还说很有用处的,尤其是数据中包含图片等内容的时候 ...

  3. SQL反模式学习笔记16 使用随机数排序

    目标:随机排序,使用高效的SQL语句查询获取随机数据样本. 反模式:使用RAND()随机函数 SELECT * FROM Employees AS e ORDER BY RAND() Limit 1 ...

  4. golang学习笔记16 beego orm 数据库操作

    golang学习笔记16 beego orm 数据库操作 beego ORM 是一个强大的 Go 语言 ORM 框架.她的灵感主要来自 Django ORM 和 SQLAlchemy. 目前该框架仍处 ...

  5. [原创]java WEB学习笔记16:JSP指令(page,include),JSP标签(forwar,include,param)

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  6. Adaptive AUTOSAR 学习笔记 16 - 时间同步和网络管理

    本系列学习笔记基于 AUTOSAR Adaptive Platform 官方文档 R20-11 版本 AUTOSAR_EXP_PlatformDesign.pdf.作者:Zijian/TENG 原文地 ...

  7. ArcGIS API for JavaScript 4.2学习笔记[16] 弹窗自定义功能按钮及为要素自定义按钮(第五章完结)

    这节对Popups这一章的最后两个例子进行介绍和解析. 第一个[Popup Actions]介绍了弹窗中如何自定义工具按钮(名为actions),以PopupTemplate+FeatureLayer ...

  8. ReactiveX 学习笔记(20)使用 RxJava + RxBinding 进行 GUI 编程

    课题 程序界面由3个文本编辑框和1个文本标签组成. 要求文本标签实时显示3个文本编辑框所输入的数字之和. 文本编辑框输入的不是合法数字时,将其值视为0. 3个文本编辑框的初值分别为1,2,3. 创建工 ...

  9. ReactiveX 学习笔记(14)使用 RxJava2 + Retrofit2 调用 REST API

    JSON : Placeholder JSON : Placeholder (https://jsonplaceholder.typicode.com/) 是一个用于测试的 REST API 网站. ...

随机推荐

  1. [UE4]让机器人开枪射击

  2. [UE4]UMG编辑器:控件作为变量、预设锚点和自由锚点

  3. Linux聚合网络

    Linux聚合网络 作者:Eric 微信:loveoracle11g 链路聚合 [root@server1 ~]# ifconfig | grep eno [root@server1 ~]# ls / ...

  4. FIN omitted, FIN-ACK sent

    STACKOVER ADDRESS:https://stackoverflow.com/questions/21390479/fin-omitted-fin-ack-sent question: As ...

  5. tomcat Error:NB:JAVA_HOME should point to a JDK not a JRE 解决方法

    环境:win7 tomcata7.0解压版本 执行:service.bat install 报错:JAVA_HOME should point to a JDK not a JRE 网上找了几种解决方 ...

  6. 一个windows计划任务的Nginx日志自动截断的批处理命令

    net stop nginx taskkill /im nginx.exe /f cd E:\nginx e: set NO=%Date:~0,4%%Date:~5,2%%Date:~8,2% set ...

  7. Keras之inception_v3使用

    一.安装 必要:tensorflow,Keras 首次运行需要安装: 1)下载模型权重   inception_v3_weights_tf_dim_ordering_tf_kernels.h5 路径见 ...

  8. spark 2.0.0集群安装与hive on spark配置

    1. 环境准备: JDK1.8 hive 2.3.4 hadoop 2.7.3 hbase 1.3.3 scala 2.11.12 mysql5.7 2. 下载spark2.0.0 cd /home/ ...

  9. MapReduce高级编程2

    MapReduce Top N .二次排序,MapJoin: TOP N 对于一组输入List(key,value),我们要创建一个Top N 列表,这是一种过滤模式,查看输入数据特定子集,观察用户的 ...

  10. Json2:使用gson解析为List和Map

    import java.lang.reflect.Type; import java.util.List; import com.google.gson.Gson; import com.google ...