我在OSX上有一个Rust应用程序启动大量线程,如下面的代码所示,但是,在查看我的OSX版本允许通过sysctl kern.num_taskthreads命令创建的最大线程数之后,我可以看出它是kern.num_taskthreads:2048这解释了为什么我不能超过2048个线程.
我如何才能超越这个硬限制?
let threads = 300000; let requests = 1; for _x in 0..threads { println!("{}",_x); let request_clone = request.clone(); let handle = thread::spawn(move || { for _y in 0..requests { request_clone.lock().unwrap().push((request::Request::new(request::Request::create_request()))); } }); child_threads.push(handle); }
解决方法
在开始之前,我建议您阅读有关
C10K problem的内容.当您达到这个规模时,您需要记住更多的事情.
话虽这么说,我建议看看mio ……
a lightweight IO library for Rust with a focus on adding as little overhead as possible over the OS abstractions.
具体来说,mio提供了一个事件循环,它允许您处理大量连接而不会产生线程.不幸的是,我不知道当前支持mio的HTTP库.你可以创建一个并成为Rust社区的英雄!