博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
core.async中go的作用研究
阅读量:7254 次
发布时间:2019-06-29

本文共 2014 字,大约阅读时间需要 6 分钟。

(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

转载于:https://www.cnblogs.com/TLightSky/p/4274612.html

你可能感兴趣的文章
SSH之密钥登陆
查看>>
批量上传公钥到Linux服务器
查看>>
关于日立存储更换故障硬盘
查看>>
从程序员到技术领导者
查看>>
squid的配置及应用
查看>>
pycharm,vim,items2常用快捷键
查看>>
数据支撑环境的改造
查看>>
ifconfig 命令用来查看和配置网络设备
查看>>
symbol AP5131重置密码和恢复出厂设置
查看>>
自定义一个jdbc框架
查看>>
yarn上手体验
查看>>
BPMN 2.0规范详解
查看>>
每天学习一个LINUX命令:pwd 显示当前目录(Print-Working-Directory)
查看>>
2.27linux和windows互传文件 3.1 用户配置文件和密码配置文件 3.2 用户组管理
查看>>
linux下yum三种搭建方法
查看>>
QTP对日前控件的处理
查看>>
前端技术/前端冷知识集锦
查看>>
RGW Usage类解析
查看>>
模板引擎缓存
查看>>
php 5.6.11添加模块
查看>>