请参阅使用Python Meteor在数据中的实时更改



我正在使用python从mongo数据库中检索数据来分析它。因此,我正在使用Meteor应用和客户端Python更改数据,以实时检索数据。这是我的代码:

from MeteorClient import MeteorClient
def call_back_meth():
    print("subscribed")
client = MeteorClient('ws://localhost:3000/websocket')
client.connect()
client.subscribe('tasks', [], call_back_meth)
a=client.find('tasks')
print(a)

当我运行此脚本时,它仅显示" A"中的当前数据,并且控制台将关闭,

我想让控制台保持打开状态并在发生变化时打印数据。我在True时使用了脚本运行并查看更改,但我想这不是一个好的解决方案。还有其他优化解决方案吗?

要获取实时反馈,您需要订阅更改,然后监视这些更改。这是观看tasks的示例:

from MeteorClient import MeteorClient
def call_back_added(collection, id, fields):
    print('* ADDED {} {}'.format(collection, id))
    for key, value in fields.items():
        print('  - FIELD {} {}'.format(key, value))
    # query the data each time something has been added to
    # a collection to see the data `grow`
    all_lists = client.find('lists', selector={})
    print('Lists: {}'.format(all_lists))
    print('Num lists: {}'.format(len(all_lists)))
client = MeteorClient('ws://localhost:3000/websocket')
client.on('added', call_back_added)
client.connect()
client.subscribe('tasks')
# (sort of) hacky way to keep the client alive
# ctrl + c to kill the script
while True:
    try:
        time.sleep(1)
    except KeyboardInterrupt:
        break
client.unsubscribe('tasks')

(参考)(doc)

最新更新