Module process
Process processing module to manage the resources of the current process
Reference method:
1var process = require('process');
Process event
The process module object is EventEmitter For instance, you can respond to process-level events by registering event listeners.
beforeExit event
When fibjs mission has been empty and no additional work is added in, the event beforeExit
is triggered
1process.on('beforeExit', exitCode => {});
Under normal circumstances, if no additional work is added to the task queue, the fibjs process will end. But if the beforeExit
callback listener event bindings, start a new task, such as opening a fiber, then fibjs process will continue to run.
process.exitCodeAs the only parameter passed to the value of beforeExit
the callback function event listener. If the process is about to terminate due to explicit reasons, such as calling directlyprocess.exitOr throw an uncaught exception, the beforeExit
event will not be triggered.
exit event
When fibjs exit, the event exit
will be triggered once all the exit
event binding listener execution is completed, the process will be terminated
1process.on('exit', exitCode => {});
exit
The callback function of the event listener has only one input parameter. The value of this parameter can be process.exitCode The attribute value, or call process.exitWhen the method passed exitCode
value.
Signal event
When the fibjs process receives a signal, it will trigger a signal event. The currently supported signals are SIGINT and SIGTERM. Each event name is represented by the uppercase of the signal name (for example, the event'SIGINT' corresponds to the signal SIGINT).
Signal events are different from other process events. Signal events are preempted. When a signal occurs, regardless of the current ioOperation, or JavaScript operation, will trigger the corresponding event as soon as possible. For example, you can use the following code to interrupt the current application and output the running status:
1
2
3
4
5
6var coroutine = require('coroutine');
process.on('SIGINT', () => {
coroutine.fibers.forEach(f => console.error("Fiber %d:\n%s", f.id, f.stack));
process.exit();
});
The signal names and their meanings are as follows:
- SIGINT: When the terminal is running, it can be supported by all platforms, and it can usually be triggered by CTRL+C.
- SIGTERM: This signal is triggered when the process is killed. Not supported under Windows.
Static function
umask
Change the current umask, Windows does not support this method
1static Integer process.umask(Integer mask);
Call parameters:
- mask: Integer, specify a new mask
Return result:
- Integer, Return to the previous mask
Change the current umask, Windows does not support this method
1static Integer process.umask(String mask);
Call parameters:
- mask: String, specify a new mask, string type octal (eg: "0664")
Return result:
- Integer, Return to the previous mask
Returns the current umask, Windows does not support this method
1static Integer process.umask();
Return result:
- Integer, Returns the current mask value
hrtime
Returns the high-precision time of the system. This time has nothing to do with the current time and is only used for high-precision timing
1static Array process.hrtime(Array diff = []);
Call parameters:
- diff: Array, the initial time for comparison
Return result:
- Array, Returns the timing time, the format is [seconds, nanoseconds]
exit
Exit the current process and return exitCode as the process result
1static process.exit();
Exit the current process and return the result
1static process.exit(Integer code);
Call parameters:
- code: Integer, return process result
cwd
Returns the current working path of the operating system
1static String process.cwd();
Return result:
- String, Return the current system path
chdir
Modify the current working path of the operating system
1static process.chdir(String directory);
Call parameters:
- directory: String, specify the new path to be set
uptime
Query the running time of the running environment, in seconds
1static Number process.uptime();
Return result:
- Number, Returns the value representing the time
cpuUsage
Query the time spent by the current process in the user and system code, the value of which is a microsecond value (one millionth of a second)
1static Object process.cpuUsage(Object previousValue = {});
Call parameters:
- previousValue: Object, specify the time of the last query
Return result:
- Object, Return report containing time
The memory report generates results similar to the following:
1
2
3
4{
"user": 132379,
"system": 50507
}
in:
- user returns the time spent by the process in user code
- system returns the time the process spends in the system code
memoryUsage
Query the memory usage report of the current process
1static Object process.memoryUsage();
Return result:
- Object, Return contains memory report
The memory report generates results similar to the following:
1
2
3
4
5{
"rss": 8622080,
"heapTotal": 4083456,
"heapUsed": 1621800
}
in:
- rss returns the size of physical memory currently occupied by the process
- heapTotal returns the v8 engine heap memory size
- heapUsed returns the size of the heap memory being used by the v8 engine
nextTick
Start a fiber
1
2static process.nextTick(Function func,
...args);
Call parameters:
- func: Function, specify the function to be executed by the fiber
- args: ..., variable parameter sequence, this sequence will be passed to the function in the fiber
binding
Get the internal module of the specified name
1static Value process.binding(String name);
Call parameters:
- name: String, specify the name of the internal module to be queried
Return result:
- Value, Return the specified internal module
Static properties
argv
Array, returns the command line parameters of the current process
1static readonly Array process.argv;
execArgv
Array, returns the special command line parameters of the current process, these parameters are used by fibjs to set the operating environment
1static readonly Array process.execArgv;
version
String, return fibjs version string
1static readonly String process.version;
versions
Object, return version information of fibjs and components
1static readonly Object process.versions;
execPath
String, query the full path of the currently running execution file
1static readonly String process.execPath;
env
Object, query the environment variables of the current process
1static readonly Object process.env;
arch
String, query the current cpu environment, possible results are'amd64','arm','arm64','ia32'
1static readonly String process.arch;
platform
String, query the current platform name, possible results are'darwin','freebsd','linux', or'win32'
1static readonly String process.platform;
pid
Integer, read the id of the process pointed to by the current object
1static readonly Integer process.pid;
ppid
Integer, read the id of the parent process pointed to by the current object
1static readonly Integer process.ppid;
stdin
Stream, Query the standard input object of the current process, in tty In TTYInputStream, Otherwise Stream
1static readonly Stream process.stdin;
stdout
Stream, Query the standard output object of the current process, in tty In TTYOutputStream, Otherwise Stream
1static readonly Stream process.stdout;
stderr
Stream, Query the standard error output object of the current process, in tty In TTYOutputStream, Otherwise Stream
1static readonly Stream process.stderr;
exitCode
Integer, query and set the exit code of the current process
1static Integer process.exitCode;