本文整理汇总了Python中rx.core.Observable.subscribe_方法的典型用法代码示例。如果您正苦于以下问题:Python Observable.subscribe_方法的具体用法?Python Observable.subscribe_怎么用?Python Observable.subscribe_使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rx.core.Observable
的用法示例。
在下文中一共展示了Observable.subscribe_方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: to_future
# 需要导入模块: from rx.core import Observable [as 别名]
# 或者: from rx.core.Observable import subscribe_ [as 别名]
def to_future(source: Observable) -> Future:
"""Converts an existing observable sequence to a Future.
Example:
future = rx.return_value(42).pipe(ops.to_future(asyncio.Future))
Args:
future_ctor: [Optional] The constructor of the future.
Returns:
A future with the last value from the observable sequence.
"""
has_value = []
def on_next(value):
has_value.append(value)
def on_error(err):
future.set_exception(err)
def on_completed():
if has_value:
future.set_result(has_value.pop())
source.subscribe_(on_next, on_error, on_completed)
# No cancellation can be done
return future
示例2: on_next
# 需要导入模块: from rx.core import Observable [as 别名]
# 或者: from rx.core.Observable import subscribe_ [as 别名]
def on_next(inner_source: Observable):
nonlocal source
d = SingleAssignmentDisposable()
with source.lock:
latest[0] += 1
_id = latest[0]
has_latest[0] = True
inner_subscription.disposable = d
# Check if Future or Observable
inner_source = from_future(inner_source) if is_future(inner_source) else inner_source
def on_next(x: Any) -> None:
if latest[0] == _id:
observer.on_next(x)
def on_error(e: Exception) -> None:
if latest[0] == _id:
observer.on_error(e)
def on_completed() -> None:
if latest[0] == _id:
has_latest[0] = False
if is_stopped[0]:
observer.on_completed()
d.disposable = inner_source.subscribe_(on_next, on_error, on_completed, scheduler=scheduler)
示例3: to_future
# 需要导入模块: from rx.core import Observable [as 别名]
# 或者: from rx.core.Observable import subscribe_ [as 别名]
def to_future(source: Observable) -> Future:
"""Converts an existing observable sequence to a Future.
If the observable emits a single item, then this item is set as the
result of the future. If the observable emits a sequence of items, then
the last emitted item is set as the result of the future.
Example:
future = rx.return_value(42).pipe(ops.to_future(asyncio.Future))
Args:
future_ctor: [Optional] The constructor of the future.
Returns:
A future with the last value from the observable sequence.
"""
has_value = False
last_value = None
def on_next(value):
nonlocal last_value
nonlocal has_value
last_value = value
has_value = True
def on_error(err):
future.set_exception(err)
def on_completed():
if has_value:
future.set_result(last_value)
else:
future.set_exception(SequenceContainsNoElementsError())
source.subscribe_(on_next, on_error, on_completed)
# No cancellation can be done
return future
示例4: set_timer
# 需要导入模块: from rx.core import Observable [as 别名]
# 或者: from rx.core.Observable import subscribe_ [as 别名]
def set_timer(timeout: Observable) -> None:
my_id = _id[0]
def timer_wins():
return _id[0] == my_id
d = SingleAssignmentDisposable()
timer.disposable = d
def on_next(x):
if timer_wins():
subscription.disposable = other.subscribe(observer, scheduler=scheduler)
d.dispose()
def on_error(e):
if timer_wins():
observer.on_error(e)
def on_completed():
if timer_wins():
subscription.disposable = other.subscribe(observer)
d.disposable = timeout.subscribe_(on_next, on_error, on_completed, scheduler)