多线程 – 何时使用非阻塞>! /线程和阻塞>! / goroutines与clojure core.async

前端之家收集整理的这篇文章主要介绍了多线程 – 何时使用非阻塞>! /线程和阻塞>! / goroutines与clojure core.async前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写一个ETL流程,从产品数据库中读取事件级数据,转换/汇总并写入分析数据仓库.我使用clojure的core.async库将这些进程分离成并发执行的组件.这是我现在的代码的主要部分
  1. (ns data-staging.main
  2. (:require [clojure.core.async :as async])
  3. (:use [clojure.core.match :only (match)]
  4. [data-staging.map-vecs]
  5. [data-staging.tables])
  6. (:gen-class))
  7.  
  8. (def submissions (make-table "Submission" "Valid"))
  9. (def photos (make-table "Photo"))
  10. (def videos (make-table "Video"))
  11. (def votes (make-table "Votes"))
  12.  
  13. ;; define channels used for sequential data processing
  14. (def chan-in (async/chan 100))
  15. (def chan-out (async/chan 100))
  16.  
  17. (defn write-thread [table]
  18. "infinitely loops between reading subsequent 10000 rows from
  19. table and ouputting a vector of the rows(maps)
  20. into 'chan-in'"
  21. (while true
  22. (let [next-rows (get-rows table)]
  23. (async/>!! chan-in next-rows)
  24. (set-max table (:max-id (last next-rows))))))
  25.  
  26. (defn aggregator []
  27. "takes output from 'chan-in' and aggregates it by coupon_id,date.
  28. then adds / drops any fields that are needed / not needed and inputs
  29. into 'chan-out'"
  30. (while true
  31. (->>
  32. (async/<!! chan-in)
  33. aggregate
  34. (async/>!! chan-out))))
  35.  
  36. (defn read-thread []
  37. "reads data from chan out and interts into Analytics DB"
  38. (while true
  39. (upsert (async/<!! chan-out))))
  40.  
  41. (defn -main []
  42. (async/thread (write-thread submissions))
  43. (async/thread (write-thread photos))
  44. (async/thread (write-thread videos))
  45. (async/thread-call aggregator)
  46. (async/thread-call read-thread))

你可以看到,我将每个os组件放在自己的线程上并使用阻止>!呼叫频道.感觉就像使用非阻塞>!对于这种用例,调用以及执行例程可能更好,特别是对于大部分时间执行i / o并等待产品数据库中的新行的数据库读取.是这样的,如果是这样,实施它的最好办法是什么?我对两种方法之间的所有权衡有一点不清楚,以及如何有效地使用go例程.还有关于如何提高整体架构的任何其他建议将非常感谢!

解决方法

就个人而言,我认为你在这里使用线程可能是正确的. go-blocks的魔法非阻塞性质来自“停车”,这是core.async的状态机使用的特殊类型的伪阻塞,但是由于您的数据库调用真正的阻塞而不是将状态机置于停放状态你只是从core.async线程池阻止一些线程.这取决于你的同步调用需要多长时间,所以这是基准可以提供信息的东西,但我强烈怀疑线程是正确的方法.

一个例外是您的聚合器功能.它看起来像我喜欢它可以被折叠成chan-out的定义,as(def chan-out(map< aggregate chan-in)). 对于线程与线程的一般概述,Martin Trojer写了一个很好的examination of the two approaches,哪一个在哪种情况下更快. Cliff的Notes版本是,go-blocks适用于使用与core.async一起使用的已经异步的库,而线程有助于使异步进程不在同步部分中.例如,如果您的数据库有一个基于回调的API,那么go-block将是一个明确的胜利.但是由于它是同步的,它们并不适合.

猜你在找的Java相关文章