我将在Android应用中使用rxandroid。我正试图在rxpy中对此行为进行建模,因为这对我来说是最容易设置和使用的。在下面的示例中,source3正在发出正确的数据;这是一个需要一些时间的初始化和我刚刚伪造的永久订阅的串联。我想要BehaviorSubject,因为我需要立即进行字段初始化的最后一个值。
我无法弄清楚如何将BehaviorSubject链接到source3之上,以便在记住最后一个值时发出源3。我已经在互联网上搜索了两天,并没有找到关于这个用例的明确方向。这是我的代码,问题是为什么我没有得到观察者的任何排放。
from rx import Observable, Observer
from rx.subjects import BehaviorSubject
import time, random
def fake_initialization(observer):
time.sleep(5) # It takes some time
observer.on_next("Alpha")
observer.on_completed()
def fake_subscription(observer):
iter = 0 # Subscription emits forever
while True:
observer.on_next("message %02d"%(iter))
time.sleep(random.randrange(2,5))
iter += 1
class PrintObserver(Observer):
def on_next(self, value):
print("Received {0}".format(value))
#bsubject.on_next(value)
def on_completed(self):
print("Done!")
def on_error(self, error):
print("Error Occurred: {0}".format(error))
source1 = Observable.create(fake_initialization)
source2 = Observable.create(fake_subscription)
source3 = source1 + source2
bsubject = BehaviorSubject(False)
source4 = source3.multicast(bsubject)
source4.connect()
source4.subscribe(PrintObserver())
答案 0 :(得分:0)
这对某人来说实际上是一个相当简单的答案。我发布这个以防万一其他人在这种情况下结束。不可否认,我没有仔细阅读rxpy页面。您需要自己添加并发,大概是因为Python中有很多并发解决方案。这是最终的工作代码:
import random
import time
import multiprocessing
from rx import Observable,Observer
from rx.concurrency import ThreadPoolScheduler
from rx.subjects import Subject
class PrintObserver1(Observer):
def on_next(self, value):
print("Received 1 {0}".format(value))
#bsubject.on_next(value)
def on_completed(self):
print("Done 1!")
def on_error(self, error):
print("Error Occurred: 1 {0}".format(error))
class PrintObserver2(Observer):
def on_next(self, value):
print("Received 2 {0}".format(value))
#bsubject.on_next(value)
def on_completed(self):
print("Done 2!")
def on_error(self, error):
print("Error Occurred: 2 {0}".format(error))
def fake_initialization(observer):
time.sleep(5) # It takes some time
observer.on_next("Alpha")
observer.on_completed()
def fake_subscription(observer):
iter = 0 # Subscription emits forever
while True:
observer.on_next("message %02d"%(iter))
time.sleep(random.randrange(2,5))
iter += 1
optimal_thread_count = multiprocessing.cpu_count()
pool_scheduler = ThreadPoolScheduler(optimal_thread_count)
source1 = Observable.create(fake_initialization).subscribe_on(pool_scheduler)
source2 = Observable.create(fake_subscription).subscribe_on(pool_scheduler)
catted_source = source1 + source2
native_source = Observable.interval(1000)
print native_source,catted_source
#source = source3
subject = Subject()
# native_source = works
# catted_source = not works
subSource = catted_source.subscribe(subject)
#####
subSubject1 = subject.subscribe(PrintObserver1())
subSubject2 = subject.subscribe(PrintObserver2())
time.sleep(30)
subject.on_completed()
subSubject1.dispose()
subSubject2.dispose()
另请注意,您必须安装'期货'并发包用于Python 2.7。
如果您收到此错误:
from concurrent.futures import ThreadPoolExecutor
ImportError: No module named concurrent.futures
阅读本文(链接是针对略有不同的错误,但解决方案有效):