dongxingguo1978 2017-11-22 14:56
浏览 44

创建切片字节goroutine挂起

I have an upload program that I am working on and I am running into an issue. I have n go routines that handle uploading the parts to a big file. Essentially it will split the file into 100MB chunks and upload them concurrently depending on the amount of concurrent processes you specify in the config.

The issue I'm having is when I create a buffer to read the file and upload the make([]byte, 100000000) hangs... but only if it's in a go routine. (I'm using 100000000 to simplify the upload calculations)

Here is an example.

This works: https://play.golang.org/p/tkn8JVir9S

package main

import (
    "fmt"
)

func main() {
    buffer := make([]byte,  100000000)
    fmt.Println(len(buffer))
}

This doesn't: https://play.golang.org/p/H8626OLpqQ

package

 main

import (
    "fmt"
)

func main() {
    go createBuffer()
    for {
    }

}

func createBuffer() {
    buffer := make([]byte, 100000000)
    fmt.Println(len(buffer))
}

It just hangs... I'm not sure if there is a memory constraint for a go routine? I tried to research and see what I could find but nothing. Any thoughts would be appreciated.

EDIT: Thanks everyone for the feedback. I will say I didn't explain the real issue very well and will try to provide more of a holistic view next time. I ended up using a channel to block to keep my goroutines ready for new files to process. This is for a DR backup uploading to a 3rd party all that requires large files to be split into 100mb chunks. I guess I should have been more clear as to the nature of my program.

  • 写回答

2条回答 默认 最新

  • doutenglou6588 2017-11-22 15:31
    关注

    This program hangs because there is an infinite loop in your code. Try running the code just like this to prove it to yourself. The goroutine is not what is causing the hanging.

    func main() {
        for {
        }
    }
    

    If you just want to see fmt.Println(..) print, then I'd recommend having a time.Sleep call or similar.

    If you would like to wait for a bunch of goroutines to complete, then I'd recommend this excellent answer to that exact question.

    评论

报告相同问题?

悬赏问题

  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元