本文整理汇总了Python中rx.disposable.CompositeDisposable.add方法的典型用法代码示例。如果您正苦于以下问题:Python CompositeDisposable.add方法的具体用法?Python CompositeDisposable.add怎么用?Python CompositeDisposable.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rx.disposable.CompositeDisposable
的用法示例。
在下文中一共展示了CompositeDisposable.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: subscribe
# 需要导入模块: from rx.disposable import CompositeDisposable [as 别名]
# 或者: from rx.disposable.CompositeDisposable import add [as 别名]
def subscribe(observer, scheduler=None):
is_open = [False]
def on_next(left):
if is_open[0]:
observer.on_next(left)
def on_completed():
if is_open[0]:
observer.on_completed()
subs = source.subscribe_(on_next, observer.on_error, on_completed, scheduler)
subscriptions = CompositeDisposable(subs)
right_subscription = SingleAssignmentDisposable()
subscriptions.add(right_subscription)
def on_next2(x):
is_open[0] = True
right_subscription.dispose()
def on_completed2():
right_subscription.dispose()
right_subscription.disposable = other.subscribe_(on_next2, observer.on_error, on_completed2, scheduler)
return subscriptions
示例2: subscribe
# 需要导入模块: from rx.disposable import CompositeDisposable [as 别名]
# 或者: from rx.disposable.CompositeDisposable import add [as 别名]
def subscribe(observer, scheduler_=None):
_scheduler = scheduler or scheduler_ or timeout_scheduler
n = [0]
s = [None]
timer_d = SerialDisposable()
window_id = [0]
group_disposable = CompositeDisposable(timer_d)
ref_count_disposable = RefCountDisposable(group_disposable)
def create_timer(_id):
m = SingleAssignmentDisposable()
timer_d.disposable = m
def action(scheduler, state):
if _id != window_id[0]:
return
n[0] = 0
window_id[0] += 1
new_id = window_id[0]
s[0].on_completed()
s[0] = Subject()
observer.on_next(add_ref(s[0], ref_count_disposable))
create_timer(new_id)
m.disposable = _scheduler.schedule_relative(timespan, action)
s[0] = Subject()
observer.on_next(add_ref(s[0], ref_count_disposable))
create_timer(0)
def on_next(x):
new_window = False
new_id = 0
s[0].on_next(x)
n[0] += 1
if n[0] == count:
new_window = True
n[0] = 0
window_id[0] += 1
new_id = window_id[0]
s[0].on_completed()
s[0] = Subject()
observer.on_next(add_ref(s[0], ref_count_disposable))
if new_window:
create_timer(new_id)
def on_error(e):
s[0].on_error(e)
observer.on_error(e)
def on_completed():
s[0].on_completed()
observer.on_completed()
group_disposable.add(source.subscribe_(on_next, on_error, on_completed, scheduler_))
return ref_count_disposable
示例3: subscribe
# 需要导入模块: from rx.disposable import CompositeDisposable [as 别名]
# 或者: from rx.disposable.CompositeDisposable import add [as 别名]
def subscribe(observer, scheduler=None):
window = [Subject()]
d = CompositeDisposable()
r = RefCountDisposable(d)
observer.on_next(add_ref(window[0], r))
def on_next_window(x):
window[0].on_next(x)
def on_error(err):
window[0].on_error(err)
observer.on_error(err)
def on_completed():
window[0].on_completed()
observer.on_completed()
d.add(source.subscribe_(on_next_window, on_error, on_completed, scheduler))
def on_next_observer(w):
window[0].on_completed()
window[0] = Subject()
observer.on_next(add_ref(window[0], r))
d.add(window_boundaries.subscribe_(on_next_observer, on_error, on_completed, scheduler))
return r
示例4: subscribe
# 需要导入模块: from rx.disposable import CompositeDisposable [as 别名]
# 或者: from rx.disposable.CompositeDisposable import add [as 别名]
def subscribe(observer, scheduler=None):
was_invoked = [False]
def on_completed():
observer.on_completed()
try:
if not was_invoked[0]:
finally_action()
was_invoked[0] = True
except Exception as err: # pylint: disable=broad-except
observer.on_error(err)
def on_error(exception):
observer.on_error(exception)
try:
if not was_invoked[0]:
finally_action()
was_invoked[0] = True
except Exception as err: # pylint: disable=broad-except
observer.on_error(err)
composite_disposable = CompositeDisposable()
composite_disposable.add(OnDispose(was_invoked))
subscription = source.subscribe_(observer.on_next, on_error, on_completed, scheduler)
composite_disposable.add(subscription)
return composite_disposable
示例5: subscribe
# 需要导入模块: from rx.disposable import CompositeDisposable [as 别名]
# 或者: from rx.disposable.CompositeDisposable import add [as 别名]
def subscribe(observer, scheduler=None):
group = CompositeDisposable()
is_stopped = [False]
m = SingleAssignmentDisposable()
group.add(m)
def on_next(inner_source):
inner_subscription = SingleAssignmentDisposable()
group.add(inner_subscription)
inner_source = from_future(inner_source) if is_future(inner_source) else inner_source
@synchronized(source.lock)
def on_completed():
group.remove(inner_subscription)
if is_stopped[0] and len(group) == 1:
observer.on_completed()
on_next = synchronized(source.lock)(observer.on_next)
on_error = synchronized(source.lock)(observer.on_error)
subscription = inner_source.subscribe_(on_next, on_error, on_completed, scheduler)
inner_subscription.disposable = subscription
def on_completed():
is_stopped[0] = True
if len(group) == 1:
observer.on_completed()
m.disposable = source.subscribe_(on_next, observer.on_error, on_completed, scheduler)
return group
示例6: run
# 需要导入模块: from rx.disposable import CompositeDisposable [as 别名]
# 或者: from rx.disposable.CompositeDisposable import add [as 别名]
def run(self):
srcs = list(self.parent.sources)
N = len(srcs)
self.queues = [None] * N
self.isDone = [False] * N
self.subscriptions = [None] * N
self.gate = RLock()
for i in range(0, N):
self.queues[i] = deque()
# Loop twice because subscribing could already yield
# a value before all queues are initialized
for i in range(0, N):
d = SingleAssignmentDisposable()
self.subscriptions[i] = d
o = self.O(self, i)
d.disposable = srcs[i].subscribeSafe(o)
c = CompositeDisposable(self.subscriptions)
def dispose():
for q in self.queues:
q.clear()
c.add(Disposable.create(dispose))
return c
示例7: test_groupdisposable_clear
# 需要导入模块: from rx.disposable import CompositeDisposable [as 别名]
# 或者: from rx.disposable.CompositeDisposable import add [as 别名]
def test_groupdisposable_clear():
disp1 = [False]
disp2 = [False]
def action1():
disp1[0] = True
d1 = Disposable(action1)
def action2():
disp2[0] = True
d2 = Disposable(action2)
g = CompositeDisposable(d1, d2)
assert g.length == 2
g.clear()
assert disp1[0]
assert disp2[0]
assert not g.length
disp3 = [False]
def action3():
disp3[0] = True
d3 = Disposable(action3)
g.add(d3);
assert not disp3[0]
assert g.length == 1
示例8: _subscribe_core
# 需要导入模块: from rx.disposable import CompositeDisposable [as 别名]
# 或者: from rx.disposable.CompositeDisposable import add [as 别名]
def _subscribe_core(self, observer=None, scheduler=None) -> typing.Disposable:
clock = self.scheduler.to_seconds(self.scheduler.now)
self.subscriptions.append(Subscription(clock))
index = len(self.subscriptions) - 1
disp = CompositeDisposable()
def get_action(notification):
def action(scheduler, state):
notification.accept(observer)
return Disposable()
return action
for message in self.messages:
notification = message.value
# Don't make closures within a loop
action = get_action(notification)
disp.add(self.scheduler.schedule_relative(message.time, action))
def dispose() -> None:
start = self.subscriptions[index].subscribe
end = self.scheduler.to_seconds(self.scheduler.now)
self.subscriptions[index] = Subscription(start, end)
disp.dispose()
return Disposable(dispose)
示例9: test_groupdisposable_add
# 需要导入模块: from rx.disposable import CompositeDisposable [as 别名]
# 或者: from rx.disposable.CompositeDisposable import add [as 别名]
def test_groupdisposable_add():
d1 = Disposable()
d2 = Disposable()
g = CompositeDisposable(d1)
assert g.length == 1
assert g.contains(d1)
g.add(d2)
assert g.length == 2
assert g.contains(d2)
示例10: run
# 需要导入模块: from rx.disposable import CompositeDisposable [as 别名]
# 或者: from rx.disposable.CompositeDisposable import add [as 别名]
def run(self):
self.gate = RLock()
groupDisposable = CompositeDisposable()
self.refCountDisposable = RefCountDisposable(groupDisposable)
self.createWindow()
groupDisposable.add(self.parent.scheduler.schedulePeriodic(self.parent.timeSpan, self.tick))
groupDisposable.add(self.parent.source.subscribeSafe(self))
return self.refCountDisposable
示例11: RecursiveScheduledFunction
# 需要导入模块: from rx.disposable import CompositeDisposable [as 别名]
# 或者: from rx.disposable.CompositeDisposable import add [as 别名]
class RecursiveScheduledFunction(object):
def __init__(self, action, scheduler, method = None):
self.action = action
self.group = CompositeDisposable()
self.lock = RLock()
if method == None:
self.schedule = scheduler.scheduleWithState
else:
self.schedule = getattr(scheduler, method)
def run(self, state):
self.action(state, self.actionCallback)
def actionCallback(self, newState, dueTime = None):
self.isDone = False
self.isAdded = False
if dueTime == None:
self.cancel = self.schedule(
newState,
self.schedulerCallback
)
else:
self.cancel = self.schedule(
newState,
dueTime,
self.schedulerCallback
)
with self.lock:
if not self.isDone:
self.group.add(self.cancel)
self.isAdded = True
def schedulerCallback(self, scheduler, state):
with self.lock:
if self.isAdded:
self.group.remove(self.cancel)
else:
self.isDone = True
self.run(state)
return Disposable.empty()
示例12: test_groupdisposable_addafterdispose
# 需要导入模块: from rx.disposable import CompositeDisposable [as 别名]
# 或者: from rx.disposable.CompositeDisposable import add [as 别名]
def test_groupdisposable_addafterdispose():
disp1 = [False]
disp2 = [False]
def action1():
disp1[0] = True
d1 = Disposable(action1)
def action2():
disp2[0] = True
d2 = Disposable(action2)
g = CompositeDisposable(d1)
assert g.length == 1
g.dispose()
assert disp1[0]
assert g.length == 0
g.add(d2)
assert disp2[0]
assert g.length == 0
示例13: subscribe
# 需要导入模块: from rx.disposable import CompositeDisposable [as 别名]
# 或者: from rx.disposable.CompositeDisposable import add [as 别名]
def subscribe(observer, scheduler=None):
has_current = [False]
is_stopped = [False]
m = SingleAssignmentDisposable()
g = CompositeDisposable()
g.add(m)
def on_next(inner_source):
if not has_current[0]:
has_current[0] = True
inner_source = rx.from_future(inner_source) if is_future(inner_source) else inner_source
inner_subscription = SingleAssignmentDisposable()
g.add(inner_subscription)
def on_completed_inner():
g.remove(inner_subscription)
has_current[0] = False
if is_stopped[0] and len(g) == 1:
observer.on_completed()
inner_subscription.disposable = inner_source.subscribe_(
observer.on_next,
observer.on_error,
on_completed_inner,
scheduler
)
def on_completed():
is_stopped[0] = True
if not has_current[0] and len(g) == 1:
observer.on_completed()
m.disposable = source.subscribe_(on_next, observer.on_error, on_completed, scheduler)
return g
示例14: subscribe
# 需要导入模块: from rx.disposable import CompositeDisposable [as 别名]
# 或者: from rx.disposable.CompositeDisposable import add [as 别名]
def subscribe(observer, scheduler=None):
group = CompositeDisposable()
rcd = RefCountDisposable(group)
left_map = OrderedDict()
right_map = OrderedDict()
left_id = [0]
right_id = [0]
def on_next_left(value):
subject = Subject()
with left.lock:
_id = left_id[0]
left_id[0] += 1
left_map[_id] = subject
try:
result = (value, add_ref(subject, rcd))
except Exception as e:
log.error("*** Exception: %s" % e)
for left_value in left_map.values():
left_value.on_error(e)
observer.on_error(e)
return
observer.on_next(result)
for right_value in right_map.values():
subject.on_next(right_value)
md = SingleAssignmentDisposable()
group.add(md)
def expire():
if _id in left_map:
del left_map[_id]
subject.on_completed()
group.remove(md)
try:
duration = left_duration_mapper(value)
except Exception as e:
for left_value in left_map.values():
left_value.on_error(e)
observer.on_error(e)
return
def on_error(error):
for left_value in left_map.values():
left_value.on_error(error)
observer.on_error(error)
md.disposable = duration.pipe(ops.take(1)).subscribe_(nothing, on_error, expire, scheduler)
def on_error_left(error):
for left_value in left_map.values():
left_value.on_error(error)
observer.on_error(error)
group.add(left.subscribe_(on_next_left, on_error_left, observer.on_completed, scheduler))
def send_right(value):
with left.lock:
_id = right_id[0]
right_id[0] += 1
right_map[_id] = value
md = SingleAssignmentDisposable()
group.add(md)
def expire():
del right_map[_id]
group.remove(md)
try:
duration = right_duration_mapper(value)
except Exception as e:
for left_value in left_map.values():
left_value.on_error(e)
observer.on_error(e)
return
def on_error(error):
with left.lock:
for left_value in left_map.values():
left_value.on_error(error)
observer.on_error(error)
md.disposable = duration.pipe(ops.take(1)).subscribe_(nothing, on_error, expire, scheduler)
with left.lock:
for left_value in left_map.values():
left_value.on_next(value)
#.........这里部分代码省略.........
示例15: Sink
# 需要导入模块: from rx.disposable import CompositeDisposable [as 别名]
# 或者: from rx.disposable.CompositeDisposable import add [as 别名]
class Sink(rx.linq.sink.Sink):
def __init__(self, parent, observer, cancel):
super(SelectMany.Sink, self).__init__(observer, cancel)
self.parent = parent
self.index = -1
def run(self):
self.gate = RLock()
self.isStopped = False
self.group = CompositeDisposable()
self.sourceSubscription = SingleAssignmentDisposable()
self.group.add(self.sourceSubscription)
self.sourceSubscription.disposable = self.parent.source.subscribeSafe(self)
return self.group
def onNext(self, value):
inner = None
try:
if self.parent.withIndex:
self.index += 1
inner = self.parent.selectorOnNext(value, self.index)
else:
inner = self.parent.selectorOnNext(value)
except Exception as e:
with self.gate:
self.observer.onError(e)
self.dispose()
if isinstance(inner, Observable):
self.subscribeInner(inner)
return
# iterable
try:
for current in inner:
self.observer.onNext(current)
except Exception as e:
self.observer.onError(e)
self.dispose()
def onError(self, exception):
if self.parent.selectorOnError != None:
try:
inner = None
if self.parent.withIndex:
self.index += 1
inner = self.parent.selectorOnError(exception, self.index)
else:
inner = self.parent.selectorOnError(exception)
except Exception as e:
with self.gate:
self.observer.onError(e)
self.dispose()
else:
self.subscribeInner(inner)
self.final()
else:
with self.gate:
self.observer.onError(exception)
self.dispose()
def onCompleted(self):
if self.parent.selectorOnCompleted != None:
try:
inner = None
if self.parent.withIndex:
inner = self.parent.selectorOnCompleted(self.index)
else:
inner = self.parent.selectorOnCompleted()
except Exception as e:
with self.gate:
self.observer.onError(e)
self.dispose()
return
else:
self.subscribeInner(inner)
self.final()
def final(self):
self.isStopped = True
if self.group.length == 1:
#
# Notice there can be a race between OnCompleted of the source and any
# of the inner sequences, where both see _group.Count == 1, and one is
# waiting for the lock. There won't be a double OnCompleted observation
# though, because the call to Dispose silences the observer by swapping
# in a NopObserver<T>.
#
with self.gate:
self.observer.onCompleted()
self.dispose()
else:
self.sourceSubscription.dispose()
#.........这里部分代码省略.........