Cassandra Python驱动程序execute_async与回调无法正常工作 [英] Cassandra python driver execute_async with callback not working as expected

查看:126
本文介绍了Cassandra Python驱动程序execute_async与回调无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用datastax python驱动程序从Cassandra查询数据。我希望此查询尽可能快。我正在使用同步 session.execute 方法,但想使用异步 session.execute_async ,因为它可能是由于不必等待行处理代码完成,因此效率更高。

I am trying to query data from Cassandra using the datastax python driver. I would like this query to be as fast as possible. I was using the synchronous session.execute method but would like to use the asynchronous session.execute_async as it will presumably be more efficient by not having to wait for the row processing code to complete.

当我尝试使用这种方法时,根据文档,它不起作用。我在使用Cassandra 2.1.3和v2.5 cassandra python驱动程序的Ubuntu 14.0.4上。

When I try to use this approach, as per the documentation it does not work. I am on Ubuntu 14.0.4 using Cassandra 2.1.3 and the v2.5 cassandra python driver.

以下代码足以在我的计算机上重现该问题:

The following code is enough to reproduce the issue on my machine:

def print_row_count(rows, label):
    for i, row in enumerate(rows):
        do_something = row
    print "{}: processed {} rows".format(label, i+1)

def print_err(reason):
    print "Error: {}".format(reason)

cluster = Cluster(['192.168.1.100'])
session = cluster.connect()
session.set_keyspace("foo")
session.default_fetch_size = 200
future_res = session.execute_async("SELECT * FROM bar LIMIT 1000;")
future_res.add_callback(print_row_count, 'Async')
future_res.add_errback(print_err)

当我执行此代码时,不会打印任何内容。

When I execute this code nothing gets printed.

但是,当我添加到 ResponseFuture.result 的阻塞调用中时,同步调用将得到执行,并且原始异步ca被执行。 lls。

However when I add into the blocking call to ResponseFuture.result that synchronous call gets executed as well as the original async calls.

block_future_res = future_res.result()
print_row_count(block_future_res, 'BlockFuture') 

运行以下两行后的输出:

Output after running with these two lines added:

Async: processed 200 rows
Async: processed 200 rows
Async: processed 200 rows
Async: processed 200 rows
Async: processed 200 rows
BlockFuture: processed 1000 rows

也许我误解了回调系统的工作原理,但看不到文档中的任何位置您需要先调用 result()才能执行回调。

Perhaps I misunderstand how the callback system works but I can't see anywhere in the docs where you need to call result() first for the callbacks to execute.

任何帮助表示赞赏。

更新:

有关回调行为的最初问题由@WangST
回答,但是我希望能够通过async_executions进行页面调度,并且以下代码允许这样做:

The initial question about callback behaviour is answered by @WangST However I wanted to be able to page through async_executions and the following code allows this:

future = session.execute_async("SELECT * FROM metadata LIMIT 1000;")
future.add_callback(print_row_count, 'Async')
future.add_errback(print_err)

# Call this once so that the future has_more_pages value is set
future_res = future.result()
while future.has_more_pages:
    future.start_fetching_next_page()
    future_res = future.result()


推荐答案

不带:

block_future_res = future_res.result()

execute_async()查询完成之前,主线程无事可做并退出。所以什么也没印出来。

the main thread has nothing to do and exit, before the execute_async() query finish. so nothing printed.

这篇关于Cassandra Python驱动程序execute_async与回调无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆