tcl - 如何查看tcl中CPU的数量?

标签 tcl

我使用的软件支持多线程和TCL接口(interface)。所以我不知道如何找到TCL中的CPU数量来限制最大线程。

最佳答案

Tcl 中没有内置任何内容;它已经被讨论过,但没有采取行动,因为 CPU 的数量不一定是进程可能使用的数量(到目前为止,这是获取该信息的最相关原因)。也就是说,该信息是可用的。只是不同平台获取信息的方式有所不同。

proc numberOfCPUs {} {
    # Windows puts it in an environment variable
    global tcl_platform env
    if {$tcl_platform(platform) eq "windows"} {
        return $env(NUMBER_OF_PROCESSORS)
    }

    # Check for sysctl (OSX, BSD)
    set sysctl [auto_execok "sysctl"]
    if {[llength $sysctl]} {
        if {![catch {exec {*}$sysctl -n "hw.ncpu"} cores]} {
            return $cores
        }
    }

    # Assume Linux, which has /proc/cpuinfo, but be careful
    if {![catch {open "/proc/cpuinfo"} f]} {
        set cores [regexp -all -line {^processor\s} [read $f]]
        close $f
        if {$cores > 0} {
            return $cores
        }
    }

    # No idea what the actual number of cores is; exhausted all our options
    # Fall back to returning 1; there must be at least that because we're running on it!
    return 1
}

请注意,如果您希望找出要运行的 CPU 数量,那么您真正想要的是 CPU 关联掩码中设置的位数。不幸的是,在大多数平台上检索该信息并非易事。

关于tcl - 如何查看tcl中CPU的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29482303/

相关文章:

java - 如何用Java Hook 到系统级expect?

python - 使用 python 2.7.3 Mac OSX 10.8.2 导入 Tkinter 失败

linux - 如何从期望脚本中转义异常/唯一字符?

tkinter - 隐藏 Tktable 中的字符

python - <event> 和 <<event>> 之间的 Tkinter 区别

sqlite - 从 TCL 在 sqlite IN 语句中使用变量

build - 如何构建gitk?

visual-studio-2017 - 在 Visual Studio 2017 中构建 TclTk 时出错