使用for在python中创建多个Rx主题

时间:2019-07-18 00:16:48

标签: python python-3.x reactive-programming

我正在尝试为Subject中的每个Observables创建一个cam和2个cams。我通过将对象保存在Cam类中并在Cam中为每个cam存储一个cam_holder类实例来防止对象被删除。这里的问题是:我最终得到3个rx中继器,它们全部用于cam中的最后一个cams,而不是每个中继器。有人可以找到问题所在吗?

cam_holder = {}

class Cam():
    cam = None
    watchdog = None
    buffer_signal = None
    stream = None
    repeater = None

for cam in cams:
    unique_id = str(cam.ip) + ':' + str(cam.onvif)
    cam_holder[unique_id] = Cam()
    c = cam_holder[unique_id]
    c.cam = cam
    c.cam.log('starting watchdog')

    c.watchdog = rx.subject.Subject()

    c.buffer_signal = rx.create(lambda observable, _: c.watchdog.subscribe(observable)).pipe(
        ops.filter(lambda x: x==Camera.COMPLETE_BUFFER)
    )

    c.stream = rx.create(lambda observable, _: c.watchdog.subscribe(observable)).pipe(
        ops.buffer_when(lambda: c.buffer_signal)
    )

    c.stream.subscribe(
        on_next = lambda i: process_camera_condition(c.cam,i),
        on_error = lambda e: c.cam.log(e),
        on_completed = lambda: None,
    )

    c.buffer_signal.subscribe(
        on_next = lambda i: None,
        on_error = lambda e: c.cam.log(e),
        on_completed = lambda: None,
    )

    c.repeater = rx.interval(QUERY_INTERVAL).pipe(
        ops.do_action(lambda x: c.cam.watchdog(c.watchdog,None))
    )

    c.repeater.subscribe(
        on_next = lambda i: None,
        on_error = lambda e: c.cam.log(e),
        on_completed = lambda: None,
    )

0 个答案:

没有答案