FAQ
I'm looking for examples of how to cancel a read operation on a file after
a timeout. The reason is that
I'd like to be able to gracefully shutdown a goroutine which is doing IO,
but if it blocks on a read there's
no way I can find that guarantees that goroutine can be shut down.

If you're working with a net.Conn you can just use SetReadDeadline. The
alarm() call on linux is one thing
I've used alarm signals for python: https://gist.github.com/3724043 but
when use cgo to send on alarm
on linux http://play.golang.org/p/VzMEMyYDCQ go automatically restarts the
syscall.

Currently I'm wondering if there's a way to use something in the syscall
package to implement this manually.

Any thoughts?

Thanks

--

Search Discussions

  • Matt Harden at Sep 17, 2012 at 2:14 pm
    You need to register to receive the alarm signal, I think, using
    signal.Notify. You can also set your file descriptor as non-blocking and
    use syscall.Select with a timeout, but that's going to be more work.
    On Friday, September 14, 2012 2:15:19 PM UTC-5, Shane Hansen wrote:

    I'm looking for examples of how to cancel a read operation on a file after
    a timeout. The reason is that
    I'd like to be able to gracefully shutdown a goroutine which is doing IO,
    but if it blocks on a read there's
    no way I can find that guarantees that goroutine can be shut down.

    If you're working with a net.Conn you can just use SetReadDeadline. The
    alarm() call on linux is one thing
    I've used alarm signals for python: https://gist.github.com/3724043 but
    when use cgo to send on alarm
    on linux http://play.golang.org/p/VzMEMyYDCQ go automatically restarts
    the syscall.

    Currently I'm wondering if there's a way to use something in the syscall
    package to implement this manually.

    Any thoughts?

    Thanks
    --
  • Shane Hansen at Sep 18, 2012 at 7:52 pm
    Thanks Matt!

    Sounds like select is the way to go if it *really* matters (which I can't
    think of any good use case where it would at the moment).

    Something else that was extremely interesting that I discovered: If you
    .Close() the file in a separate goroutine, the read continues to
    block on linux (the syscall is interrupted and then resumes) but not on
    other platforms. The implication seems to be that a interrupted
    read syscall can resume on linux even if the file was closed during the
    interruption.

    On Mon, Sep 17, 2012 at 8:14 AM, Matt Harden wrote:

    You need to register to receive the alarm signal, I think, using
    signal.Notify. You can also set your file descriptor as non-blocking and
    use syscall.Select with a timeout, but that's going to be more work.

    On Friday, September 14, 2012 2:15:19 PM UTC-5, Shane Hansen wrote:

    I'm looking for examples of how to cancel a read operation on a file
    after a timeout. The reason is that
    I'd like to be able to gracefully shutdown a goroutine which is doing IO,
    but if it blocks on a read there's
    no way I can find that guarantees that goroutine can be shut down.

    If you're working with a net.Conn you can just use SetReadDeadline. The
    alarm() call on linux is one thing
    I've used alarm signals for python: https://gist.github.**com/3724043<https://gist.github.com/3724043> but
    when use cgo to send on alarm
    on linux http://play.golang.org/**p/VzMEMyYDCQ<http://play.golang.org/p/VzMEMyYDCQ> go
    automatically restarts the syscall.

    Currently I'm wondering if there's a way to use something in the syscall
    package to implement this manually.

    Any thoughts?

    Thanks

    --
    --
  • Larry Clapp at Sep 18, 2012 at 8:51 pm

    On Tuesday, September 18, 2012 3:52:09 PM UTC-4, Shane Hansen wrote:
    Something else that was extremely interesting that I discovered: If you
    .Close() the file in a separate goroutine, the read continues to
    block on linux (the syscall is interrupted and then resumes) but not on
    other platforms.
    That seems like a bug, to me.

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 14, '12 at 7:22p
activeSep 18, '12 at 8:51p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase