In most cases you should not need to poll explicity.
Write a goroutine that blocks while reading the contents of your fd
(file, network, etc...), and then send the data read across a channel.
Under the hood Go will take care of the poll for you.
But if you really need to poll explicitly, I can't help you since I
never had to do that.
--
André Moraes
http://andredevchannel.blogspot.com/
In most cases you should not need to poll explicity.
Write a goroutine that blocks while reading the contents of your fd
(file, network, etc...), and then send the data read across a channel.
Under the hood Go will take care of the poll for you.
But if you really need to poll explicitly, I can't help you since I
never had to do that.
func readFile(f *os.File, _data chan string, _close chan int) {
// read the file contents with f.Read
// process it
// send in the channel.
_data <- "Sample string. You should send your data here"
// when you are done
// send something on the close channel
_close <- 0
}
func main() {
f, err := os.Open("/my/file/path")
if err != nil {
panic(err) // error while opening the file.
}
dataCh := make(chan string) // unbuffered channel.
closeCh := make(chan int) // unbuffered channel.
go readFile(f, dataCh, closeCh) // dont block while reading the file.
for {
select { // this select the statement that has data on the channel.
case s:=<-dataCh
print(s) // print some data read from the file
case <-closeCh // EOF or other error
break
}
}
}
2011/9/22 Vasiliy Tolstov <v.to...@selfip.ru>:
--
André Moraes
http://andredevchannel.blogspot.com/
In Go you normally simply start a goroutine that reads from Stdin and
sends the data over a channel.
Goroutines are cheap.
If you have a specific case that isn't covered by blocking in a go-routine, you can always use the syscall's directly, with a very similar API to what you would do in C.
For example here's the epoll ones in stdlib: https://golang.org/pkg/syscall/#EpollCreate
On 25 Jun 2016 11:08 p.m., "Michael Soulier" <msou...@gmail.com> wrote:
>
> Sure, but when you read and get an EOF you return immediately, so the goroutine would be busy waiting when there's nothing to read, would it not?
>
You'll only get an EOF if the file descriptor has been closed, if it's closed then you're not going to be able to read anything more anyway.
What are you trying to do?
You'll only get an EOF if the file descriptor has been closed, if it's closed then you're not going to be able to read anything more anyway.
What are you trying to do?
You should not be using select in the first place. You are making things
complicated for no reason whatsoever. (If I understand your intention
correctly.)
You should just read from os.Stdin. It will block until there is
something to read (unless you did something special to set it in
non-blocking mode). Check for EOF to know when to quit reading. Use a
goroutine if you need to do something else while the read is blocked. If
you do not need to do something else, you do not need a goroutine. Just
a plain and simple read loop.
Unfortunately not. runsv starts the logger and connects the service's stdout to the logger's stdin. It opens this pipe even if the service isn't up yet, so when you read from stdin, it immediately returns with an EOF, and does not block. That's why svlogd uses poll on stdin.If I were simply accepting input from a piped shell command, you'd be right. I wish I were.
Hmm. Maybe I misunderstand how runsv connects the two. A simple shell test seems to behave more as expected. I'll need to dig.