• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python disposables.SerialDisposable类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中rx.disposables.SerialDisposable的典型用法代码示例。如果您正苦于以下问题:Python SerialDisposable类的具体用法?Python SerialDisposable怎么用?Python SerialDisposable使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了SerialDisposable类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: subscribe

    def subscribe(observer):
        subscription = SerialDisposable()
        cancelable = SerialDisposable()

        def action(scheduler, state=None):
            try:
                source = next(sources)
            except StopIteration:
                observer.on_completed()
                return

            # Allow source to be a factory method taking an error
            source = source(state) if callable(source) else source
            current = Observable.from_future(source)

            d = SingleAssignmentDisposable()
            subscription.disposable = d

            def on_resume(state=None):
                scheduler.schedule(action, state)

            d.disposable = current.subscribe(observer.on_next, on_resume, on_resume)

        cancelable.disposable = scheduler.schedule(action)
        return CompositeDisposable(subscription, cancelable)
开发者ID:JohnWowUs,项目名称:RxPY,代码行数:25,代码来源:onerrorresumenext.py


示例2: test_select_disposeinsideselector

 def test_select_disposeinsideselector(self):
     scheduler = TestScheduler()
     xs = scheduler.create_hot_observable(on_next(100, 1), on_next(200, 2), on_next(500, 3), on_next(600, 4))
     results = scheduler.create_observer()
     d = SerialDisposable()
     invoked = [0]
     
     def projection(x, *args, **kw):
         invoked[0] += 1
         
         if scheduler.clock > 400:
             #print("*** Dispose ****")
             d.dispose()
         return x
 
     d.disposable = xs.select(projection).subscribe(results)
 
     def action(scheduler, state):
         return d.dispose()
 
     scheduler.schedule_absolute(ReactiveTest.disposed, action)
     scheduler.start()
     
     results.messages.assert_equal(on_next(100, 1), on_next(200, 2))
     xs.subscriptions.assert_equal(ReactiveTest.subscribe(0, 500))
     
     assert invoked[0] == 3
开发者ID:mvschaik,项目名称:RxPY,代码行数:27,代码来源:test_select.py


示例3: 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))
开发者ID:JohnWowUs,项目名称:RxPY,代码行数:29,代码来源:concat.py


示例4: test_select_with_index_dispose_inside_selector

def test_select_with_index_dispose_inside_selector():
    scheduler = TestScheduler()
    xs = scheduler.create_hot_observable(on_next(100, 4), on_next(200, 3), on_next(500, 2), on_next(600, 1))
    invoked = 0
    results = scheduler.create_observer()
    d = SerialDisposable()
    
    def projection(x, index):
        nonlocal invoked
        invoked += 1
        if scheduler.clock > 400:
            d.dispose()
        
        return x + index * 10

    d.disposable = xs.select(projection).subscribe(results)

    def action(scheduler, state):
        return d.dispose()

    scheduler.schedule_absolute(disposed, action)
    scheduler.start()
    results.messages.assert_equal(on_next(100, 4), on_next(200, 13))
    xs.subscriptions.assert_equal(subscribe(0, 500))
    assert invoked == 3
开发者ID:Reactive-Extensions,项目名称:RxPy,代码行数:25,代码来源:test_select.py


示例5: subscribe

    def subscribe(observer):
        m = SingleAssignmentDisposable()
        d = SerialDisposable()
        d.disposable = m

        def action(scheduler, state):
            d.disposable = ScheduledDisposable(scheduler, source.subscribe(observer))

        m.disposable = scheduler.schedule(action)
        return d
开发者ID:ESSL-CQQ,项目名称:RxPY,代码行数:10,代码来源:subscribeon.py


示例6: ScheduledObserver

class ScheduledObserver(AbstractObserver):
    def __init__(self, scheduler, observer):
        super(ScheduledObserver, self).__init__()
        self.scheduler = scheduler
        self.observer = observer
        self.is_acquired = False
        self.has_faulted = False
        self.queue = []
        self.disposable = SerialDisposable()

    def next(self, value):
        def func():
            self.observer.on_next(value)
        self.queue.append(func)
    
    def error(self, exception):
        def func():
            self.observer.on_error(exception)    
        self.queue.append(func)
                
    def completed(self):
        def func():
            self.observer.on_completed()        
        self.queue.append(func)
            
    def ensure_active(self):
        is_owner, parent = False, self
        if not self.has_faulted and len(self.queue):
            is_owner = not self.is_acquired
            self.is_acquired = True
        
        if is_owner:
            def action(action1, state):
                work = None
                if len(parent.queue):
                    work = parent.queue.pop(0)
                else:
                    parent.is_acquired = False
                    return
                
                try:
                    work()
                except Exception as ex:
                    parent.queue = []
                    parent.has_faulted = True
                    raise ex
        
                action1()
            self.disposable.disposable = self.scheduler.schedule_recursive(action)
    
    def dispose(self):
        super(ScheduledObserver, self).dispose()
        self.disposable.dispose()
开发者ID:Reactive-Extensions,项目名称:RxPy,代码行数:53,代码来源:scheduledobserver.py


示例7: subscribe

        def subscribe(observer):
            delays = CompositeDisposable()
            at_end = [False]

            def done():
                if (at_end[0] and delays.length == 0):
                    observer.on_completed()

            subscription = SerialDisposable()

            def start():
                def on_next(x):
                    try:
                        delay = selector(x)
                    except Exception as error:
                        observer.on_error(error)
                        return

                    d = SingleAssignmentDisposable()
                    delays.add(d)

                    def on_next(_):
                        observer.on_next(x)
                        delays.remove(d)
                        done()

                    def on_completed():
                        observer.on_next(x)
                        delays.remove(d)
                        done()

                    d.disposable = delay.subscribe(on_next, observer.on_error,
                                                   on_completed)

                def on_completed():
                    at_end[0] = True
                    subscription.dispose()
                    done()

                subscription.disposable = source.subscribe(on_next,
                                                           observer.on_error,
                                                           on_completed)

            if not sub_delay:
                start()
            else:
                subscription.disposable(sub_delay.subscribe(
                    lambda _: start(),
                    observer.on_error,
                    start))

            return CompositeDisposable(subscription, delays)
开发者ID:phaufe,项目名称:RxPY,代码行数:52,代码来源:delaywithselector.py


示例8: test_mutabledisposable_dispose

def test_mutabledisposable_dispose():
    disp = [False]
    m = SerialDisposable()

    def action():
        disp[0] = True
    d = Disposable.create(action)
    m.disposable = d

    assert d == m.disposable
    assert not disp[0]
    m.dispose()
    assert disp[0]
    assert m.disposable == None
开发者ID:ESSL-CQQ,项目名称:RxPY,代码行数:14,代码来源:test_disposable.py


示例9: subscribe

        def subscribe(observer):
            switched = False
            _id = 0
            
            original = SingleAssignmentDisposable()
            subscription = SerialDisposable()
            timer = SerialDisposable()
            subscription.disposable = original

            def create_timer():
                my_id = _id

                def action(scheduler, state=None):
                    nonlocal switched

                    switched = (_id == my_id)
                    timer_wins = switched
                    if timer_wins:
                        subscription.disposable = other.subscribe(observer)
                    
                timer.disposable = scheduler_method(duetime, action)

            create_timer()
            def on_next(x):
                nonlocal _id

                on_next_wins = not switched
                if on_next_wins:
                    _id += 1
                    observer.on_next(x)
                    create_timer()

            def on_error(e):
                nonlocal _id

                on_error_wins = not switched
                if on_error_wins:
                    _id += 1
                    observer.on_error(e)

            def on_completed():
                nonlocal _id
                
                on_completed_wins = not switched
                if on_completed_wins:
                    _id += 1
                    observer.on_completed()
            
            original.disposable = source.subscribe(on_next, on_error, on_completed)
            return CompositeDisposable(subscription, timer)
开发者ID:Reactive-Extensions,项目名称:RxPy,代码行数:50,代码来源:observable_time.py


示例10: test_mutabledisposable_dispose

def test_mutabledisposable_dispose():
    disp = False
    m = SerialDisposable()
    
    def action():
        nonlocal disp
        disp = True
    d = Disposable(action)
    m.disposable = d

    assert d == m.disposable
    assert not disp
    m.dispose()
    assert disp
    assert m.disposable == None
开发者ID:Reactive-Extensions,项目名称:RxPy,代码行数:15,代码来源:test_disposable.py


示例11: __init__

 def __init__(self, scheduler, observer):
     super(ScheduledObserver, self).__init__()
     self.scheduler = scheduler
     self.observer = observer
     self.is_acquired = False
     self.has_faulted = False
     self.queue = []
     self.disposable = SerialDisposable()
开发者ID:tanwanirahul,项目名称:RxPY,代码行数:8,代码来源:scheduledobserver.py


示例12: subscribe

        def subscribe(observer):
            d1 = SingleAssignmentDisposable()
            subscription = SerialDisposable()

            subscription.disposable = d1
            
            def on_error(exception):
                try:
                    result = handler(exception)
                except Exception as ex:
                    observer.on_error(ex)
                    return
                
                d = SingleAssignmentDisposable()
                subscription.disposable = d
                d.disposable = result.subscribe(observer)
            
            d1.disposable = source.subscribe(observer.on_next, on_error, observer.on_completed)
            return subscription
开发者ID:Reactive-Extensions,项目名称:RxPy,代码行数:19,代码来源:observable_multiple.py


示例13: __init__

    def __init__(self, scheduler, observer):
        super(ScheduledObserver, self).__init__(self._next, self._error, self._completed)

        self.scheduler = scheduler
        self.observer = observer

        self.lock = Lock()
        self.is_acquired = False
        self.has_faulted = False
        self.queue = []
        self.disposable = SerialDisposable()
开发者ID:AlexMost,项目名称:RxPY,代码行数:11,代码来源:scheduledobserver.py


示例14: __init__

    def __init__(self, scheduler, period, action, state=None):
        """
        Keyword arguments:
        state -- Initial state passed to the action upon the first iteration.
        period -- Period for running the work periodically.
        action -- Action to be executed, potentially updating the state."""

        self._scheduler = scheduler
        self._state = state
        self._period = period
        self._action = action
        self._cancel = SerialDisposable()
开发者ID:JohnWowUs,项目名称:RxPY,代码行数:12,代码来源:scheduleperiodic.py


示例15: test_mutabledisposable_replacebeforedispose

def test_mutabledisposable_replacebeforedispose():
    disp1 = [False]
    disp2 = [False]
    m = SerialDisposable()

    def action1():
        disp1[0] = True
    d1 = Disposable.create(action1)
    m.disposable = d1

    assert d1 == m.disposable
    assert not disp1[0]

    def action2():
        disp2[0] = True
    d2 = Disposable.create(action2)
    m.disposable = d2

    assert d2 == m.disposable
    assert disp1[0]
    assert not disp2[0]
开发者ID:ESSL-CQQ,项目名称:RxPY,代码行数:21,代码来源:test_disposable.py


示例16: test_mutabledisposable_replaceafterdispose

def test_mutabledisposable_replaceafterdispose():
    disp1 = [False]
    disp2 = [False]
    m = SerialDisposable()
    m.dispose()

    def action1():
        disp1[0] = True
    d1 = Disposable.create(action1)
    m.disposable = d1

    assert m.disposable == None
    assert disp1[0]

    def action2():
        disp2[0] = True
    d2 = Disposable.create(action2)
    m.disposable = d2

    assert m.disposable == None
    assert disp2[0]
开发者ID:ESSL-CQQ,项目名称:RxPY,代码行数:21,代码来源:test_disposable.py


示例17: test_mutabledisposable_replaceafterdispose

def test_mutabledisposable_replaceafterdispose():
    disp1 = False
    disp2 = False
    m = SerialDisposable()
    m.dispose()

    def action1():
        nonlocal disp1
        disp1 = True
    d1 = Disposable(action1)
    m.disposable = d1

    assert m.disposable == None
    assert disp1

    def action2():
        nonlocal disp2
        disp2 = True
    d2 = Disposable(action2)
    m.disposable = d2

    m.disposable == None
    assert disp2
开发者ID:Reactive-Extensions,项目名称:RxPy,代码行数:23,代码来源:test_disposable.py


示例18: test_mutabledisposable_replacebeforedispose

def test_mutabledisposable_replacebeforedispose():
    disp1 = False
    disp2 = False
    m = SerialDisposable()

    def action1():
        nonlocal disp1
        disp1 = True
    d1 = Disposable(action1)
    m.disposable = d1

    assert d1 == m.disposable
    assert not disp1

    def action2():
        nonlocal disp2
        disp2 = True
    d2 = Disposable(action2)
    m.disposable = d2

    assert d2 == m.disposable
    assert disp1
    assert not disp2
开发者ID:Reactive-Extensions,项目名称:RxPy,代码行数:23,代码来源:test_disposable.py


示例19: SchedulePeriodic

class SchedulePeriodic(object):
    """Scheduler with support for running periodic tasks. This type of
    scheduler can be used to run timers more efficiently instead of using
    recursive scheduling."""

    def __init__(self, scheduler, period, action, state=None):
        """
        Keyword arguments:
        state -- Initial state passed to the action upon the first iteration.
        period -- Period for running the work periodically.
        action -- Action to be executed, potentially updating the state."""

        self._scheduler = scheduler
        self._state = state
        self._period = period
        self._action = action
        self._cancel = SerialDisposable()

    def tick(self, scheduler, command):
        self._cancel.disposable = self._scheduler.schedule_relative(self._period, self.tick, 0)
        try:
            new_state = self._action(self._state)
        except Exception:
            self._cancel.dispose()
            raise
        else:
            if new_state is not None:  # Update state if other than None
                self._state = new_state

    def start(self):
        """Returns the disposable object used to cancel the scheduled recurring
        action (best effort).
        """

        self._cancel.disposable = self._scheduler.schedule_relative(self._period, self.tick, 0)
        return self._cancel
开发者ID:JohnWowUs,项目名称:RxPY,代码行数:36,代码来源:scheduleperiodic.py


示例20: subscribe

        def subscribe(observer):
            subscription = SerialDisposable()
            timer = SerialDisposable()
            original = SingleAssignmentDisposable()

            subscription.disposable = original

            switched = False
            _id = [0]

            def set_timer(timeout):
                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)

                    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)

            set_timer(first_timeout)

            def observer_wins():
                res = not switched
                if res:
                    _id[0] += 1

                return res

            def on_next(x):
                if observer_wins():
                    observer.on_next(x)
                    timeout = None
                    try:
                        timeout = timeout_duration_selector(x)
                    except Exception as e:
                        observer.on_error(e)
                        return

                    set_timer(timeout)

            def on_error(e):
                if observer_wins():
                    observer.on_error(e)

            def on_completed():
                if observer_wins():
                    observer.on_completed()

            original.disposable = source.subscribe(on_next, on_error, on_completed)
            return CompositeDisposable(subscription, timer)
开发者ID:phaufe,项目名称:RxPY,代码行数:66,代码来源:timeoutwithselector.py



注:本文中的rx.disposables.SerialDisposable类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python disposables.SingleAssignmentDisposable类代码示例发布时间:2022-05-27
下一篇:
Python disposables.Disposable类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap