模塊coroutine
並發控制模塊
引用方法:
1var coroutine = require('coroutine');
對象
Lock
鎖對象,參見 Lock
1Lock coroutine.Lock;
Semaphore
信號量對象,參見 Semaphore
1Semaphore coroutine.Semaphore;
Condition
條件變量對象,參見 Condition
1Condition coroutine.Condition;
Event
事件對象,參見 Event
1Event coroutine.Event;
Worker
獨立線程工作對象,參見 Worker
1Worker coroutine.Worker;
靜態函數
start
啟動一個纖程並返回纖程對象
1
2static Fiber coroutine.start(Function func,
...args);
調用參數:
- func: Function, 制定纖程執行的函數
- args: ..., 可變參數序列,此序列會在纖程內傳遞給函數
返回結果:
- Fiber, 返回纖程對象
parallel
並行執行一組函數,並等待返回
1
2static Array coroutine.parallel(Array funcs,
Integer fibers = -1);
調用參數:
- funcs: Array, 並行執行的函數數組
- fibers: Integer, 限制並發fiber 數量,缺省為-1,啟用與funcs 數量相同fiber
返回結果:
- Array, 返回函數執行結果的數組
並行執行一個函數處理一組數據,並等待返回
1
2
3static Array coroutine.parallel(Array datas,
Function func,
Integer fibers = -1);
調用參數:
- datas: Array, 並行執行的數據數組
- func: Function, 並行執行的函數
- fibers: Integer, 限制並發fiber 數量,缺省為-1,啟用與datas 數量相同fiber
返回結果:
- Array, 返回函數執行結果的數組
並行執行一個函數多次,並等待返回
1
2
3static Array coroutine.parallel(Function func,
Integer num,
Integer fibers = -1);
調用參數:
- func: Function, 並行執行的函數數
- num: Integer, 重複任務數量
- fibers: Integer, 限制並發fiber 數量,缺省為-1,啟用與funcs 數量相同fiber
返回結果:
- Array, 返回函數執行結果的數組
並行執行一組函數,並等待返回
1static Array coroutine.parallel(...funcs);
調用參數:
- funcs: ..., 一組並行執行的函數
返回結果:
- Array, 返回函數執行結果的數組
current
返回當前纖程
1static Fiber coroutine.current();
返回結果:
- Fiber, 當前纖程對象
sleep
暫停當前纖程指定的時間
1static coroutine.sleep(Integer ms = 0) async;
調用參數:
- ms: Integer, 指定要暫停的時間,以毫秒為單位,缺省為0,即有空閒立即回恢復運行
靜態屬性
fibers
Array, 返回當前正在運行的全部fiber 數組
1static readonly Array coroutine.fibers;
spareFibers
Integer, 查詢和設置空閒 Fiber 數量,服務器抖動較大時可適度增加空閒 Fiber數量。缺省為256
1static Integer coroutine.spareFibers;
vmid
Integer, 查詢當前 vm 編號
1static readonly Integer coroutine.vmid;
loglevel
Integer, 修改和查詢本 vm 的輸出級別,用以過濾輸出信息,缺省為 console.NOTSET,全部輸出
1static Integer coroutine.loglevel;