RxPy – 例子
在本章中,我们将详细讨论以下主题−
- 基本的例子显示了观察者、操作者和订阅观察者的工作。
- 观察者和主体之间的区别。
- 理解冷观察变量和热观察变量。
下面是一个基本的例子,展示了观察者、操作者和订阅观察者的工作。
例子
test.py
import requests
import rx
import json
from rx import operators as ops
def filternames(x):
if (x["name"].startswith("C")):
return x["name"]
else :
return ""
content = requests.get('https://jsonplaceholder.typicode.com/users')
y = json.loads(content.text)
source = rx.from_(y)
case1 = source.pipe(
ops.filter(lambda c: filternames(c)),
ops.map(lambda a:a["name"])
)
case1.subscribe(
on_next = lambda i: print("Got - {0}".format(i)), 8. RxPy — Examples
on_error = lambda e: print("Error : {0}".format(e)),
on_completed = lambda: print("Job Done!"),
)
这里,是一个非常简单的例子,我从这个URL中获取用户数据 –
https://jsonplaceholder.typicode.com/users。
对数据进行过滤,给出以 “C “开头的名字,然后使用地图只返回这些名字。以下是相同的输出 –
E:\pyrx\examples>python test.py
Got - Clementine Bauch
Got - Chelsey Dietrich
Got - Clementina DuBuque
Job Done!
可观察对象和主体的区别
在这个例子中,我们将看到可观察者和主体之间的区别。
from rx import of, operators as op
import random
test1 = of(1,2,3,4,5)
sub1 = test1.pipe(
op.map(lambda a : a+random.random())
)
print("From first subscriber")
subscriber1 = sub1.subscribe(lambda i: print("From sub1 {0}".format(i)))
print("From second subscriber")
subscriber2 = sub1.subscribe(lambda i: print("From sub2 {0}".format(i)))
输出
E:\pyrx>python testrx.py
From first subscriber
From sub1 1.610450821095726
From sub1 2.9567564032037335
From sub1 3.933217537811936
From sub1 4.82444905626622
From sub1 5.929414892567188
From second subscriber
From sub2 1.8573813517529874
From sub2 2.902433239469483
From sub2 3.2289868093016825
From sub2 4.050413890694411
From sub2 5.226515068012821
在上面的例子中,每当你订阅观察者时,它都会给你新的值。
主题实例
from rx import of, operators as op
import random
from rx.subject import Subject
subject_test = Subject()
subject_test.subscribe(
lambda x: print("From sub1 {0}".format(x))
)
subject_test.subscribe(
lambda x: print("From sub2 {0}".format(x))
)
test1 = of(1,2,3,4,5)
sub1 = test1.pipe(
op.map(lambda a : a+random.random())
)
subscriber = sub1.subscribe(subject_test)
输出
E:\pyrx>python testrx.py
From sub1 1.1789422863284509
From sub2 1.1789422863284509
From sub1 2.5525627903260153
From sub2 2.5525627903260153
From sub1 3.4191549324778325
From sub2 3.4191549324778325
From sub1 4.644042420199624
From sub2 4.644042420199624
From sub1 5.079896897489065
From sub2 5.079896897489065
如果你看到值是共享的,在两个订阅者之间使用主题。
理解冷和热的可观察变量
一个可观察变量被划分为
- 冷观察者
- 热观察物
当多个订阅者在订阅时,会注意到观察变量的不同。
冷观察器
冷观察变量是被执行的观察变量,每次被订阅时都会呈现数据。当它被订阅时,观测器被执行并给出新的值。
下面的例子给出了对冷观察项的理解。
from rx import of, operators as op
import random
test1 = of(1,2,3,4,5)
sub1 = test1.pipe(
op.map(lambda a : a+random.random())
)
print("From first subscriber")
subscriber1 = sub1.subscribe(lambda i: print("From sub1 {0}".format(i)))
print("From second subscriber")
subscriber2 = sub1.subscribe(lambda i: print("From sub2 {0}".format(i)))
输出
E:\pyrx>python testrx.py
From first subscriber
From sub1 1.610450821095726
From sub1 2.9567564032037335
From sub1 3.933217537811936
From sub1 4.82444905626622
From sub1 5.929414892567188
From second subscriber
From sub2 1.8573813517529874
From sub2 2.902433239469483
From sub2 3.2289868093016825
From sub2 4.050413890694411
From sub2 5.226515068012821
在上面的例子中,每次订阅观测器,它都会执行观测器并发射出数值。如上例所示,这些值也可以因订阅者不同而不同。
热观察变量
在热观察器的情况下,它们会在准备好的时候发射出数值,而不是一直等待订阅。当值被发射出来时,所有的订阅者将得到相同的值。
当你想在观测器准备好时发射值,或者你想向所有订阅者分享相同的值时,你可以使用热观测器。
热观察的一个例子是Subject和connectable操作符。
from rx import of, operators as op
import random
from rx.subject import Subject
subject_test = Subject()
subject_test.subscribe(
lambda x: print("From sub1 {0}".format(x))
)
subject_test.subscribe(
lambda x: print("From sub2 {0}".format(x))
)
test1 = of(1,2,3,4,5)
sub1 = test1.pipe(
op.map(lambda a : a+random.random())
)
subscriber = sub1.subscribe(subject_test)
输出
E:\pyrx>python testrx.py
From sub1 1.1789422863284509
From sub2 1.1789422863284509
From sub1 2.5525627903260153
From sub2 2.5525627903260153
From sub1 3.4191549324778325
From sub2 3.4191549324778325
From sub1 4.644042420199624
From sub2 4.644042420199624
From sub1 5.079896897489065
From sub2 5.079896897489065
如果你看到,相同的值在订阅者之间共享。你可以使用publish () connectable observable操作符实现同样的效果。