tencent cloud

All product documents
Tencent Cloud Super App as a Service
Multi-process Worker
Last updated: 2025-03-25 18:15:55
Multi-process Worker
Last updated: 2025-03-25 18:15:55
In gaming, every frame of 16ms is extremely valuable. Tasks that can be processed asynchronously should be run in a Worker, which can then return the results to the main thread once completed.
Workers operate in a separate global context and thread, and cannot directly call methods from the main thread.
Data transfer between the Worker and the main thread is done using Worker.postMessage to send data, and Worker.onMessage to receive data. The data is copied, not shared directly.

Directions

Step 1. Configure Worker information

In game.json , you can specify the directory where Worker code is located. All JavaScript code in this directory will be bundled into a single JS file:
Configuration example:
{
"workers": "workers"
}
With this configuration, all JS files in the workers directory will be bundled into one JS file and included as part of the mini game's initial package.
The initial package size is limited (currently 4 MB). To prevent Worker code from taking up space in the initial package, starting from base library v2.0.10, Worker code can be packaged as a subpackage (requires the latest version of the developer tools).
Example of configuring Worker code as a subpackage:
{
"workers": {
"path": "workers",
"isSubpackage": true // true means the Worker is packaged as a subpackage. Default is false. Setting false is equivalent to { "workers": "workers" }
}
}

Step 2. Add Worker code files

Based on the configuration in step 1, create the following entry files in the code directory:
workers/request/index.js
workers/request/utils.js
workers/response/index.js
Once added, the directory structure should look like this:
├── game.js
├── game.json
├── project.config.json
└── workers
├── request
│ ├── index.js
│ └── utils.js
└── response
└── index.js

Step 3. Write Worker Code

In workers/request/index.js , write the Worker response code.
const utils = require('./utils')
// In the Worker thread execution context, a global worker object is exposed. Use worker.onMessage/postMessage directly
worker.onMessage(function (res) {
console.log(res)
})

Step 4. Initialize Worker in the main thread

In the main thread code game.js, initialize the Worker.
const worker = wx.createWorker('workers/request/index.js') // Specify the Worker entry file path, absolute path
Starting from base library v2.0.10, if the Worker code is configured as a subpackage, you need to download the Worker code using wx.preDownloadSubpackage before initializing the Worker.
var task = wx.preDownloadSubpackage({
packageType: "workers",
success(res) {
console.log("load worker success", res)
var worker = wx.createWorker("workers/request/index.js") // Create Worker If the Worker subpackage is not fully downloaded, calling createWorker will result in an error
},
fail(res) {
console.log("load worker fail", res)
}
})

task.onProgressUpdate(res => {
console.log(res.progress) // Monitor download progress using onProgressUpdate
console.log(res.totalBytesWritten)
console.log(res.totalBytesExpectedToWrite)
})

Step 5. Send messages from main thread to Worker

worker.postMessage({
msg: 'hello worker'
})
For other APIs of the Worker object, refer to the Worker API Description.

Note

The maximum concurrent number of Workers is limited to 1. Use Worker.terminate() to end the current Worker before creating another.
Code inside the Worker can only require files within the specified Worker path and cannot reference files from other paths.
The Worker entry file is specified when calling createWorker, allowing developers to dynamically specify the Worker entry file.
The Worker does not support the wx series of APIs.
Sending messages between Workers is not supported.
Only JS files are allowed inside the Worker directory; other types of static files must be placed outside the Worker directory.


Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 available.

7x24 Phone Support