本文整理汇总了Python中rx.disposable.SingleAssignmentDisposable.dispose方法的典型用法代码示例。如果您正苦于以下问题:Python SingleAssignmentDisposable.dispose方法的具体用法?Python SingleAssignmentDisposable.dispose怎么用?Python SingleAssignmentDisposable.dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rx.disposable.SingleAssignmentDisposable
的用法示例。
在下文中一共展示了SingleAssignmentDisposable.dispose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ScheduledItem
# 需要导入模块: from rx.disposable import SingleAssignmentDisposable [as 别名]
# 或者: from rx.disposable.SingleAssignmentDisposable import dispose [as 别名]
class ScheduledItem(Generic[typing.TState]): # pylint: disable=unsubscriptable-object
def __init__(self, scheduler: SchedulerBase, state: Optional[typing.TState], action: typing.ScheduledAction,
duetime: typing.AbsoluteTime):
self.scheduler = scheduler
self.state = state
self.action = action
self.duetime = duetime
self.disposable = SingleAssignmentDisposable()
def invoke(self) -> None:
ret = self.scheduler.invoke_action(self.action, self.state)
self.disposable.disposable = ret
def cancel(self) -> None:
"""Cancels the work item by disposing the resource returned by
invoke_core as soon as possible."""
self.disposable.dispose()
def is_cancelled(self) -> bool:
return self.disposable.is_disposed
def __lt__(self, other):
return self.duetime < other.duetime
def __gt__(self, other):
return self.duetime > other.duetime
def __eq__(self, other):
return self.duetime == other.duetime
示例2: Sink
# 需要导入模块: from rx.disposable import SingleAssignmentDisposable [as 别名]
# 或者: from rx.disposable.SingleAssignmentDisposable import dispose [as 别名]
class Sink(rx.linq.sink.Sink):
def __init__(self, parent, observer, cancel):
super(SampleWithObservable.Sink, self).__init__(observer, cancel)
self.parent = parent
def run(self):
self.gate = RLock()
self.atEnd = False
self.hasValue = False
self.value = None
self.sourceSubscription = SingleAssignmentDisposable()
self.sourceSubscription.disposable = self.parent.source.subscribeSafe(self)
samplerSubscription = self.parent.sampler.subscribeSafe(self.SamplerObserver(self))
return CompositeDisposable(self.sourceSubscription, samplerSubscription)
def onNext(self, value):
with self.gate:
self.hasValue = True
self.value = value
def onError(self, exception):
with self.gate:
self.observer.onError(exception)
self.dispose()
def onCompleted(self):
with self.gate:
self.atEnd = True
self.sourceSubscription.dispose()
class SamplerObserver(Observer):
def __init__(self, parent):
self.parent = parent
def onNext(self, value):
with self.parent.gate:
if self.parent.hasValue:
self.parent.hasValue = False
self.parent.observer.onNext(self.parent.value)
if self.parent.atEnd:
self.parent.observer.onCompleted()
self.parent.dispose()
def onError(self, exception):
with self.parent.gate:
self.parent.observer.onError(exception)
self.parent.dispose()
def onCompleted(self):
with self.parent.gate:
if self.parent.hasValue:
self.parent.hasValue = False
self.parent.observer.onNext(self.parent.value)
if self.parent.atEnd:
self.parent.observer.onCompleted()
self.parent.dispose()
示例3: AutoDetachObserver
# 需要导入模块: from rx.disposable import SingleAssignmentDisposable [as 别名]
# 或者: from rx.disposable.SingleAssignmentDisposable import dispose [as 别名]
class AutoDetachObserver(Observer):
def __init__(self, on_next=None, on_error=None, on_completed=None):
self._on_next = on_next or noop
self._on_error = on_error or default_error
self._on_completed = on_completed or noop
self._subscription = SingleAssignmentDisposable()
self.is_stopped = False
def on_next(self, value: Any) -> None:
if self.is_stopped:
return
try:
self._on_next(value)
except Exception:
self.dispose()
raise
def on_error(self, error) -> None:
if self.is_stopped:
return
self.is_stopped = True
try:
self._on_error(error)
finally:
self.dispose()
def on_completed(self) -> None:
if self.is_stopped:
return
self.is_stopped = True
try:
self._on_completed()
finally:
self.dispose()
def set_disposable(self, value):
self._subscription.disposable = value
subscription = property(fset=set_disposable)
def dispose(self) -> None:
self.is_stopped = True
self._subscription.dispose()
def fail(self, exn: Exception) -> bool:
if self.is_stopped:
return False
self.is_stopped = True
self._on_error(exn)
return True
示例4: Sink
# 需要导入模块: from rx.disposable import SingleAssignmentDisposable [as 别名]
# 或者: from rx.disposable.SingleAssignmentDisposable import dispose [as 别名]
class Sink(rx.linq.sink.Sink):
def __init__(self, parent, observer, cancel):
super(TakeLastCount.Sink, self).__init__(observer, cancel)
self.parent = parent
self.queue = deque()
def run(self):
self.subscription = SingleAssignmentDisposable()
self.loopDisposable = SingleAssignmentDisposable()
self.subscription.disposable = self.parent.source.subscribeSafe(self)
return CompositeDisposable(self.subscription, self.loopDisposable)
def onNext(self, value):
self.queue.append(value)
if len(self.queue) > self.parent.count:
self.queue.popleft()
def onError(self, exception):
self.observer.onError(exception)
self.dispose()
def onCompleted(self):
self.subscription.dispose()
scheduler = self.parent.scheduler
if scheduler.isLongRunning:
self.loopDisposable.disposable = scheduler.scheduleLongRunning(self.loop)
else:
self.loopDisposable.disposable = scheduler.scheduleRecursive(self.loopRec)
def loopRec(self, recurse):
if len(self.queue) > 0:
self.observer.onNext(self.queue.popleft())
recurse()
else:
self.observer.onCompleted()
self.dispose()
def loop(self, cancel):
while not cancel.isDisposed:
if len(self.queue) == 0:
self.observer.onCompleted()
break
else:
self.observer.onNext(self.queue.popleft())
self.dispose()
示例5: test_futuredisposable_disposeafterset
# 需要导入模块: from rx.disposable import SingleAssignmentDisposable [as 别名]
# 或者: from rx.disposable.SingleAssignmentDisposable import dispose [as 别名]
def test_futuredisposable_disposeafterset():
d = SingleAssignmentDisposable()
disposed = [False]
def action():
disposed[0] = True
dd = Disposable(action)
d.disposable = dd
assert dd == d.disposable
assert not disposed[0]
d.dispose()
assert disposed[0]
d.dispose()
assert disposed[0]
示例6: Sink
# 需要导入模块: from rx.disposable import SingleAssignmentDisposable [as 别名]
# 或者: from rx.disposable.SingleAssignmentDisposable import dispose [as 别名]
class Sink(rx.linq.sink.Sink):
def __init__(self, parent, observer, cancel):
super(TakeLastBufferTime.Sink, self).__init__(observer, cancel)
self.parent = parent
def run(self):
self.subscription = SingleAssignmentDisposable()
self.loop = SingleAssignmentDisposable()
self.startTime = self.parent.scheduler.now()
self.subscription.disposable = self.parent.source.subscribeSafe(self)
return CompositeDisposable(self.subscription, self.loop)
def elapsed(self):
return self.parent.scheduler.now() - self.startTime
def trim(self, now):
while len(self.queue) > 0:
current = self.queue.popleft()
if current.interval < self.parent.duration:
self.queue.appendleft(current)
break
def onNext(self, value):
now = self.elapsed()
self.queue.append(Struct(value=value,interval=now))
self.trim(now)
def onError(self, exception):
self.observer.onError(exception)
self.dispose()
def onCompleted(self):
self.subscription.dispose()
now = self.elapsed()
self.trim(now)
res = list([x.value for x in self.queue])
self.observer.onNext(res)
self.observer.onCompleted()
self.dispose()
示例7: test_futuredisposable_disposebeforeset
# 需要导入模块: from rx.disposable import SingleAssignmentDisposable [as 别名]
# 或者: from rx.disposable.SingleAssignmentDisposable import dispose [as 别名]
def test_futuredisposable_disposebeforeset():
disposed = [False]
def dispose():
disposed[0] = True
d = SingleAssignmentDisposable()
dd = Disposable(dispose)
assert not disposed[0]
d.dispose()
assert not disposed[0]
d.disposable = dd
assert d.disposable == None
assert disposed[0]
d.dispose()
assert disposed[0]
示例8: AutoDetachObserver
# 需要导入模块: from rx.disposable import SingleAssignmentDisposable [as 别名]
# 或者: from rx.disposable.SingleAssignmentDisposable import dispose [as 别名]
class AutoDetachObserver(ObserverBase):
def __init__(self, observer, disposable = None):
super(AutoDetachObserver, self).__init__()
self.observer = observer
self.m = SingleAssignmentDisposable()
if disposable != None:
self.m.disposable = disposable
def onNextCore(self, value):
noError = False
try:
self.observer.onNext(value)
noError = True
finally:
if not noError:
self.dispose()
def onErrorCore(self, ex):
try:
self.observer.onError(ex)
finally:
self.dispose()
def onCompletedCore(self):
try:
self.observer.onCompleted()
finally:
self.dispose()
def disposable():
"""The disposable property."""
def fget(self):
self.m.disposable
def fset(self, value):
self.m.disposable = value
return locals()
disposable = property(**disposable())
def dispose(self):
with self.lock:
super(AutoDetachObserver, self).dispose()
self.m.dispose()
示例9: O
# 需要导入模块: from rx.disposable import SingleAssignmentDisposable [as 别名]
# 或者: from rx.disposable.SingleAssignmentDisposable import dispose [as 别名]
class O(Observer):
def __init__(self, parent, sourceObserver):
self.parent = parent
self.sourceObserver = sourceObserver
self.subscription = SingleAssignmentDisposable()
def setdisposable(self, value):
self.subscription.disposable = value
disposable = property(None, setdisposable)
def onNext(self, value):
self.sourceObserver.observer = self.parent.observer
self.subscription.dispose()
def onError(self, exception):
self.parent.observer.onError(exception)
self.parent.dispose()
def onCompleted(self):
self.subscription.dispose()
示例10: T
# 需要导入模块: from rx.disposable import SingleAssignmentDisposable [as 别名]
# 或者: from rx.disposable.SingleAssignmentDisposable import dispose [as 别名]
class T(Observer):
def __init__(self, parent):
self.parent = parent
self.observer = NoopObserver()
self.subscription = SingleAssignmentDisposable()
def setdisposable(self, value):
self.subscription.disposable = value
disposable = property(None, setdisposable)
def onNext(self, value):
self.observer.onNext(value)
def onError(self, exception):
self.parent.observer.onError(exception)
self.parent.dispose()
def onCompleted(self):
self.observer.onCompleted()
# We can't cancel the other stream yet, it may be on its way
# to dispatch an OnError message and we don't want to have a race.
self.subscription.dispose()
示例11: ConcurrentSink
# 需要导入模块: from rx.disposable import SingleAssignmentDisposable [as 别名]
# 或者: from rx.disposable.SingleAssignmentDisposable import dispose [as 别名]
class ConcurrentSink(rx.linq.sink.Sink):
def __init__(self, parent, observer, cancel):
super(Merge.ConcurrentSink, self).__init__(observer, cancel)
self.parent = parent
def run(self):
self.gate = RLock()
self.q = Queue()
self.isStopped = False
self.activeCount = 0
self.group = CompositeDisposable()
self.sourceSubscription = SingleAssignmentDisposable()
self.group.add(self.sourceSubscription)
self.sourceSubscription.disposable = self.parent.sources.subscribeSafe(self)
return self.group
def onNext(self, value):
with self.gate:
if self.activeCount < self.parent.maxConcurrency:
self.activeCount += 1
self.subscribe(value)
else:
self.q.put_nowait(value)
def onError(self, exception):
with self.gate:
self.observer.onError(exception)
self.dispose()
def onCompleted(self):
with self.gate:
self.isStopped = True
if self.activeCount == 0:
self.observer.onCompleted()
self.dispose()
else:
self.sourceSubscription.dispose()
def subscribe(self, innerSource):
subscription = SingleAssignmentDisposable()
self.group.add(subscription)
subscription.disposable = innerSource.subscribeSafe(self.LockObserver(self, subscription))
class LockObserver(Observer):
def __init__(self, parent, subscription):
self.parent = parent
self.subscription = subscription
def onNext(self, value):
with self.parent.gate:
self.parent.observer.onNext(value)
def onError(self, exception):
with self.parent.gate:
self.parent.observer.onError(exception)
self.parent.dispose()
def onCompleted(self):
self.parent.group.remove(self.subscription)
with self.parent.gate:
if self.parent.q.qsize() > 0:
s = self.q.get()
self.parent.subscribe(s)
else:
self.parent.activeCount -= 1
if self.parent.isStopped and self.parent.activeCount == 0:
self.parent.observer.onCompleted()
self.parent.dispose()
示例12: SerialSink
# 需要导入模块: from rx.disposable import SingleAssignmentDisposable [as 别名]
# 或者: from rx.disposable.SingleAssignmentDisposable import dispose [as 别名]
class SerialSink(rx.linq.sink.Sink):
def __init__(self, parent, observer, cancel):
super(Merge.SerialSink, self).__init__(observer, cancel)
self.parent = parent
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.sources.subscribeSafe(self)
return self.group
def onNext(self, value):
innerSubscription = SingleAssignmentDisposable()
self.group.add(innerSubscription)
innerSubscription.disposable = value.subscribeSafe(self.LockObserver(self, innerSubscription))
def onError(self, exception):
with self.gate:
self.observer.onError(exception)
self.dispose()
def onCompleted(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()
class LockObserver(Observer):
def __init__(self, parent, subscription):
self.parent = parent
self.subscription = subscription
def onNext(self, value):
with self.parent.gate:
self.parent.observer.onNext(value)
def onError(self, exception):
with self.parent.gate:
self.parent.observer.onError(exception)
self.parent.dispose()
def onCompleted(self):
self.parent.group.remove(self.subscription)
if self.parent.isStopped and self.parent.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.parent.gate:
self.parent.observer.onCompleted()
self.parent.dispose()
示例13: Sink
# 需要导入模块: from rx.disposable import SingleAssignmentDisposable [as 别名]
# 或者: from rx.disposable.SingleAssignmentDisposable import dispose [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()
#.........这里部分代码省略.........
示例14: LongrunningSink
# 需要导入模块: from rx.disposable import SingleAssignmentDisposable [as 别名]
# 或者: from rx.disposable.SingleAssignmentDisposable import dispose [as 别名]
class LongrunningSink(rx.linq.sink.Sink):
def __init__(self, parent, observer, cancel):
super(DelayTime.LongrunningSink, self).__init__(observer, cancel)
self.parent = parent
def run(self):
self.scheduler = self.parent.scheduler
self.cancelTimer = SerialDisposable()
self.gate = RLock()
self.evt = Semaphore(0)
self.stopped = False
self.stop = Event()
self.queue = deque()
self.hasCompleted = False
self.completeAt = 0
self.hasFailed = False
self.exception = None
self.delay = 0
self.startTime = self.scheduler.now()
if self.parent.isAbsolute:
self.cancelTimer.disposable = self.scheduler.scheduleAbsolute(
self.parent.dueTime,
self.start
)
else:
self.delay = Scheduler.normalize(self.parent.dueTime)
self.scheduleDrain()
self.sourceSubscription = SingleAssignmentDisposable()
self.sourceSubscription.disposable = self.parent.source.subscribeSafe(self)
return CompositeDisposable(self.sourceSubscription, self.cancelTimer)
def elapsed(self):
return self.scheduler.now() - self.startTime
def start(self):
with self.gate:
self.delay = self.elapsed()
for item in self.queue:
item.interval += self.delay
self.scheduleDrain()
def scheduleDrain(self):
def cancel():
self.stopped = True
self.stop.set()
self.evt.release()
self.stop.clear()
self.cancelTimer.disposable = Disposable.create(cancel)
self.scheduler.scheduleLongRunning(self.drainQueue)
def onNext(self, value):
next = self.elapsed() + self.delay
with self.gate:
self.queue.append(Struct(value=value, interval=next))
self.evt.release()
def onError(self, exception):
self.sourceSubscription.dispose()
with self.gate:
self.queue.clear()
self.exception = exception
self.hasFailed = True
self.evt.release()
def onCompleted(self):
self.sourceSubscription.dispose()
next = self.elapsed() + self.delay
with self.gate:
self.completeAt = next
self.hasCompleted = True
self.evt.release()
def drainQueue(self, cancel):
while True:
self.evt.acquire()
if self.stopped:
return
hasFailed = False
error = NotImplementedError
hasValue = False
value = NotImplementedError
hasCompleted = False
#.........这里部分代码省略.........
示例15: Sink
# 需要导入模块: from rx.disposable import SingleAssignmentDisposable [as 别名]
# 或者: from rx.disposable.SingleAssignmentDisposable import dispose [as 别名]
class Sink(rx.linq.sink.Sink):
def __init__(self, parent, observer, cancel):
super(DelayTime.Sink, self).__init__(observer, cancel)
self.parent = parent
def run(self):
self.scheduler = self.parent.scheduler
self.cancelTimer = SerialDisposable()
self.gate = RLock()
self.active = False # as soon as a value arrived
self.running = False # on relative: True, on absolute: True after absolute time
self.queue = deque()
self.hasCompleted = False
self.completeAt = 0
self.hasFailed = False
self.exception = None
self.delay = 0
self.startTime = self.scheduler.now()
if self.parent.isAbsolute:
self.ready = False
self.cancelTimer.disposable = self.scheduler.scheduleWithAbsolute(
self.parent.dueTime,
self.start
)
else:
self.ready = True
self.delay = Scheduler.normalize(self.parent.dueTime)
self.sourceSubscription = SingleAssignmentDisposable()
self.sourceSubscription.disposable = self.parent.source.subscribeSafe(self)
return CompositeDisposable(self.sourceSubscription, self.cancelTimer)
def elapsed(self):
return self.scheduler.now() - self.startTime
def start(self):
next = 0
shouldRun = False
with self.gate:
self.delay = self.elapsed()
if len(self.queue) > 0:
next = self.queue[0].interval
for item in self.queue:
item.interval += self.delay
shouldRun = True
self.active = True
self.ready = True
if shouldRun:
self.cancelTimer.disposable = self.scheduler.scheduleRecursiveWithRelative(
next,
self.drainQueue
)
def onNext(self, value):
next = self.elapsed() + self.delay
shouldRun = False
with self.gate:
self.queue.append(Struct(value=value, interval=next))
shouldRun = self.ready and (not self.active)
self.active = True
if shouldRun:
self.cancelTimer.disposable = self.scheduler.scheduleRecursiveWithRelative(
self.delay,
self.drainQueue
)
def onError(self, exception):
self.sourceSubscription.dispose()
shouldRun = False
with self.gate:
self.queue.clear()
self.exception = exception
self.hasFailed = True
shouldRun = not self.running
if shouldRun:
self.observer.onError(exception)
self.dispose()
def onCompleted(self):
self.sourceSubscription.dispose()
next = self.elapsed() + self.delay
#.........这里部分代码省略.........