RxPy - 示例

  • 简述

    在本章中,我们将详细讨论以下主题 -
    • 基本示例展示了 observable、operators 和订阅观察者的工作。
    • 可观察对象和对象之间的区别。
    • 了解冷和热的可观察对象。
    下面给出了一个基本示例,展示了 observable、操作符和订阅观察者的工作。

    例子

    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!
    
    
  • 可观察对象和对象之间的区别

    在这个例子中,我们将看到 observable 和 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)))
    
    

    输出

    
    
    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
    
    
    在上面的例子中,每次你订阅 observable 时,它​​都会给你新的值。

    主题示例

    
    
    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
    
    
    如果您看到使用该主题的两个订阅者之间共享值。
  • 了解冷和热 Observables

    Observable 被分类为
    • 冷观测
    • 热观察
    当多个订阅者订阅时,会注意到 observables 的差异。

    冷观测

    Cold observables,是被执行的可观察对象,并在每次订阅时呈现数据。当它被订阅时,observable 被执行并给出新的值。
    下面的例子给出了对cold observable的理解。
    
    
    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
    
    
    在上面的示例中,每次订阅 observable 时,它​​都会执行 observable 并发出值。如上面的示例所示,这些值也可能因订阅者而异。

    热点观察

    在 hot observable 的情况下,它们会在准备好时发出值,并且不会总是等待订阅。发出值时,所有订阅者将获得相同的值。
    当您希望在可观察对象准备好时发出值,或者您希望与所有订阅者共享相同的值时,您可以使用热可观察对象。
    热可观察的一个例子是主题和可连接运算符。
    
    
    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 () 可连接的 observable 运算符来实现相同的目的。