zoukankan      html  css  js  c++  java
  • core.async中go的作用研究

    (defmacro go
      "Asynchronously executes the body, returning immediately to the
      calling thread. Additionally, any visible calls to <!, >! and alt!/alts!
      channel operations within the body will block (if necessary) by
      'parking' the calling thread rather than tying up an OS thread (or
      the only JS thread when in ClojureScript). Upon completion of the
      operation, the body will be resumed.
      Returns a channel which will receive the result of the body when
      completed"
      [& body]
      `(let [c# (cljs.core.async/chan 1)]
         (cljs.core.async.impl.dispatch/run
          (fn []
            (let [f# ~(ioc/state-machine body 1 &env ioc/async-custom-terminators)
                  state# (-> (f#)
                             (ioc/aset-all! cljs.core.async.impl.ioc-helpers/USER-START-IDX c#))]
              (cljs.core.async.impl.ioc-helpers/run-state-machine-wrapped state#))))
         c#))

    查了不少资料,不过还是代码的注释说的清楚一点,一旦在go的body中显式调用 <!,>!,alt!,alts!就会将go的状态机转为parking状态。

    优点:对比thread的block住再切换thread可以显著减少切换时候的资源消耗,而且不受系统的

    缺点:如果一个go block不使用<!而是<!!,将会使得整个thread卡在当前block导致后面的block无法执行

    另外在参考了这篇文章里说的:

    The <! function takes a value from the channel. This function can only be used in the context context of a go.

    This will allow the loop to park until it has a value.

    It’s nice because it allow you to write code to appear as if the code blocks at that point without actually blocking.

    妈蛋原来这几个操作符只能用在go block里啊!

    另外go的介绍中提到:

    The go block itself immediately returns a channel, on which it will eventually put the value of the last expression of the body (if non-nil), and then close.

    go block会直接的返回一个channel,执行的结果会最后反馈到这个channel里

    参考:

    http://swannodette.github.io/2013/08/02/100000-processes/

    http://clojure.com/blog/2013/06/28/clojure-core-async-channels.html

    http://martintrojer.github.io/clojure/2013/07/07/coreasync-and-blocking-io/

    http://stackoverflow.com/questions/21445284/when-to-use-non-blocking-threads-and-blocking-goroutines-with-clojure

    https://groups.google.com/forum/#!topic/clojure/QWYcsUEtdnE

  • 相关阅读:
    win7破解vs2008
    学习C# via clr 第三版扎记
    html 网页嵌入 QQ MSN 旺旺 Gtalk快速对话框官方代码的方法
    识别打气筒气嘴 法式气嘴、美式气嘴、英式气嘴
    飞信资料
    DCOM 遇到错误 “登录失败:未知的用户名或错误密码
    VS 新建Asp.Net 网站 与 新建Asp.Net web 应用程序区别
    一款网页开发必备,让你欲罢不能的客户网页编辑js控件 百度 UEditor
    一天一小步_我学C#入门精典_第一天
    将DataTable对象输出到新建EXCEL文档中
  • 原文地址:https://www.cnblogs.com/TLightSky/p/4274612.html
Copyright © 2011-2022 走看看