FAQ
code first
http://play.golang.org/p/YldOJZNg1a

its been report written 2 times, but after 2 seconds tick the main function
can not read the content of the file

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Davy zhang at Mar 7, 2013 at 5:45 am
    but when I put

    time.Sleep(1 * time.Second)

    after the

    <-time.Tick(2 * time.Second)


    the file can be read after that, so is there explanation ?
    On Thursday, March 7, 2013 1:42:21 PM UTC+8, davy zhang wrote:

    code first
    http://play.golang.org/p/YldOJZNg1a

    its been report written 2 times, but after 2 seconds tick the main
    function can not read the content of the file
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Davy zhang at Mar 7, 2013 at 5:57 am
    I used sync and os.O_SYNC

    http://play.golang.org/p/q5SA0eV2pi

    but the result is the same

    but when manually open the file using less /tmp/filesave

    the content is there! So the file is written actually but the problem is
    the way I read it?
    On Thursday, March 7, 2013 1:45:00 PM UTC+8, davy zhang wrote:

    but when I put

    time.Sleep(1 * time.Second)

    after the

    <-time.Tick(2 * time.Second)


    the file can be read after that, so is there explanation ?
    On Thursday, March 7, 2013 1:42:21 PM UTC+8, davy zhang wrote:

    code first
    http://play.golang.org/p/YldOJZNg1a

    its been report written 2 times, but after 2 seconds tick the main
    function can not read the content of the file
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tamás Gulácsi at Mar 7, 2013 at 6:14 am
    It works for me
    gthomas@waterhouse /tmp % go test -v x_test.go
    warning: building out-of-date packages:
    flag
    text/tabwriter
    runtime/pprof
    testing
    installing these packages with 'go test -i x_test.go' will speed future
    tests.

    === RUN TestStoreFileSave
    updated value 12345789 /tmp/filesave
    written 8
    updated value 12345789 /tmp/filesave
    written 8
    12345789
    --- PASS: TestStoreFileSave (2.00 seconds)
    PASS
    ok command-line-arguments 2.018s

    gthomas@waterhouse /tmp % go version
    go version devel +08dc15884e88 Mon Feb 25 08:29:46 2013 -0800 linux/amd64

    GThomas

    2013. március 7., csütörtök 6:57:36 UTC+1 időpontban davy zhang a
    következőt írta:
    I used sync and os.O_SYNC

    http://play.golang.org/p/q5SA0eV2pi

    but the result is the same

    but when manually open the file using less /tmp/filesave

    the content is there! So the file is written actually but the problem is
    the way I read it?
    On Thursday, March 7, 2013 1:45:00 PM UTC+8, davy zhang wrote:

    but when I put

    time.Sleep(1 * time.Second)

    after the

    <-time.Tick(2 * time.Second)


    the file can be read after that, so is there explanation ?
    On Thursday, March 7, 2013 1:42:21 PM UTC+8, davy zhang wrote:

    code first
    http://play.golang.org/p/YldOJZNg1a

    its been report written 2 times, but after 2 seconds tick the main
    function can not read the content of the file
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Davy zhang at Mar 7, 2013 at 6:16 am
    I am using the stable version is that the main reason ?

    go version go1.0.3

    On Thursday, March 7, 2013 2:14:19 PM UTC+8, Tamás Gulácsi wrote:

    It works for me
    gthomas@waterhouse /tmp % go test -v x_test.go
    warning: building out-of-date packages:
    flag
    text/tabwriter
    runtime/pprof
    testing
    installing these packages with 'go test -i x_test.go' will speed future
    tests.

    === RUN TestStoreFileSave
    updated value 12345789 /tmp/filesave
    written 8
    updated value 12345789 /tmp/filesave
    written 8
    12345789
    --- PASS: TestStoreFileSave (2.00 seconds)
    PASS
    ok command-line-arguments 2.018s

    gthomas@waterhouse /tmp % go version
    go version devel +08dc15884e88 Mon Feb 25 08:29:46 2013 -0800 linux/amd64

    GThomas

    2013. március 7., csütörtök 6:57:36 UTC+1 időpontban davy zhang a
    következőt írta:
    I used sync and os.O_SYNC

    http://play.golang.org/p/q5SA0eV2pi

    but the result is the same

    but when manually open the file using less /tmp/filesave

    the content is there! So the file is written actually but the problem is
    the way I read it?
    On Thursday, March 7, 2013 1:45:00 PM UTC+8, davy zhang wrote:

    but when I put

    time.Sleep(1 * time.Second)

    after the

    <-time.Tick(2 * time.Second)


    the file can be read after that, so is there explanation ?
    On Thursday, March 7, 2013 1:42:21 PM UTC+8, davy zhang wrote:

    code first
    http://play.golang.org/p/YldOJZNg1a

    its been report written 2 times, but after 2 seconds tick the main
    function can not read the content of the file
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Davy zhang at Mar 7, 2013 at 6:25 am
    Thanks for the test, but could you run few more times? This test sometimes
    is right, but not always. Thanks again.

    On Thu, Mar 7, 2013 at 2:14 PM, Tamás Gulácsi wrote:

    It works for me
    gthomas@waterhouse /tmp % go test -v x_test.go
    warning: building out-of-date packages:
    flag
    text/tabwriter
    runtime/pprof
    testing
    installing these packages with 'go test -i x_test.go' will speed future
    tests.

    === RUN TestStoreFileSave
    updated value 12345789 /tmp/filesave
    written 8
    updated value 12345789 /tmp/filesave
    written 8
    12345789
    --- PASS: TestStoreFileSave (2.00 seconds)
    PASS
    ok command-line-arguments 2.018s

    gthomas@waterhouse /tmp % go version
    go version devel +08dc15884e88 Mon Feb 25 08:29:46 2013 -0800 linux/amd64

    GThomas

    2013. március 7., csütörtök 6:57:36 UTC+1 időpontban davy zhang a
    következőt írta:
    I used sync and os.O_SYNC

    http://play.golang.org/p/**q5SA0eV2pi<http://play.golang.org/p/q5SA0eV2pi>

    but the result is the same

    but when manually open the file using less /tmp/filesave

    the content is there! So the file is written actually but the problem is
    the way I read it?
    On Thursday, March 7, 2013 1:45:00 PM UTC+8, davy zhang wrote:

    but when I put

    time.Sleep(1 * time.Second)

    after the

    <-time.Tick(2 * time.Second)


    the file can be read after that, so is there explanation ?
    On Thursday, March 7, 2013 1:42:21 PM UTC+8, davy zhang wrote:

    code first
    http://play.golang.org/p/**YldOJZNg1a<http://play.golang.org/p/YldOJZNg1a>

    its been report written 2 times, but after 2 seconds tick the main
    function can not read the content of the file
    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/LoURp3_SYoI/unsubscribe?hl=en-US
    .
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Davy zhang at Mar 7, 2013 at 7:04 am
    I tested using
    go version devel +131f8c8eb722 Wed Mar 06 16:52:03 2013 -0800 darwin/amd64

    and there's no real improvement, it did printed the content at first time
    but after that, the result is unstable, sometimes right, sometimes nothing
    printed
    On Thursday, March 7, 2013 2:25:20 PM UTC+8, davy zhang wrote:

    Thanks for the test, but could you run few more times? This test sometimes
    is right, but not always. Thanks again.


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gulácsi Tamás at Mar 7, 2013 at 7:24 am
    Works consistently.
    Just for the record, my /tmp is a tmpfs...

    Questions:
    1. why do you open the file with truncating in each goroutine, and not
    close the file handler at the end?
    2. why don't you synchronize betweem the trunncates and the read? If you
    read just when the goroutine opens (ant truncates) the file, than you will
    read zero length.
    3. what do you test here? Two instances of time.Tick don't happen at once.

    GThomas


    2013/3/7 davy zhang <davyzhang@gmail.com>
    Thanks for the test, but could you run few more times? This test sometimes
    is right, but not always. Thanks again.

    On Thu, Mar 7, 2013 at 2:14 PM, Tamás Gulácsi wrote:

    It works for me
    gthomas@waterhouse /tmp % go test -v x_test.go
    warning: building out-of-date packages:
    flag
    text/tabwriter
    runtime/pprof
    testing
    installing these packages with 'go test -i x_test.go' will speed future
    tests.

    === RUN TestStoreFileSave
    updated value 12345789 /tmp/filesave
    written 8
    updated value 12345789 /tmp/filesave
    written 8
    12345789
    --- PASS: TestStoreFileSave (2.00 seconds)
    PASS
    ok command-line-arguments 2.018s

    gthomas@waterhouse /tmp % go version
    go version devel +08dc15884e88 Mon Feb 25 08:29:46 2013 -0800 linux/amd64

    GThomas

    2013. március 7., csütörtök 6:57:36 UTC+1 időpontban davy zhang a
    következőt írta:
    I used sync and os.O_SYNC

    http://play.golang.org/p/**q5SA0eV2pi<http://play.golang.org/p/q5SA0eV2pi>

    but the result is the same

    but when manually open the file using less /tmp/filesave

    the content is there! So the file is written actually but the problem is
    the way I read it?
    On Thursday, March 7, 2013 1:45:00 PM UTC+8, davy zhang wrote:

    but when I put

    time.Sleep(1 * time.Second)

    after the

    <-time.Tick(2 * time.Second)


    the file can be read after that, so is there explanation ?
    On Thursday, March 7, 2013 1:42:21 PM UTC+8, davy zhang wrote:

    code first
    http://play.golang.org/p/**YldOJZNg1a<http://play.golang.org/p/YldOJZNg1a>

    its been report written 2 times, but after 2 seconds tick the main
    function can not read the content of the file
    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/LoURp3_SYoI/unsubscribe?hl=en-US
    .
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Davy zhang at Mar 7, 2013 at 9:18 am
    I get it, there's a race condition here, I didn't realize it at the first
    place. Your point 2 is the main reason of my problem. Thanks so much for
    helping me out
    The test may seemed weird, I know. But the real case I am working on is to
    save mem content to disk at certain time, so I need to update the file
    (truncating it) every-time I open it and write to it.
    for your point3 , I just write a test code to test if the content been
    successfully written, maybe time.Sleep() more suitable ?
    On Thursday, March 7, 2013 3:24:05 PM UTC+8, Tamás Gulácsi wrote:

    Works consistently.
    Just for the record, my /tmp is a tmpfs...

    Questions:
    1. why do you open the file with truncating in each goroutine, and not
    close the file handler at the end?
    2. why don't you synchronize betweem the trunncates and the read? If you
    read just when the goroutine opens (ant truncates) the file, than you will
    read zero length.
    3. what do you test here? Two instances of time.Tick don't happen at once.

    GThomas


    2013/3/7 davy zhang <davy...@gmail.com <javascript:>>
    Thanks for the test, but could you run few more times? This test
    sometimes is right, but not always. Thanks again.


    On Thu, Mar 7, 2013 at 2:14 PM, Tamás Gulácsi <tgula...@gmail.com<javascript:>
    wrote:
    It works for me
    gthomas@waterhouse /tmp % go test -v x_test.go
    warning: building out-of-date packages:
    flag
    text/tabwriter
    runtime/pprof
    testing
    installing these packages with 'go test -i x_test.go' will speed future
    tests.

    === RUN TestStoreFileSave
    updated value 12345789 /tmp/filesave
    written 8
    updated value 12345789 /tmp/filesave
    written 8
    12345789
    --- PASS: TestStoreFileSave (2.00 seconds)
    PASS
    ok command-line-arguments 2.018s

    gthomas@waterhouse /tmp % go version
    go version devel +08dc15884e88 Mon Feb 25 08:29:46 2013 -0800 linux/amd64

    GThomas

    2013. március 7., csütörtök 6:57:36 UTC+1 időpontban davy zhang a
    következőt írta:
    I used sync and os.O_SYNC

    http://play.golang.org/p/**q5SA0eV2pi<http://play.golang.org/p/q5SA0eV2pi>

    but the result is the same

    but when manually open the file using less /tmp/filesave

    the content is there! So the file is written actually but the problem
    is the way I read it?
    On Thursday, March 7, 2013 1:45:00 PM UTC+8, davy zhang wrote:

    but when I put

    time.Sleep(1 * time.Second)

    after the

    <-time.Tick(2 * time.Second)


    the file can be read after that, so is there explanation ?
    On Thursday, March 7, 2013 1:42:21 PM UTC+8, davy zhang wrote:

    code first
    http://play.golang.org/p/**YldOJZNg1a<http://play.golang.org/p/YldOJZNg1a>

    its been report written 2 times, but after 2 seconds tick the main
    function can not read the content of the file
    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/LoURp3_SYoI/unsubscribe?hl=en-US
    .
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tamás Gulácsi at Mar 7, 2013 at 9:28 am
    No, Tick is ok, but you have to signal the reader that the writing isdone. Can be a mutex, or a channel.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Davy zhang at Mar 7, 2013 at 9:32 am
    You'r totally right, I gonna add a mutex there. Thanks again
    On Thursday, March 7, 2013 5:28:11 PM UTC+8, Tamás Gulácsi wrote:

    No, Tick is ok, but you have to signal the reader that the writing isdone.
    Can be a mutex, or a channel.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jan Mercl at Mar 7, 2013 at 7:27 am

    On Thu, Mar 7, 2013 at 6:42 AM, davy zhang wrote:
    code first
    http://play.golang.org/p/YldOJZNg1a

    its been report written 2 times, but after 2 seconds tick the main function
    can not read the content of the file
    Your OS has a write cache and your program has a (file content) race
    condition. Use http://golang.org/pkg/os/#File.Sync.

    -j

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 7, '13 at 5:42a
activeMar 7, '13 at 9:32a
posts12
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase