Module coroutine
Concurrency control module
coroutineThe module provides an fiberAPI for controlling the order of execution. It allows developers to switch manually fiber, enabling collaborative multitasking. coroutineAn important function of the module is sleepthe function, which allows the currently executing fibermodule to give up the CPU and let other fibermodules run.
Here is a simple example code that demonstrates how to use coroutinethe module:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16const coroutine = require('coroutine');
function foo() {
    console.log('start foo');
    coroutine.sleep(1000); // enter sleep mode
    console.log('end foo');
}
function bar() {
    console.log('start bar');
    coroutine.sleep(2000);
    console.log('end bar');
}
coroutine.start(foo);
coroutine.start(bar);
In the code above, we define two functions fooand and barthen use coroutine.startthe function to start both fiber. In each fiber, we use coroutine.sleepthe function to free up the CPU and let others fiberrun.
object
Lock
lock object, seeLock
1Lock coroutine.Lock;
Semaphore
semaphore object, seeSemaphore
1Semaphore coroutine.Semaphore;
Condition
condition variable object, seeCondition
1Condition coroutine.Condition;
Event
event object, seeEvent
1Event coroutine.Event;
Worker
Independent thread work object, seeWorker
1Worker coroutine.Worker;
static function
start
Starts a fiber and returns the fiber object
1
2static Fiber coroutine.start(Function func,
    ...args);
Call parameters:
- func: Function, specify the function executed by the fiber
- args: ..., a variadic sequence of arguments that will be passed to the function within the fiber
Return results:
- Fiber, returns the fiber object
parallel
Execute a set of functions in parallel and wait for return
1
2static Array coroutine.parallel(Array funcs,
    Integer fibers = -1);
Call parameters:
- funcs: Array, array of functions executed in parallel
- fibers: Integer, limits the number of concurrent fibers, the default is -1, enables the same number of fibers as funcs
Return results:
- Array, returns an array of function execution results
Execute a function in parallel to process a set of data and wait for the return
1
2
3static Array coroutine.parallel(Array datas,
    Function func,
    Integer fibers = -1);
Call parameters:
- datas: Array, data array for parallel execution
- func: Function, function executed in parallel
- fibers: Integer, limits the number of concurrent fibers, the default is -1, enables the same number of fibers as datas
Return results:
- Array, returns an array of function execution results
Execute a function multiple times in parallel and wait for return
1
2
3static Array coroutine.parallel(Function func,
    Integer num,
    Integer fibers = -1);
Call parameters:
- func: Function, the number of functions executed in parallel
- num: Integer, number of repeated tasks
- fibers: Integer, limits the number of concurrent fibers, the default is -1, enables the same number of fibers as funcs
Return results:
- Array, returns an array of function execution results
Execute a set of functions in parallel and wait for return
1static Array coroutine.parallel(...funcs);
Call parameters:
- funcs: ..., a set of functions executed in parallel
Return results:
- Array, returns an array of function execution results
current
Returns the current fiber
1static Fiber coroutine.current();
Return results:
- Fiber, the current fiber object
sleep
Pause the current fiber for the specified time
1static coroutine.sleep(Integer ms = 0) async;
Call parameters:
- ms: Integer, specifies the time to pause, in milliseconds, the default is 0, that is, the operation will be resumed immediately when there is time.
static properties
fibers
Array, returns all currently running fiber arrays
1static readonly Array coroutine.fibers;
spareFibers
Integer, query and set idleFiberQuantity, when server jitter is large, idle can be increased appropriatelyFiberquantity. Default is 256
1static Integer coroutine.spareFibers;
vmid
Integer, query the currentvmserial number
1static readonly Integer coroutine.vmid;
loglevel
Integer, modify and query thisvmThe output level is used to filter output information. The default isconsole.NOTSET, output all
1static Integer coroutine.loglevel;
