RxPy将值传递给观察者

时间:2017-07-14 07:17:01

标签: python rx-py

有没有办法根据用户输入将值传递给观察者(这意味着传递的值不是一直都是固定的)?

from rx import Observable, Observer

def push_five_strings(observer,value):
        observer.on_next(value)
        #observer.on_next("Alpha")
        observer.on_completed()


class PrintObserver(Observer):

    def on_next(self, value):
        print("Received {0}".format(value))

    def on_completed(self):
        print("Done!")

    def on_error(self, error):
        print("Error Occurred: {0}".format(error))

strings = [("Alpha", "Beta", "Gamma", "Delta", "Epsilon")]
for i in strings:
        push_five_strings(strings) #e.g. getting the values to push in one string at a time from a list of strings
#push_five_strings("Gamma")
#push_five_strings("Alpha")
#push_five_strings("Beta")
#push_five_strings("Delta")

source = Observable.create(push_five_strings)
#source = Observable.from_(["Alpha", "Beta", "Gamma", "Delta", "Epsilon"])
#source = Observable.from_([value])


source.subscribe(PrintObserver())

我试图去了解RxPy,但网上几乎没有任何例子...

1 个答案:

答案 0 :(得分:0)

    puts d.run(['1','1','0','a'])

启动并输入结果:

from rx import Observable, Observer                                                                                      
import sys                                                                                                               

class PrintObserver(Observer):                                                                                           

    def on_next(self, value):                                                                                            
        print("Received {0}".format(value))                                                                              

    def on_completed(self):                                                                                              
        print("Done!")                                                                                                   

    def on_error(self, error):                                                                                           
        print("Error Occurred: {0}".format(error))                                                                       

Observable.from_(sys.stdin).subscribe(PrintObserver())  

使用abc Received abc def Received def Done! 停止输入流。