Rxpy 简明教程
RxPy - Examples
在本章中,我们将详细讨论以下主题 −
-
基础示例展示了可观察、操作符的工作原理,以及订阅观察者。
-
可观察与主题之间的差异。
-
理解冷可观察和热可观察。
下面给出了一个基础示例,展示了可观察、操作符的工作原理,以及订阅观察者。
Example
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 获取用户数据
过滤数据,按“C”开头的名字进行过滤,然后使用 map 只返回名字。以下是相同数据的输出 −
E:\pyrx\examples>python test.py
Got - Clementine Bauch
Got - Chelsey Dietrich
Got - Clementina DuBuque
Job Done!
Difference between observable and subject
在这个示例中,我们将查看可观察与主题之间的差异。
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)))
Output
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 Example
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)
Output
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
如果你看到这些值是在两个使用主题的订阅者之间共享的。
Understanding Cold and Hot Observables
可观察被分类为
-
Cold Observables
-
Hot Observables
当多个订阅者都订阅的时候就会发现可观测的差异。
Cold Observables
冷可观测是指可执行且在每次订阅时都会呈现数据的可观测。当它被订阅时,可观测会被执行,并给出新值。
以下示例给出了对冷可观察的理解。
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)))
Output
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
在以上示例中,每次订阅可观察,它都会执行可观察并发出值。这些值也可以像在以上示例中展示的那样在不同的订阅者之间不同。
Hot Observables
在热可观察的情况下,当它们准备好时会发出值,而且不会总是等待订阅。当值被发出时,所有的订阅者都将得到相同的值。
当你希望在可观察准备好时发出值,或者希望向所有订阅者共享相同的值时,你可以利用热可观察。
热可观察的示例包括主题和可连接操作符。
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)
Output
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() 可连接可观察操作符实现相同的目的。