GO 如何实现 页面非阻塞?

2018-06-20 16:27:27 +08:00
 lauix
如题

页面非阻塞,就是一个页面访问要 5 秒,10 人访问不排队,并发进行。

Nodejs 自带,Python tornado gevent 都可以实现。

今天研究 golang 发现找不到如何实现。。。

研究了 channel 还是不行

问问 v2 的大神们,有解决方法吗? or 是否有 go web 的高性能解决方案。

基于 web 框架 iris 开发研究测试。
7609 次点击
所在节点    程序员
66 条回复
dishonest
2018-06-20 16:31:03 +08:00
每个请求 go 不是会开一个 goroutine 吗,这就是并发的呀
gouchaoer
2018-06-20 16:31:58 +08:00
你用 go 写的都是非阻塞的
k9982874
2018-06-20 16:34:12 +08:00
GOMAXPROCS 了解一下?
feiyuanqiu
2018-06-20 16:34:30 +08:00
可以用 buffered channel,也可以用 select{ default: } 做 Non-Blocking Channel 操作

https://tour.golang.org/concurrency/6
lauix
2018-06-20 16:35:48 +08:00
@dishonest
@gouchaoer
没有啊,(代码里写了个 10 秒等待) 两个浏览器同时访问一个页面,都是依次排队执行的。第一个浏览器执行完,第二个浏览器才会执行。
lauix
2018-06-20 16:38:47 +08:00
@k9982874
这个我知道,并不能解决 页面非阻塞 也就是 event loop

@feiyuanqiu
看了看,一样不能实现 页面非阻塞
nazor
2018-06-20 16:44:04 +08:00
其实我更好奇你写出的阻塞代码。
ZSeptember
2018-06-20 16:47:05 +08:00
我也挺好奇的。贴出你的代码看看。
LT
2018-06-20 16:48:45 +08:00
其实我更好奇你写出的阻塞代码。每个 http 请求本来就是独立的啊
mritd
2018-06-20 16:52:45 +08:00
Talk is cheap, show me the code!
alexsunxl
2018-06-20 16:58:53 +08:00
非常明确, 是你的代码有问题.. 贴出来吧
lauix
2018-06-20 17:03:30 +08:00
@nazor
@ZSeptember
@LT
@alexsunxl
@mritd

看 追加的内容
LT
2018-06-20 17:21:08 +08:00
time.sleep 是主线程休眠,你要模拟延迟响应应该写盗 go func(){}里面
LT
2018-06-20 17:21:30 +08:00
```
func test(ctx context.Context) {
go func(){
fmt.Println("start")

// 等待 10 秒
time.Sleep(time.Duration(10) * time.Second)

fmt.Println("end")

ctx.Text("test")
}()
}

```
flyingnn
2018-06-20 17:26:17 +08:00
是并发的啊,看控制台输出:
start
start
end
end
LT
2018-06-20 17:30:43 +08:00
mkeith
2018-06-20 17:31:16 +08:00
@flyingnn 你和楼主分别是什么操作系统啊?
tysx
2018-06-20 17:32:33 +08:00
mark 一下,好奇你是怎么做到的
march1993
2018-06-20 17:33:11 +08:00
你看看右下角图的 URL 是什么鬼……
dishonest
2018-06-20 17:33:35 +08:00
@LT time.Sleep 是 pause current goroutine。

iris 没用过,或许是 iris 的问题?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/464449

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX