README.md (2351B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Number of CPUs 22 23 > Number of CPUs. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var NUM_CPUS = require( '@stdlib/os/num-cpus' ); 31 ``` 32 33 #### NUM_CPUS 34 35 Number of CPUs. 36 37 ```javascript 38 var n = NUM_CPUS; 39 // returns <number> 40 ``` 41 42 </section> 43 44 <!-- /.usage --> 45 46 <section class="notes"> 47 48 ## Notes 49 50 - In a web browser, the number of CPUs is determined by querying the hardware concurrency [API][hardware-concurrency]. 51 - Otherwise, the number of CPUs is determined via the [os][node-os] module. 52 53 </section> 54 55 <!-- /.notes --> 56 57 <section class="examples"> 58 59 ## Examples 60 61 <!-- run-disable --> 62 63 <!-- eslint-disable no-process-exit --> 64 65 <!-- eslint no-undef: "error" --> 66 67 ```javascript 68 var proc = require( 'process' ); 69 var cluster = require( 'cluster' ); 70 var NUM_CPUS = require( '@stdlib/os/num-cpus' ); 71 72 var i; 73 74 function onTimeout() { 75 proc.exit( 0 ); 76 } 77 78 if ( cluster.isMaster ) { 79 for ( i = 0; i < NUM_CPUS; i++ ) { 80 cluster.fork(); 81 } 82 } else { 83 console.log( 'Worker %s. Process id: %d.', cluster.worker.id, cluster.worker.process.pid ); 84 85 setTimeout( onTimeout, 1000 ); 86 } 87 ``` 88 89 </section> 90 91 <!-- /.examples --> 92 93 * * * 94 95 <section class="cli"> 96 97 ## CLI 98 99 <section class="usage"> 100 101 ### Usage 102 103 ```text 104 Usage: num-cpus [options] 105 106 Options: 107 108 -h, --help Print this message. 109 -V, --version Print the package version. 110 ``` 111 112 </section> 113 114 <!-- /.usage --> 115 116 <section class="examples"> 117 118 ### Examples 119 120 ```bash 121 $ num-cpus 122 <number> 123 ``` 124 125 </section> 126 127 <!-- /.examples --> 128 129 </section> 130 131 <!-- /.cli --> 132 133 <section class="links"> 134 135 [node-os]: https://nodejs.org/api/os.html#os_os_cpus 136 137 [hardware-concurrency]: https://developer.mozilla.org/en-US/docs/Web/API/NavigatorConcurrentHardware/hardwareConcurrency 138 139 </section> 140 141 <!-- /.links -->