python - 使用 python meteor 查看数据的实时变化

标签 python mongodb meteor real-time ddp

我正在使用 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”中的当前数据,控制台将关闭,

我想让控制台保持打开状态并在发生变化时打印数据。 我已经使用 While 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')

( Reference ) ( Docs )

关于python - 使用 python meteor 查看数据的实时变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42611765/

相关文章:

python - 如何在 Django 的列表迭代期间显示索引?

python - 当通过不允许的方法(如 PUT、DELETE...)请求应用程序时,如何返回 json 输出?

python - 类型错误 : 'CommandCursor' object has no attribute '__getitem__'

javascript - Meteor 出现意外的 mongo 退出代码 100

javascript - Meteor:确保函数不会对远程数据抛出异常

javascript - 如何使用 meteor 在客户端对象上绑定(bind)服务器端事件,反之亦然

python - 大型 python 重新数组的内存处理,以便在 GridEngine 上快速读写

python - 如何确定 Keras 上的卷积神经网络预测的二进制类别?

javascript - Meteor.users.update 用户对象内列表中的元素 [Meteor]

node.js - 如何将 meteor 助手存储在 mongoDB 集合中,以及如何从 mongoDB 集合中呈现它