General task processing method and system based on webworker
By creating a web worker thread pool and a task pool, automatic allocation and management of tasks is realized, and the cumbersome problem of creating exclusive threads in the existing technology is solved, which improves task processing efficiency and computer utilization, and reduces development costs.
Patent Information
- Application Number
- CN202510156479.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-12
- Publication Date
- 2025-07-11
AI Technical Summary
When using web workers to create multi-threaded environments, the existing technology requires manual creation of exclusive threads. The use is cumbersome and inefficient, and the computer computing power cannot be fully utilized, resulting in stuttering task processing and increasing server pressure.
By creating a web worker thread pool and task pool and extracting task processing logic, developers only need to register a task model, use the linkage between thread pool and task pool to automatically allocate and manage child thread processing tasks, and realize parallel execution of tasks and result return.
Improve the efficiency and flexibility of task processing, reduce development costs, ensure that the main thread is not blocked, make full use of computer computing capabilities, and reduce server pressure.
Smart Images

Figure CN120295809A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical fields of task scheduling and task processing. Specifically, it relates to a general task processing method and system based on webworker. Background Art
[0002] With the development of web technology, more and more Internet companies use the B / S architecture to develop their products. And with the increasing complexity of business, many business task processes have been moved from the backend to the frontend. The web frontend plays an important role in task processing.
[0003] Among them, for the task processing of time-consuming services, due to the single-threaded problem of JavaScript, all tasks can only be completed on one thread. If one of the tasks takes a very long time, it will occupy the thread all the time, affecting the execution of other tasks and causing the web page to drop frames, that is, there will be a lag phenomenon, seriously affecting the user experience. In most cases, for the task processing of time-consuming services, it is processed on the backend service, and the web frontend only needs to do simple logic processing. However, in this case of the backend service processing time-consuming services, it increases the pressure on the server side, raises the cost of the server, and does not make full use of the natural distributed ability under the B / S architecture.
[0004] With the enhancement of computer computing power, especially the emergence of multi-core CPUs, single-threading brings great inconvenience and cannot give full play to the computing power of the computer. For this reason, the web worker technology came into being. The role of web worker is to create a multi-threaded environment for JavaScript, allowing the main thread to create web worker child threads and allocate some tasks to the child threads to run. While the main thread is running, the web worker child threads run in the background, and the two do not interfere with each other. When the webworker child threads complete the computing tasks, they communicate with the main thread again to return the results to the main thread. In this multi-threaded environment, some computationally intensive or high-latency time-consuming tasks are borne by the web worker child threads, and the main thread (usually responsible for UI interaction) will be very smooth and will not be blocked or slowed down.
[0005] However, the current technology that uses web worker to create a multi-threaded environment for JavaScript needs to manually create corresponding exclusive web worker child threads every time when processing different time-consuming tasks. It is not general enough, and is rather cumbersome and inefficient to use. Summary of the Invention
[0006] In view of this, the purpose of the present invention is to propose a general task processing method and system based on webworker, extracting the creation and execution of web worker, so that developers only need to focus on writing the processing logic of time-consuming tasks itself, registering the written task processing model at any time period before use, and only need to tell the thread which model to use to process data when using; when there is an idle web worker sub-thread, it will immediately process the task; when the web worker sub-thread is busy processing, the current task will be put into the task pool for queuing; once there is an idle web worker sub-thread, it will take tasks from the task pool for processing, so as to reduce the usage cost of web worker and improve work efficiency.
[0007] The present invention provides a general task processing method based on webworker, including the following steps:
[0008] S1. Create a web worker thread pool, create a worker main thread, create multiple worker sub-threads, and store the multiple worker sub-threads in the web worker thread pool in the form of a queue;
[0009] Specifically, the number of sub-threads can be determined according to the number of cpu threads or the number of creations can be specified manually.
[0010] An example of the web worker thread pool is as follows:
[0011] waitWorker = [worker, worker, worker...]
[0012] S2. Create a task pool, and use a queue or a linked list to put the time-consuming tasks to be executed into the task pool, trigger task execution, and wait for the idle worker sub-threads in the web worker thread pool to execute;
[0013] An example of the task pool is as follows:
[0014]
[0015] Specifically, the web worker thread pool is used to circularly process the tasks in the task pool;
[0016] An example code for creating a task pool for storing tasks waiting to be executed is as follows:
[0017]
[0018] In JavaScript, the characteristics of arrays are utilized to implement a queue structure for creating a task pool, and methods for adding tasks (push), retrieving tasks (shift), and obtaining the current number of tasks (size) are provided.
[0019] S3. Create a task execution model library, pre-register time-consuming tasks to be executed in the task execution model library. When a worker sub-thread executes a task, retrieve the task from the task pool, retrieve the corresponding execution model from the registered task execution model library, use this execution model to execute the task, and return the execution result to the main thread.
[0020] Create a task execution model library. The sample code for presetting the processing logic to be executed is as follows: model-pool.js: export default modelPool = { modelNameA: () => {}, / / Execution logic modelNameB: () => {}, .... }
[0021] S4. Implement the linkage between the thread pool and the task pool. When adding a task, notify the web worker thread pool to retrieve a worker sub-thread to execute the task; when a worker sub-thread finishes executing a task, return the execution result to the main thread. At this time, retrieve another task from the task pool to execute; when there are no tasks in the task pool, put the worker sub-thread into the web worker thread pool.
[0022] Specifically, the main thread worker receives the processing result through the onmessage event and executes the callback handleMessage. Through currying, the callback can obtain the currently executing worker sub-thread and the previously executed task, call the success or failure callback function of the task, and return the execution result to the user; at this time, check whether there are tasks to be executed in the task pool again. If there are no tasks to be executed, put the current work sub-thread into the web worker thread pool; if there are tasks to be executed, retrieve the task and call sendMessage again.
[0023] The sample code is as follows:
[0024]
[0025] The entire scheduling process loops in an event-driven manner. Through the above solution, webworkers can be fully and flexibly used to handle time-consuming tasks.
[0026] Developers only need to focus on the logical processing of time-consuming tasks themselves. Register the written task processing model before use (it can be registered at any time period before use). When using it, they only need to tell the thread which model to use to process data currently. When there is an idle web worker sub-thread, the task will be processed immediately; when the web worker sub-thread is busy, the current task will be put into the task pool for queuing; once there is an idle web worker sub-thread, the task will be taken out from the task pool for processing; this method separates the creation and execution of web workers, and developers only need to focus on the logical writing of the tasks themselves.
[0027] Further, the method of creating multiple worker sub-threads in step S1 includes:
[0028] Use the worker.js file to create worker sub-threads (the created data can be determined by oneself), and save the multiple worker sub-threads in the web worker thread pool using an array.
[0029] The example code is as follows:
[0030]
[0031] Call the cretaWorker method to create worker sub-threads using new MyWorker() and store them in waitWorkers. At the same time, initialize the task pool using new TaskPool();
[0032] Further, the method of putting the time-consuming tasks to be executed into the task pool and triggering task execution in step S2 includes:
[0033] Externally call setWorkerTask to add the task to the task pool and call startWorker to trigger task execution.
[0034] The example code is as follows:
[0035]
[0036] Specifically, when triggering a task, judge whether there is an idle sub-thread in the current thread pool. If not, exit; if there is an idle sub-thread, take out one sub-thread; call sendMessage and pass in the current sub-thread worker and the current task task;
[0037] The example code is as follows:
[0038]
[0039] Further, when the task is executed in the worker sub-thread in the S3 step, the method of taking out the task from the task pool includes:
[0040] Write a general file for the worker.js thread (the main function of the worker.js thread general file is to import the model library), and listen for events triggered by the main thread through the onmessage method;
[0041] The method of taking out the corresponding execution model from the registered task execution model library in the S3 step and using the execution model to execute the task includes: according to the task model identifier, taking out the corresponding execution method from the registered task execution model library and executing the task;
[0042] The method of returning the execution result to the main thread in the S3 step includes: after the execution is completed, returning the execution result to the main thread through the postMessage communication.
[0043] The example code is as follows:
[0044]
[0045] Specifically, bind the onmessage event to the current main thread worker to receive the callback when the worker sub-thread finishes processing the task. Here, the callback handleMessage is curried for subsequent use of the current worker, and then the postMessage event is triggered;
[0046] The example code is as follows:
[0047]
[0048] In the main thread worker, receive the parameters passed in through the postMessage above through the onmessage event, obtain the logic to be executed in the task execution model library using the modelName, and return the execution result through the postMessage event of the worker sub-thread itself after the task execution is completed;
[0049] The example code is as follows:
[0050]
[0051] Further, when adding a task in the S4 step, the method of notifying the web worker thread pool to take out a worker sub-thread to execute the task includes:
[0052] When triggering a task, it is determined whether there is an idle sub-thread in the current web worker thread pool; if there is no idle worker sub-thread, the task is placed in the task pool to wait; if there is an idle worker sub-thread, a worker sub-thread is taken out.
[0053] Further, the method of returning the execution result to the main thread through postMessage communication includes:
[0054] Bind an onmessage event to the current main thread worker to receive the callback when the worker sub-thread finishes processing the task, currying the callback handleMessage for subsequent use of the current worker sub-thread, and then trigger the postMessage event.
[0055] The sample code is as follows:
[0056]
[0057] Further, the method of taking out the corresponding execution method from the registered task execution model library according to the task model identifier includes:
[0058] In the main thread worker, receive the parameters passed in through postMessage communication through the onmessage event, use modelName to obtain the logic to be executed in the task execution model library, and return the execution result through the postMessage event of the worker sub-thread itself after the task execution is completed.
[0059] The sample code is as follows:
[0060]
[0061] The present invention also provides a general task processing system based on webworker, which executes the general task processing method based on webworker as described above, including:
[0062] The web worker thread pool module: used to create a web worker thread pool, create a worker main thread, create multiple worker sub-threads, and store the multiple worker sub-threads in the web worker thread pool in the form of a queue;
[0063] The task pool module: used to create a task pool, and put the time-consuming tasks to be executed into the task pool in the way of a queue or a linked list, trigger task execution, and wait for the idle worker sub-threads in the web worker thread pool to execute;
[0064] Task Execution Model Library Module: It is used to create a task execution model library, pre-register time-consuming tasks that need to be executed into the task execution model library. When a worker sub-thread executes a task, it retrieves the task from the task pool, retrieves the corresponding execution model from the registered task execution model library, uses this execution model to execute the task, and returns the execution result to the main thread;
[0065] ThreadPool and Task Pool Linkage Module: It is used to realize the linkage between the thread pool and the task pool. When adding a task, it notifies the web worker thread pool to retrieve a worker sub-thread to execute the task; when a worker sub-thread finishes executing a task, it returns the execution result to the main thread, and then retrieves another task from the task pool to execute; when there are no tasks in the task pool, it puts the worker sub-thread into the web worker thread pool.
[0066] The present invention also provides a computer-readable storage medium, on which a computer program is stored. When the program is executed by a processor, it realizes the steps of the webworker-based general task processing method as described above.
[0067] The present invention also provides a computer device, which includes a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the program, it realizes the steps of the webworker-based general task processing method as described above.
[0068] Compared with the prior art, the beneficial effects of the present invention are as follows:
[0069] The webworker-based general task processing method and system provided by the present invention cache time-consuming tasks that need to be executed through a task pool; through the web worker thread pool, multiple time-consuming tasks can be processed in parallel simultaneously, thus ensuring the task processing speed; the main thread and the sub-threads communicate through events to ensure continuous task processing; this solution abstracts the creation of web workers and the parallel execution of tasks, and developers only need to focus on writing the processing logic of time-consuming tasks, effectively reducing the usage cost of webworkers and improving work efficiency. Description of the Drawings
[0070] By reading the detailed description of the preferred embodiments below, various other advantages and benefits will become clear to those of ordinary skill in the art. The drawings are only for the purpose of showing the preferred embodiments and are not considered to be a limitation of the present invention.
[0071] In the drawings:
[0072] Figure 1It is the scheduling flowchart of general task processing based on webworker in the embodiment of the present invention;
[0073] Figure 2 It is the flowchart of a general task processing method based on webworker of the present invention;
[0074] Figure 3 It is the schematic diagram of the composition of the computer device in the embodiment of the present invention. Detailed implementation manners
[0075] Here, the exemplary embodiments will be described in detail, and the examples are shown in the drawings. When the following description refers to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. On the contrary, they are merely examples of devices and products consistent with some aspects of the present disclosure as detailed in the appended claims.
[0076] The terms used in the present disclosure are only for the purpose of describing specific embodiments and are not intended to limit the present disclosure. The singular forms "a", "the", and "said" used in the present disclosure and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise. It should also be understood that the term "and / or" used herein refers to and includes any or all possible combinations of one or more of the associated listed items.
[0077] It should be understood that although the terms first, second, third, etc. may be used in the present disclosure to describe various information, such information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of the present disclosure, the first information may also be referred to as the second information, and similarly, the second information may also be referred to as the first information. Depending on the context, the word "if" as used herein may be interpreted as "when" or "while" or "in response to determining".
[0078] The following further details the embodiments of the present invention.
[0079] The embodiment of the present invention provides a general task processing method based on webworker. Refer to Figure 2 As shown, it includes the following steps:
[0080] S1. Create a web worker thread pool, create a worker main thread, create multiple worker sub-threads, and store the multiple worker sub-threads in the web worker thread pool in the form of a queue;
[0081] The web worker thread pool is as follows:
[0082] waitWorker = [worker, worker, worker...]
[0083] Methods for creating multiple worker sub - threads include:
[0084] Use the worker.js file to create worker sub - threads and save multiple worker sub - threads in the web worker thread pool using an array.
[0085] In this embodiment, the cretaWorker method is called to create worker sub - threads using new MyWorker() and store them in waitWorkers. At the same time, a task pool is initialized using new TaskPool().
[0086] S2. Create a task pool. Use a queue or a linked list to put time - consuming tasks to be executed into the task pool, trigger task execution, and wait for idle worker sub - threads in the web worker thread pool to execute;
[0087] The task pool is as follows:
[0088]
[0089] The web worker thread pool is used to process tasks in the task pool cyclically;
[0090] Methods for putting time - consuming tasks to be executed into the task pool and triggering task execution include: externally calling setWorkerTask to add tasks to the task pool and calling startWorker to trigger task execution.
[0091] S3. Create a task execution model library, pre - register time - consuming tasks to be executed in the task execution model library. When a worker sub - thread executes a task, take out the task from the task pool, take out the corresponding execution model from the registered task execution model library, use this execution model to execute the task, and return the execution result to the main thread;
[0092] When a worker sub - thread executes a task, methods for taking out the task from the task pool include: writing a general worker.js thread file and listening for events triggered by the main thread through the onmessage method;
[0093] The method of taking out the corresponding execution model from the registered task execution model library and using this execution model to execute tasks includes: taking out the corresponding execution method from the registered task execution model library according to the task model identifier, and executing the task;
[0094] The method of returning the execution result to the main thread includes: after the execution is completed, returning the execution result to the main thread through postMessage communication.
[0095] The method of returning the execution result to the main thread through postMessage communication includes:
[0096] Bind the onmessage event to the current main thread worker to receive the callback when the worker sub-thread finishes processing the task, currying the callback handleMessage for subsequent use of the current worker sub-thread, and then trigger the postMessage event.
[0097] The method of taking out the corresponding execution method from the registered task execution model library according to the task model identifier includes:
[0098] Receive the parameters passed in through postMessage communication in the main thread worker through the onmessage event, obtain the logic to be executed in the task execution model library using modelName, and return the execution result through the postMessage event of the worker sub-thread itself after the task execution is completed.
[0099] S4. Implement the linkage between the thread pool and the task pool. When a task is added, notify the web worker thread pool to take out a worker sub-thread to execute the task; when a worker sub-thread finishes executing a task, return the execution result to the main thread, and then take another task from the task pool to execute; when there are no tasks in the task pool, put the worker sub-thread into the web worker thread pool.
[0100] The method of notifying the web worker thread pool to take out a worker sub-thread to execute the task when a task is added includes:
[0101] When a task is triggered, judge whether there are idle sub-threads in the current web worker thread pool; if there are no idle worker sub-threads, put the task into the task pool to wait; if there are idle worker sub-threads, take out a worker sub-thread.
[0102] Figure 1Shows the scheduling process of general task processing based on webworker in this embodiment.
[0103] An embodiment of the present invention also provides a general task processing system based on webworker, which executes the general task processing method based on webworker as described above, including:
[0104] Web worker thread pool module: Used to create a web worker thread pool, create a worker main thread, create multiple worker sub-threads, and store the multiple worker sub-threads in the web worker thread pool in the form of a queue;
[0105] Task pool module: Used to create a task pool, and use a queue or a linked list to put the time-consuming tasks to be executed into the task pool, trigger task execution, and wait for the idle worker sub-threads in the web worker thread pool to execute;
[0106] Task execution model library module: Used to create a task execution model library, pre-register the time-consuming tasks to be executed into the task execution model library. When a worker sub-thread executes a task, take out the task from the task pool, take out the corresponding execution model from the registered task execution model library, use this execution model to execute the task, and return the execution result to the main thread;
[0107] Thread pool and task pool linkage module: Used to realize the linkage between the thread pool and the task pool. When adding a task, notify the web worker thread pool to take out a worker sub-thread to execute the task; when a worker sub-thread finishes executing a task, return the execution result to the main thread, and then take another task from the task pool to execute; when there is no task in the task pool, put the worker sub-thread into the web worker thread pool.
[0108] The processing process of an embodiment of the present invention in an actual application includes the following steps:
[0109] (1) Create a task pool for storing tasks waiting to be executed;
[0110] The code is as follows:
[0111]
[0112] In Javascript, use the characteristics of an array to implement a queue structure, which is used to create a task pool and provide methods for adding tasks (push), obtaining tasks (shife), and the current number of tasks (size).
[0113] (2) Create a task execution model library and preset the processing logic to be executed;
[0114]
[0115] (3) Write a common file for the worker.js thread, and listen for events triggered by the main thread through the onmessage method. Retrieve the corresponding execution method from the model library using the task model identifier in the parameters, and then execute it. After the execution is completed, return the result to the main thread through the postMessage communication;
[0116] (4) Create a sub-thread pool. Use the worker.js file in step (3) to create worker sub-threads. The data for creation can be determined by yourself and save it in an array as the thread pool;
[0117] (5) Add a task and trigger task execution; externally call setWorkerTask to add the task to the task pool, and call startWorker to trigger task execution;
[0118] (6) When triggering a task, determine whether there are idle sub-threads in the current thread pool. If not, exit; if there are idle sub-threads, take out one sub-thread; call sendMessage and pass in the current sub-thread worker and the current task task;
[0119] The code is as follows:
[0120]
[0121] (7) Bind the onmessage event to the current main thread worker to receive the callback when the worker sub-thread finishes processing the task. Here, currying the callback handleMessage for subsequent use of the current worker, and then trigger the postMessage event;
[0122] The code is as follows:
[0123]
[0124] (8) In the main thread worker, receive the parameters passed in through postMessage in the above through the onmessage event, use modelName to obtain the logic to be executed in the task execution model library, and return the execution result through the postMessage event of the worker sub-thread itself after the task execution is completed; the main thread receives the result by binding the onmessage event in step (7);
[0125] The code is as follows:
[0126]
[0127] (9) The main thread worker receives the processing result through the onmessage event and executes the callback handleMessage. Due to currying, the callback can obtain the currently executing worker sub-thread and the previously executed task, call the success or failure callback function of the task, and return the execution result to the user. At this time, it is judged again whether there is a task to be executed in the task pool. If there is no task to be executed, the current work sub-thread is put into the web worker thread pool. If there is a task to be executed, the task is taken out and sendMessage is called again, repeating step (7);
[0128] The code is as follows:
[0129]
[0130] The entire scheduling process loops in an event-driven manner, and web workers can be fully and flexibly used to handle time-consuming tasks.
[0131] The embodiment of the present invention further provides a computer device, Figure 3 which is a schematic structural diagram of a computer device provided by the embodiment of the present invention; see the attached drawing Figure 3 As shown, the computer device includes: an input system 23, an output system 24, a memory 22, and a processor 21; the memory 22 is used to store one or more programs; when the one or more programs are executed by the one or more processors 21, the one or more processors 21 implement the webworker-based general task processing method provided in the above embodiment; wherein the input system 23, the output system 24, the memory 22, and the processor 21 can be connected through a bus or other means, Figure 3 taking the connection through the bus as an example.
[0132] The memory 22, as a computable device-readable and writable storage medium, can be used to store software programs and computer-executable programs, such as the program instructions corresponding to the webworker-based general task processing method described in the embodiments of the present invention. The memory 22 mainly includes a program storage area and a data storage area. Among them, the program storage area can store an operating system and application programs required for at least one function. The data storage area can store data created according to the use of the device, etc. In addition, the memory 22 can include high-speed random access memory and can also include non-volatile memory, such as at least one magnetic disk storage device, a flash memory device, or other non-volatile solid-state storage devices. In some instances, the memory 22 can further include a memory remotely set relative to the processor 21, and these remote memories can be connected to the device through a network. Examples of the above network include but are not limited to the Internet, an enterprise intranet, a local area network, a mobile communication network, and combinations thereof.
[0133] The input system 23 can be used to receive input digital or character information and generate key signal inputs related to the user settings and function controls of the device. The output system 24 can include display devices such as a display screen.
[0134] The processor 21 executes various functional applications and data processing of the device by running the software programs, instructions, and modules stored in the memory 22, that is, implements the above-mentioned webworker-based general task processing method.
[0135] The computer device provided above can be used to execute the webworker-based general task processing method provided in the above embodiments, and has corresponding functions and beneficial effects.
[0136] An embodiment of the present invention further provides a storage medium containing computer-executable instructions, and the computer-executable instructions are used to execute the general task processing method based on webworker provided in the above embodiment when executed by a computer processor. The storage medium is any of various types of memory devices or storage devices, and the storage medium includes: installation media, such as CD-ROMs, floppy disks, or tape systems; computer system memories or random access memories, such as DRAM, DDR RAM, SRAM, EDO RAM, Rambus RAM, etc.; non-volatile memories, such as flash memories, magnetic media (such as hard disks or optical storage); registers or other similar types of memory elements, etc.; the storage medium may also include other types of memories or combinations thereof; in addition, the storage medium may be located in a first computer system in which the program is executed, or may be located in a different second computer system, and the second computer system is connected to the first computer system through a network (such as the Internet); the second computer system may provide program instructions to the first computer for execution. The storage medium includes two or more storage media that may reside in different locations (such as in different computer systems connected through a network). The storage medium may store program instructions (such as specifically implemented as a computer program) executable by one or more processors.
[0137] Of course, for a storage medium containing computer-executable instructions provided by an embodiment of the present invention, the computer-executable instructions are not limited to the general task processing method based on webworker described in the above embodiment, and may also execute related operations in the general task processing method based on webworker provided by any embodiment of the present invention.
[0138] So far, the technical solution of the present invention has been described in combination with the preferred embodiments. However, it is easy for those skilled in the art to understand that the protection scope of the present invention is obviously not limited to these specific embodiments. Without departing from the principle of the present invention, those skilled in the art can make equivalent changes or substitutions to the relevant technical features, and the technical solutions after these changes or substitutions will all fall within the protection scope of the present invention.
[0139] The above are only the preferred embodiments of the present invention and are not used to limit the present invention; for those skilled in the art, the present invention may have various changes and variations. Any modification, equivalent substitution, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A general task processing method based on webworker, characterized in that, It includes the following steps: S1. Create a web worker thread pool, create a worker main thread, create multiple worker sub-threads, and store the multiple worker sub-threads in the web worker thread pool in the form of a queue; S2. Create a task pool, and use a queue or a linked list to put the time-consuming tasks to be executed into the task pool, trigger task execution, and wait for the idle worker sub-threads in the web worker thread pool to execute; S3. Create a task execution model library, pre-register the time-consuming tasks to be executed in the task execution model library. When a worker sub-thread executes a task, take out the task from the task pool, take out the corresponding execution model from the registered task execution model library, and use this execution model to execute the task, and return the execution result to the main thread; S4. Realize the linkage between the thread pool and the task pool. When adding a task, notify the web worker thread pool to take out a worker sub-thread to execute the task; when a worker sub-thread finishes executing a task, return the execution result to the main thread, and then take out another task from the task pool to execute; when there is no task in the task pool, put the worker sub-thread into the web worker thread pool.
2. The general task processing method based on webworker according to claim 1, wherein The method of taking out a task from the task pool when the worker sub-thread in step S3 executes the task includes: Write a general worker.js thread file, and listen for events triggered by the main thread through the onmessage method; The method of taking out the corresponding execution model from the registered task execution model library and using this execution model to execute the task in step S3 includes: According to the task model identifier, take out the corresponding execution method from the registered task execution model library and execute the task; The method of returning the execution result to the main thread in step S3 includes: After execution is completed, return the execution result to the main thread through postMessage communication.
3. The general task processing method based on webworker according to claim 1, characterized in that, The method of creating multiple worker sub-threads in step S1 includes: Use the worker.js file to create worker sub-threads, and save the multiple worker sub-threads in the web worker thread pool with an array.
4. The general task processing method based on webworker according to claim 1, characterized in that, The method of putting the time-consuming tasks to be executed into the task pool and triggering task execution in step S2 includes: Externally call setWorkerTask to add the task to the task pool, and call startWorker to trigger task execution.
5. The general task processing method based on webworker according to claim 1, wherein The method of notifying the web worker thread pool to take out a worker sub-thread to execute the task when adding a task in step S4 includes: When triggering a task, judge whether there are idle sub-threads in the current web worker thread pool; if there are no idle worker sub-threads, put the task into the task pool and wait; if there are idle worker sub-threads, take out a worker sub-thread.
6. The general task processing method based on webworker according to claim 2, wherein, The method of returning the execution result to the main thread through postMessage communication includes: Bind the onmessage event to the current main thread worker to receive the callback when the worker sub-thread finishes processing the task. Curry the callback handleMessage for subsequent use of the current worker sub-thread, and then trigger the postMessage event.
7. The general task processing method based on webworker according to claim 4, characterized in that, The method of taking out the corresponding execution method from the registered task execution model library according to the task model identifier includes: Receive the parameters passed in through postMessage communication in the main thread worker through the onmessage event. Use modelName to obtain the logic to be executed in the task execution model library. After the task execution is completed, return the execution result through the postMessage event of the worker sub-thread itself.
8. A general task processing system based on webworker, characterized in that, Execute the webworker-based general task processing method according to any one of claims 1-7, including: Web worker thread pool module: used to create a web worker thread pool, create a worker main thread, create multiple worker sub-threads, and store the multiple worker sub-threads in the web worker thread pool in the form of a queue; Task pool module: used to create a task pool, and put the time-consuming tasks to be executed into the task pool in the form of a queue or a linked list, trigger task execution, and wait for the idle worker sub-threads in the web worker thread pool to execute; Task execution model library module: used to create a task execution model library, pre-register the time-consuming tasks to be executed in the task execution model library. When the worker sub-thread executes the task, take out the task from the task pool, take out the corresponding execution model from the registered task execution model library, and use this execution model to execute the task, and return the execution result to the main thread; Thread pool and task pool linkage module: used to realize the linkage between the thread pool and the task pool. When adding a task, notify the web worker thread pool to take out a worker sub-thread to execute the task; when a worker sub-thread finishes executing the task, return the execution result to the main thread, and then take another task from the task pool to execute; when there is no task in the task pool, put the worker sub-thread into the web worker thread pool.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it realizes the steps of the webworker-based general task processing method according to any one of claims 1-7.
10. A computer device, the computer device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, When the processor executes the program, it realizes the steps of the webworker-based general task processing method according to any one of claims 1-7.