小编use*_*965的帖子

Golang http server 在启动无限循环的goroutine时阻塞

正如我从 golang 文档中了解到的,如果我将 runtime.GOMAXPROCS(8) 设置为 8 核(intel i7)的 cpu,然后启动一个无限循环的 goroutine,其他 gorutines 不应该被阻塞,因为有足够的线程和 goprocs。但是在使用 net/http 包时就不是这样了,一个无限循环的 goroutine 会在几次调用后阻塞 http 服务器。任何人都可以帮助解释原因吗?

  1. 如果我注释“无限循环”这一行,在服务器之后启动客户端,客户端将输出1000个星号;但是如果我启用 goroutine,客户端会在打印几个星号后阻塞
  2. 我已经尝试在 goroutine 中添加 runtime.LockOSThread() ,似乎不起作用
  3. 我的环境:osx 10.10, go version go1.3.1 darwin/amd64

服务器代码:

package main

import (
    "fmt"
    "log"
    "net/http"
    "runtime"
)

func myHandler(w http.ResponseWriter, req *http.Request) {
    w.Write([]byte("hello"))
}

func infiniteloop() {
    for {

    }
}

func main() {
    // set max procs for multi-thread executing
    runtime.GOMAXPROCS(runtime.NumCPU())

    // print GOMAXPROCS=8 on my computer
    fmt.Println("GOMAXPROCS=", runtime.GOMAXPROCS(-1))
    http.Handle("/", http.HandlerFunc(myHandler))

    // uncomment …
Run Code Online (Sandbox Code Playgroud)

http block go goroutine

5
推荐指数
1
解决办法
2795
查看次数

在运行时期间可以移动所有权时,Rust编译器如何知道何时调用drop?

根据Rust编程语言:

在Rust中,您可以指定在值超出范围时运行特定的代码位,编译器将自动插入此代码

程序员不应该显式地释放资源(dropDrop特征中调用函数),drop每当所有者超出范围时Rust都会调用,这在编译期间完成,但是drop如果它依赖于Rust,Rust怎么可能知道何时调用关于运行时信息?

extern crate rand;
use rand::Rng;

struct Foo {}

impl Drop for Foo {
    fn drop(&mut self) {
        println!("drop occurs");
    }
}

fn main() {
    let foo = Foo {};
    if rand::thread_rng().gen() {
        let _t = foo; // move foo to _t
    } //   1) drop occurs here if random bool is true
} //       2) drop occurs here if random bool …
Run Code Online (Sandbox Code Playgroud)

release ownership rust

5
推荐指数
1
解决办法
138
查看次数

标签 统计

block ×1

go ×1

goroutine ×1

http ×1

ownership ×1

release ×1

rust ×1