我有一个错误,它不小心将 anObservable
用作可迭代对象。对于大多数对象,这通常很容易检测到:
>>> tuple(object())
Traceback (most recent call last):
File "C:\Program Files (x86)\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 3035, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-4-40e3dfc60da8>", line 1, in <module>
tuple(object())
TypeError: 'object' object is not iterable
然而,对于一个 Rx observable,它会默默地使 Python 崩溃:
MWE:
from rx import Observable
observable = Observable.from_list([1,2,3])
tuple(observable) # Python will die silently here
没有回溯,也没有迹象表明有任何问题。这使得已经很难调试的并发响应式代码更难调试——我花了 2 个小时才最终找到这个。
仔细观察,迭代 anObservable
似乎会创建新的 observables,尽管我不知道从哪里可以观察到 observable 没有__iter__
方法。
>>> for i, x in enumerate(observable):
>>> print x
>>> if i > 100: # To prevent Python from crashing
>>> break
<rx.anonymousobservable.AnonymousObservable object at 0x03111710>
<rx.anonymousobservable.AnonymousObservable object at 0x03111850>
<rx.anonymousobservable.AnonymousObservable object at 0x03111990>
<rx.anonymousobservable.AnonymousObservable object at 0x03111AD0>
<rx.anonymousobservable.AnonymousObservable object at 0x03111C10>
<rx.anonymousobservable.AnonymousObservable object at 0x03111D50>
<rx.anonymousobservable.AnonymousObservable object at 0x03111E90>
etc...
这是一个错误,还是一个功能?Observable
s是否可以迭代?