本文整理匯總了Python中rx.core.Disposable類的典型用法代碼示例。如果您正苦於以下問題:Python Disposable類的具體用法?Python Disposable怎麽用?Python Disposable使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Disposable類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_groupdisposable_remove
def test_groupdisposable_remove():
disp1 = [False]
disp2 = [False]
def action1():
disp1[0] = True
d1 = Disposable.create(action1)
def action2():
disp2[0] = True
d2 = Disposable.create(action2)
g = CompositeDisposable(d1, d2)
assert g.length == 2
assert g.contains(d1)
assert g.contains(d2)
assert g.remove(d1)
assert g.length == 1
assert not g.contains(d1)
assert g.contains(d2)
assert disp1[0]
assert g.remove(d2)
assert not g.contains(d1)
assert not g.contains(d2)
assert disp2[0]
disp3 = [False]
def action3():
disp3[0] = True
d3 = Disposable.create(action3)
assert not g.remove(d3)
assert not disp3[0]
示例2: test_groupdisposable_clear
def test_groupdisposable_clear():
disp1 = [False]
disp2 = [False]
def action1():
disp1[0] = True
d1 = Disposable.create(action1)
def action2():
disp2[0] = True
d2 = Disposable.create(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.create(action3)
g.add(d3);
assert not disp3[0]
assert g.length == 1
示例3: test_groupdisposable_contains
def test_groupdisposable_contains():
d1 = Disposable.empty()
d2 = Disposable.empty()
g = CompositeDisposable(d1, d2)
assert g.length == 2
assert g.contains(d1)
assert g.contains(d2)
示例4: _subscribe_core
def _subscribe_core(self, observer):
with self.lock:
self.check_disposed()
if not self.is_stopped:
self.observers.append(observer)
return InnerSubscription(self, observer)
if self.exception:
observer.on_error(self.exception)
return Disposable.empty()
observer.on_completed()
return Disposable.empty()
示例5: _gtk_schedule
def _gtk_schedule(self, time, action, state, periodic=False):
# Do not import GLib into global scope because Qt and GLib
# don't like each other there
from gi.repository import GLib
scheduler = self
msecs = self.to_relative(time)
disposable = SingleAssignmentDisposable()
periodic_state = [state]
stopped = [False]
def timer_handler(_):
if stopped[0]:
return False
if periodic:
periodic_state[0] = action(periodic_state[0])
else:
disposable.disposable = action(scheduler, state)
return periodic
GLib.timeout_add(msecs, timer_handler, None)
def dispose():
stopped[0] = True
return CompositeDisposable(disposable, Disposable.create(dispose))
示例6: schedule_periodic
def schedule_periodic(self, period, action, state=None):
"""Schedules a periodic piece of work by dynamically discovering the
schedulers capabilities.
Keyword arguments:
period -- Period for running the work periodically.
action -- Action to be executed.
state -- [Optional] Initial state passed to the action upon the first
iteration.
Returns the disposable object used to cancel the scheduled recurring
action (best effort)."""
period /= 1000.0
timer = [None]
s = [state]
def interval():
new_state = action(s[0])
if new_state is not None: # Update state if other than None
s[0] = new_state
timer[0] = Timer(period, interval)
timer[0].setDaemon(True)
timer[0].start()
timer[0] = Timer(period, interval)
timer[0].setDaemon(True)
timer[0].start()
def dispose():
timer[0].cancel()
return Disposable.create(dispose)
示例7: subscribe
def subscribe(observer):
subscription = SerialDisposable()
cancelable = SerialDisposable()
enum = iter(sources)
is_disposed = []
def action(action1, state=None):
if is_disposed:
return
def on_completed():
cancelable.disposable = scheduler.schedule(action)
try:
current = next(enum)
except StopIteration:
observer.on_completed()
except Exception as ex:
observer.on_error(ex)
else:
d = SingleAssignmentDisposable()
subscription.disposable = d
d.disposable = current.subscribe(observer.on_next, observer.on_error, on_completed)
cancelable.disposable = scheduler.schedule(action)
def dispose():
is_disposed.append(True)
return CompositeDisposable(subscription, cancelable, Disposable.create(dispose))
示例8: schedule_relative
def schedule_relative(self, duetime, action, state=None):
"""Schedules an action to be executed after duetime.
Keyword arguments:
duetime -- {timedelta} Relative time after which to execute the action.
action -- {Function} Action to be executed.
Returns {Disposable} The disposable object used to cancel the scheduled
action (best effort)."""
scheduler = self
seconds = self.to_relative(duetime)/1000.0
if not seconds:
return scheduler.schedule(action, state)
disposable = SingleAssignmentDisposable()
def interval():
disposable.disposable = self.invoke_action(action, state)
log.debug("timeout: %s", seconds)
timer = [eventlet.spawn_after(seconds, interval)]
def dispose():
# nonlocal timer
timer[0].kill()
return CompositeDisposable(disposable, Disposable.create(dispose))
示例9: wrapped_action
def wrapped_action(self, state):
try:
return action(parent._get_recursive_wrapper(self), state)
except Exception as ex:
if not parent._handler(ex):
raise Exception(ex)
return Disposable.empty()
示例10: subscribe
def subscribe(observer):
enum = iter(sources)
is_disposed = [False]
subscription = SerialDisposable()
def action(action1, state=None):
if is_disposed[0]:
return
try:
current = next(enum)
except StopIteration:
observer.on_completed()
except Exception as ex:
observer.on_error(ex)
else:
d = SingleAssignmentDisposable()
subscription.disposable = d
d.disposable = current.subscribe(
observer.on_next,
observer.on_error,
lambda: action1()
)
cancelable = immediate_scheduler.schedule_recursive(action)
def dispose():
is_disposed[0] = True
return CompositeDisposable(subscription, cancelable, Disposable.create(dispose))
示例11: _wxtimer_schedule
def _wxtimer_schedule(self, time, action, state, periodic=False):
scheduler = self
msecs = self.to_relative(time)
disposable = SingleAssignmentDisposable()
periodic_state = [state]
def interval():
if periodic:
periodic_state[0] = action(periodic_state[0])
else:
disposable.disposable = action(scheduler, state)
log.debug("timeout: %s", msecs)
if msecs == 0:
msecs = 1 # wx.Timer doesn't support zero.
timer = self._timer_class(interval)
timer.Start(
msecs,
self.wx.TIMER_CONTINUOUS if periodic else self.wx.TIMER_ONE_SHOT
)
self._timers.add(timer)
def dispose():
timer.Stop()
self._timers.remove(timer)
return CompositeDisposable(disposable, Disposable.create(dispose))
示例12: schedule_periodic
def schedule_periodic(self, period, action, state=None):
"""Schedule a periodic piece of work."""
secs = self.to_relative(period) / 1000.0
disposed = []
s = [state]
def run():
while True:
time.sleep(secs)
if disposed:
return
new_state = action(s[0])
if new_state is not None:
s[0] = new_state
thread = self.thread_factory(run)
thread.start()
def dispose():
disposed.append(True)
return Disposable.create(dispose)
示例13: schedule_periodic
def schedule_periodic(self, period, action, state=None):
"""Schedules a periodic piece of work to be executed in the tkinter
mainloop.
Keyword arguments:
period -- Period in milliseconds for running the work periodically.
action -- Action to be executed.
state -- [Optional] Initial state passed to the action upon the first
iteration.
Returns the disposable object used to cancel the scheduled recurring
action (best effort)."""
state = [state]
def interval():
state[0] = action(state[0])
alarm[0] = self.master.after(period, interval)
log.debug("timeout: %s", period)
alarm = [self.master.after(period, interval)]
def dispose():
# nonlocal alarm
self.master.after_cancel(alarm[0])
return Disposable.create(dispose)
示例14: schedule_relative
def schedule_relative(self, duetime, action, state=None):
"""Schedules an action to be executed after duetime.
Keyword arguments:
duetime -- {timedelta} Relative time after which to execute the action.
action -- {Function} Action to be executed.
Returns {Disposable} The disposable object used to cancel the scheduled
action (best effort)."""
scheduler = self
msecs = self.to_relative(duetime)
if msecs == 0:
return scheduler.schedule(action, state)
disposable = SingleAssignmentDisposable()
def interval():
disposable.disposable = self.invoke_action(action, state)
log.debug("timeout: %s", msecs)
alarm = self.master.after(msecs, interval)
def dispose():
# nonlocal alarm
self.master.after_cancel(alarm)
return CompositeDisposable(disposable, Disposable.create(dispose))
示例15: schedule_relative
def schedule_relative(self, duetime, action, state=None):
"""Schedules an action to be executed after duetime.
Keyword arguments:
duetime -- {timedelta} Relative time after which to execute the action.
action -- {Function} Action to be executed.
Returns {Disposable} The disposable object used to cancel the scheduled
action (best effort)."""
from twisted.internet.task import deferLater
scheduler = self
seconds = self.to_relative(duetime)/1000.0
disposable = SingleAssignmentDisposable()
def interval():
disposable.disposable = action(scheduler, state)
log.debug("timeout: %s", seconds)
handle = deferLater(self.reactor, seconds, interval).addErrback(lambda _: None)
def dispose():
if not handle.called:
handle.cancel()
return CompositeDisposable(disposable, Disposable.create(dispose))