# Worker wx.createWorker(string scriptPath, object options)
Start from base library version 1.9.90. Please remaining backward compatible.
with Promise style call: Not supported
Mini Program plugin: Support, need to Mini Program base library version no less than 2.18.1
Create a Worker Thread
# parameter
# string scriptPath
worker Entry fileAbsolute path
# object options
Optional parameters
attribute | type | Default values | Required | Introductions | Minimum version |
---|---|---|---|---|---|
useExperimentalWorker | boolean | false | no | Whether to use an experimental worker. In iOS, experimental worker JS running efficiency than non-experimental worker nearly ten times, such as the need for heavy computing in the worker is recommended to open this option. At the same time, there is a small probability that the experimental worker will be recovered by the system when the system resources are tight, so it is recommended to cooperate worker.onProcessKilled Event, which can be recreated after the worker is reclaimed. | 2.13.0 |
# Return value
# Worker
Worker object
# Note
- Before the interface is used, the app.json (Small game for game.json / Plug-in is plugin.json) In configuration workers Field, representing worker The code root directory.
- scriptPath Is the absolute path to the entry file, and is not / The beginning.
- Currently restricted to creating at most one Worker, create the next Worker Please call before Worker.terminate
- For more details see Multithreading worker guide(Mini Program)、 Multithreading worker guide(Mini games)
# sample code
// Create a normal worker
wx.createWorker('workers/index.js')
function createNewWorker() {
const worker = wx.createWorker('workers/index.js', {
useExperimentalWorker: true
})
// Listen for worker reclaims event
worker.onProcessKilled(() => {
// Recreate a worker
createNewWorker()
})
}
// Create Lab Worker
createNewWorker()