dragon7088 2017-08-01 17:06
浏览 26

如何避免遇到最大打开文件数限制

I'm building an application that will be downloading roughly 5000 CSV files concurrently using go routines and plain ol http get requests. Downloading the files in parallel.

I'm currently running into open file limits imposed by OS X.

The CSV files are served over http. Are there any other network protocols that I can use to batch each request into one? I don't have access to the server, so I can't zip them. I'd also prefer not to change the ulimit because once in production, I probably won't have access to that configuration.

  • 写回答

1条回答 默认 最新

  • dsdeeaquu38538545 2017-08-01 17:13
    关注

    You probably want to limit active concurrent requests to a more sensible number than 5000. Possibly spin up 10/20 workers and send individual files to them over a channel.

    The http client should reuse connections for requests, assuming you always read the entire request body, and close it.

    Something like this:

    func main() {
        http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 100
        for i := 0; i < 10; i++ {
            wg.Add(1)
            go worker()
        }
        var csvs = []string{"http://example.com/a.csv", "http://example.com/b.csv"}
        for _, u := range csvs {
            ch <- u
        }
        close(ch)
        wg.Wait()
    }
    
    var ch = make(chan string)
    var wg sync.WaitGroup
    
    func worker() {
        defer wg.Done()
        for u := range ch {
            get(u)
        }
    }
    
    func get(u string) {
        resp, err := http.Get(u)
        //check err here
    
        // make sure we always read rest of body, and close
        defer resp.Body.Close()
        defer io.Copy(ioutil.Discard, resp.Body)
    
        //read and decode / handle it. Make sure to read all of body.
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应