如何判断一个参考是否正在由Clojure中的dosync更新? [英] How can I tell if a ref is being updated by a dosync in Clojure?

查看:94
本文介绍了如何判断一个参考是否正在由Clojure中的dosync更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些更新,需要做一个参考,但我想选择一个时间来执行更新时,ref不大量使用。

I have some updates which need to be made to a ref, but I want to choose a time to perform the updates when the ref is not in heavy use. Is there a way I can programatically tell when the ref is in a transaction?

推荐答案

你可以 < a href =http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/add-watch =nofollow>添加观看 每次你的ref写入时更新一个访问时间(你提到它是写主要)。这将给你一个忙碌的启发。

you could add-watch that would update an access-time each time your ref is written (you mentions it was write mostly). This would give you a huristic for busy.

(def my-ref (ref {}))
(def my-ref-atime (atom 0))
(add-watch my-ref (fn [key ref old new] (swap! my-ref-atime current-time-fn)))) 

那么只有当atime在过去超过10ms时才进行低优先级工作。

then only do low priority work when atime is more than 10ms in the past.

这篇关于如何判断一个参考是否正在由Clojure中的dosync更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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