How to implement asynchronous reading in go?

huangapple go评论86阅读模式
英文:

How to implement asynchronous reading in go?

问题

我正在研究Linux下的Golang调度器。我认为Golang使用多线程来实现goroutine,当某个goroutine被阻塞在I/O操作(比如读取文件)时,其他线程会继续处理另一个goroutine。但是当有大量的I/O操作时,线程可能不够用,Golang是如何处理这个问题的呢?

我读了一篇文章http://morsmachine.dk/netpoller,它说:“Golang通过使用操作系统提供的异步接口来解决这个问题,但会阻塞正在执行I/O操作的goroutine。”这是否类似于aio_read?据说异步接口存在很多bug。我在源代码中没有找到它,或者我可能错过了。

我们知道,我们可以使用epoll来对管道和套接字进行异步I/O操作,但是epoll不能用于常规文件的读写。而Node.js使用libeio来对常规文件进行异步I/O操作。我想知道Golang在运行时是如何处理这个问题的。

英文:

I study golang scheduler in linux. I think golang using multi-thread to implement goroutine , when some goroutine is blocked in I/O(just like reading a file), other thread go on processing another goroutine . But when there is lots of I/O,I don't think thread is enough, how golang deal with it ?

I read a article http://morsmachine.dk/netpoller , it says “Go gets around this problem by using the asynchronous interfaces that the OS provides, but blocking the goroutines that are performing I/O.” .
Is it like the aio_read ? It is said that there is lots of bugs with the asynchronous interfaces . And I don’t find it in the source code or I just miss it.

As we know , I understand we can use epoll to do asynchronous io for pipes and socket, but epoll can’t be used to reading or writing of regular file. And nodejs use libeio to do this for regular file. I want to know how golang do it in runtime.

答案1

得分: 2

Go语言目前在文件I/O方面没有使用任何特殊的机制。它只是创建一个新的线程(以便为goroutines始终有GOMAXPROCS个线程可用),然后在操作上进行阻塞。我记得在邮件列表上已经有一些关于使用AIO的讨论,但那时候存在很多问题。golang-nuts邮件列表可能是这类问题的更好讨论地点。

英文:

Go doesn't use anything special for file I/O yet. It just creates a new thread (so that there are always GOMAXPROCS threads available for goroutines) and blocks on the operation. I think there already was some discussion going on about using AIO on the mailing list, but there were many problems at that time. The golang-nuts mailing list is probably a better place for this kind of questions.

huangapple
  • 本文由 发表于 2014年8月7日 11:35:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/25173687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定