tencent cloud

Tencent Cloud Super App as a Service

Release Notes and Announcements
Announcement: Tencent Cloud Mini Program Platform Renamed to Tencent Cloud Super App as a Service on January 2, 2025
Console Updates
Android SDK Updates
iOS SDK Updates
Flutter SDK Updates
IDE Updates
Base Library Updates
Product Introduction
Overview
Strengths
Use Cases
Purchase Guide
Billing Overview
Pay-As-You-Go Billing
Renewal Guide
Service Suspension Instructions
Getting Started
Plan Management
Overview
Console Account Management
Storage Configuration
Acceleration Configuration
Branding Configurations
Platform Features
Console Login
Users and Permission System
Mini Program Management
Mini Game Management
Superapp Management
Commercialization
Platform Management
User Management
Team Management
Operations Management
Security Center
Code Integration Guide
Getting Demo and SDK
Android
iOS
Flutter
Superapp Server
GUID Generation Rules
Mini Program Development Guide
Mini Program Introduction and Development Environment
Mini Program Code Composition
Guide
Framework
Components
API
Server Backend
JS SDK
Base Library
IDE Operation Instructions
Mini Game Development Guide
Guide
API
Server Backend
Practice Tutorial
Mini Program Login Practical Tutorial
Mini Program Subscription Message Practical Tutorial
Payment Practical Tutorial
Ad Integration Practical Tutorial
Mini Game Subscription Message Practical Tutorial
API Documentation
History
Introduction
API Category
Making API Requests
Operation Management APIs
User Management APIs
Team Management APIs
Sensitive API-Related APIs
Role Management APIs
Platform Management APIs
Other Console APIs
Mini Program or Mini Game APIs
Management-Sensitive APIs
Global Domain Management APIs
Superapp APIs
Data Types
Agreements
Service Level Agreement
Data Processing and Security Agreement
SDK Privacy Policy Module
SDK Data Processing and Security Agreement Module

Multi-Process Worker

PDF
Focus Mode
Font Size
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.



Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback