2
Cassandra Python驅動程序的問題在於「未來」對象返回時通過副作用添加回調。這意味着「未來」不可組合,就像Javascript或Scala的Future
是可組合的。我想知道如果存在可用於實現轉化非組合的未來成組合的未來的圖案(優選不泄漏存儲器。)Python:Asynchronous Cassandra Inserts
my_query_object.insert(1, 2, 3, 'Fred Flinstone')
.insert(1, 2, 3, 'Barney Rubble')
.insert(5000, 2, 3, 'George Jetson')
.insert(5000, 2, 3, 'Jane his wife')
在從卡桑德拉的Python駕駛員的performance部尋找Datastax,我看到他們如何創建一個不斷鏈式插入查詢的例子。也就是這種圖案的稍微更復雜的版本:
def insert_next(previous_result=sentinel):
if previous_result is not sentinel:
if isinstance(previous_result, BaseException):
log.error("Error on insert: %r", previous_result)
future = session.execute_async(query)
# NOTE: this callback also handles errors
future.add_callbacks(insert_next, insert_next)
它作爲一個玩具的例子很好。第一個查詢完成後,再次執行另一個等效查詢。該方案允許他們實現7k次寫入,而不嘗試「鏈接」回調的版本則約爲2k次寫入/秒。
我一直試圖讓我的頭腦創建某種機制,使我能夠重新捕獲確切的機制,但無濟於事。任何人都會想出類似的東西?