Skip to content

Commit 17cf677

Browse files
Merge pull request RustPython#1261 from kluid/master
Implemented stdlib function 'os.cpu_count()'
2 parents 63cfc70 + 43c1143 commit 17cf677

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,4 @@ flate2 = { version = "1.0", features = ["zlib"], default-features = false }
7878
libz-sys = "1.0.25"
7979
gethostname = "0.2.0"
8080
subprocess = "0.1.18"
81+
num_cpus = "1.0"

vm/src/stdlib/os.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use std::io::{self, Error, ErrorKind, Read, Write};
66
use std::time::{Duration, SystemTime};
77
use std::{env, fs};
88

9+
use num_cpus;
10+
911
#[cfg(unix)]
1012
use nix::errno::Errno;
1113
#[cfg(all(unix, not(target_os = "redox")))]
@@ -759,6 +761,11 @@ fn os_getpid(vm: &VirtualMachine) -> PyObjectRef {
759761
vm.new_int(pid)
760762
}
761763

764+
fn os_cpu_count(vm: &VirtualMachine) -> PyObjectRef {
765+
let cpu_count = num_cpus::get();
766+
vm.new_int(cpu_count)
767+
}
768+
762769
#[cfg(unix)]
763770
fn os_getppid(vm: &VirtualMachine) -> PyObjectRef {
764771
let ppid = unistd::getppid().as_raw();
@@ -1002,7 +1009,8 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
10021009
"R_OK" => ctx.new_int(4),
10031010
"W_OK" => ctx.new_int(2),
10041011
"X_OK" => ctx.new_int(1),
1005-
"getpid" => ctx.new_rustfunc(os_getpid)
1012+
"getpid" => ctx.new_rustfunc(os_getpid),
1013+
"cpu_count" => ctx.new_rustfunc(os_cpu_count)
10061014
});
10071015

10081016
for support in support_funcs {

0 commit comments

Comments
 (0)