2017-03-05 48 views
2

数据实时变化,我用Python检索Mongo的数据库中的数据进行分析。 使用流星的应用程序和客户端蟒蛇所以我改变数据以实时检索。这是我的代码:请参阅使用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”当前数据和控制台将关闭,

我不想让控制台保持开放和打印数据的情况下,的变化。 我已经使用,但是实际让脚本运行,看变化,但我想这不是一个很好的解决方案。有没有其他优化的解决方案?

+0

我从来没有用过MeteorClient在Python,但在流星有一个[观察器功能(HTTP://文档。 meteor.com/api/collections.html#Mongo-Cursor-observe),你可以调用上的光标,它会运行一个回调,每一次数据添加/更新/从集合中删除。看看你能否在MeteorClient中找到等价的功能。 – jordanwillis

+0

是的,我已经试过和它的工作谢谢 –

回答

0

要获得实时反馈,你需要订阅的变化,然后监视这些变化。下面是一个看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

+0

谢谢你的人,你让我很快乐:) –