Coroutine implementation method, terminal equipment and storage medium

Through the combination of bytecode weaving technology and coroutine scheduler, the problem of low concurrency processing efficiency in coroutine implementation is solved, more efficient coroutine task switching and state management is realized, and the concurrency processing capability and resource utilization of the program are improved.

CN119988016APending Publication Date: 2025-05-13BEIJING BAIJU YIXING TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510076473.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-01-17
Publication Date
2025-05-13

AI Technical Summary

Technical Problem

The prior art has problems of low concurrency processing efficiency and low resource utilization in coroutine implementation, especially lack of efficient solutions in multi-task switching and state management.

Method used

Bytecode weaving technology, coroutine-related code is added to the original bytecode, and a coroutine scheduler, worker thread pool, and message queue are created to realize the switching and state management of coroutine tasks and improve concurrency processing capabilities.

Benefits of technology

It improves the concurrent processing capability and resource utilization of the program, simplifies the difficulty of writing coroutine code, and realizes more efficient coroutine scheduling and execution processes.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119988016A_ABST
    Figure CN119988016A_ABST
Patent Text Reader

Abstract

The invention relates to a coroutine implementation method, a terminal device and a storage medium, and relates to the technical field of byte code braiding, a coroutine task is determined to be executed, coroutine related codes are added to original byte codes through a byte code braiding technology, and a coroutine scheduler, a worker thread pool and a message queue are created; the coroutine scheduler is used for obtaining a to-be-executed coroutine task from the message queue, the to-be-executed coroutine task is distributed to the worker thread to execute the coroutine task, the coroutine task comprises switching and state management operation on a coroutine, after the coroutine task is executed, the worker thread returns a result to the coroutine scheduler, and the worker thread executes the coroutine task according to the result. And after the coroutine scheduler receives a result returned by the worker thread, carrying out the next operation, judging whether other coroutine tasks continue to be scheduled or not, monitoring whether tasks to be executed exist in the message queue or not, and when all the coroutine tasks are executed, ending the whole coroutine scheduling and executing process.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of bytecode weaving, and more specifically, to a coroutine implementation method, a terminal device and a storage medium. Background Art

[0002] In today's computer science, coroutines, as a program running mechanism, play an important role in concurrent programming. Compared with threads, coroutines provide a lighter-weight concurrency implementation method, so they are also called cooperative multitasking or microthreads. Its characteristic is that it can switch between multiple execution flows in a single thread, and this switching does not cause system call overhead and only requires a very small stack space. By actively giving up control, a coroutine enables another coroutine to start execution immediately, improving the program's operating efficiency and resource utilization. Summary of the invention

[0003] The present invention aims at the technical problems existing in the prior art and provides a coroutine implementation method, a terminal device and a storage medium to solve the problems raised in the above background technology.

[0004] The technical solution of the present invention to solve the above technical problem is as follows: a method for implementing a coroutine, specifically comprising the following steps:

[0005] Step S1, determine to execute the coroutine task, add the coroutine related code to the original bytecode through the bytecode weaving technology, and create a coroutine scheduler, a worker thread pool, and a message queue;

[0006] Step S2: using the coroutine scheduler to obtain the coroutine task to be executed from the message queue, and assigning it to the worker thread to execute the coroutine task, wherein the coroutine task includes switching and state management operations of the coroutine;

[0007] Step S3: After the coroutine task is executed, the worker thread returns the result to the coroutine scheduler. After receiving the result returned by the worker thread, the coroutine scheduler performs the next operation to determine whether to continue scheduling other coroutine tasks.

[0008] Step S4, repeat steps S1 to S3 until all coroutine tasks are completed, and monitor whether there are tasks to be executed in the message queue. When all coroutine tasks are completed, the entire coroutine scheduling and execution process ends.

[0009] In a preferred embodiment, in step S1, a coroutine task is defined, the subcode of the coroutine task is added to the original bytecode through bytecode weaving technology, and a coroutine scheduler, a worker thread pool, and a message queue are created. The specific steps are as follows:

[0010] Step A1: determine to execute the coroutine task, and use bytecode weaving technology to insert coroutine-related codes into the bytecode of the coroutine task, including coroutine switching and coroutine state management;

[0011] Step A2: Create a coroutine scheduler and a thread pool, which are used to manage worker threads of tasks and execute coroutine tasks respectively. The number of worker threads initialized is the number of CPU cores, and all idle threads are added to a ring queue;

[0012] Step A3: Create a message queue to save coroutine tasks waiting to be executed, and initialize a counter to record the number of tasks currently to be executed. When a new task is executed, add the task to the message queue and increment the counter of the task to be executed.

[0013] Step A3, registering the coroutine task: registering the coroutine task to be executed in the message queue, and starting the worker thread to start executing the coroutine task.

[0014] In a preferred embodiment, in step S2, a coroutine scheduler is used to obtain the coroutine task to be executed from the message queue, and the coroutine task is assigned to a worker thread to execute the coroutine task. The coroutine task includes switching and state management operations of the coroutine. The specific steps are as follows:

[0015] Step B1, task scheduling: when the coroutine scheduler obtains the coroutine task to be executed from the message queue, it associates the task with the execution context, and the execution context includes the execution status and stack information of the task;

[0016] Step B2, coroutine switching: Use static code analysis tools to determine potential interruptible points, insert coroutine switching logic at the determined interruptible points, and when these interruptible points are reached, the coroutine scheduler determines the state of the current task according to the execution context and suspends the current task;

[0017] Step B3, coroutine state management: when switching coroutines, save the execution state of the current coroutine and load the execution state of the target coroutine;

[0018] Step B4, resume execution: After the coroutine is switched, the coroutine scheduler resumes the execution state of the suspended task according to the previously saved execution context, thereby implementing the switching and resumption of the coroutine;

[0019] Among them, in the step B2 of coroutine switching, a static code analysis tool is used to determine potential interruptible points, and the logic of coroutine switching is inserted at the determined interruptible points, further comprising the following steps:

[0020] Step B201, formulate I / O operation rules to identify potential interruptible points in the code, wherein the interruptible points represent code fragments that cause coroutine blocking;

[0021] Step B202: Use a static code analysis tool to analyze the project code, run the rules to detect potential interruptible points, and output code snippets that meet the rules and the location where the coroutine switching logic is inserted;

[0022] Step B203: insert the coroutine switching logic at the potential interruptible point according to the result output by the static code analysis tool, wherein the coroutine switching logic includes saving the execution state of the current task, suspending the current task, and selecting the next task to be executed.

[0023] In a preferred embodiment, in step S3, when the coroutine task is executed, the worker thread returns the result to the coroutine scheduler. After receiving the result returned by the worker thread, the coroutine scheduler performs the next operation to determine whether to continue scheduling other coroutine tasks. The specific steps are as follows:

[0024] Step C1, result return: When the worker thread completes a coroutine task, it returns the execution result including the return value and exception information to the coroutine scheduler;

[0025] Step C2, processing the returned result: After receiving the result returned by the worker thread, the coroutine scheduler stores the returned value into the corresponding variable and handles the exception;

[0026] Step C3: When the task execution fails, the error handling logic is triggered to retry the task operation;

[0027] Step C4: When the task is successfully executed, the next task to be executed is selected from the task queue and assigned to an available worker thread;

[0028] Wherein, when the task execution fails, the step C3 triggers the error handling logic to retry the task operation, further comprising the following steps:

[0029] Step C301, capturing exceptions: when an exception occurs during task execution, the worker thread captures the exception and passes the exception information to the coroutine scheduler;

[0030] Step C302, error handling logic: After receiving the message of task execution failure, the coroutine scheduler handles the abnormal situation according to the pre-set error handling logic;

[0031] Step C303: Before retrying the task, set a limit on the number of retries to avoid wasting resources due to infinite retries.

[0032] In a preferred embodiment, in step S4, steps S1 to S3 are repeatedly executed until all coroutine tasks are executed, and the message queue is monitored to see whether there are tasks to be executed to ensure that all coroutine tasks can be executed and completed. When all coroutine tasks are executed, the entire coroutine scheduling and execution process ends. The specific steps are as follows:

[0033] Step D1, monitor task status: during the coroutine execution process, check whether there are tasks to be executed in the message queue by polling the message queue;

[0034] Step D2, judging whether the task is completed: when the counter of the tasks to be executed is reduced to zero, it means that all tasks have been completed;

[0035] Step D3, end the coroutine scheduling: when all tasks are executed, send a termination signal to each coroutine to end the scheduling and execution process of the coroutine;

[0036] Step D4, clean up resources: After the entire coroutine scheduling and execution process is completed, clean up resources to ensure that the program exits normally;

[0037] Wherein, in the step D1, in which the task status is monitored, during the process of the coroutine executing the task, by polling the message queue to check whether there are any tasks to be executed in the message queue, the steps include:

[0038] Step D101, set polling interval: define a time interval for polling the message queue to check whether there are tasks to be executed in the message queue;

[0039] Step D102, start polling: in the main loop of the coroutine execution task, set a timer to make the program perform polling operations at set intervals;

[0040] Step D103, check the message queue: at each polling, check whether there are any tasks to be executed in the message queue by querying the length of the message queue;

[0041] Step D104, processing pending tasks: when it is found that there are still pending tasks in the message queue, the tasks are obtained from the message queue and assigned to idle coroutines for execution.

[0042] Step D105, continue polling: after processing the tasks to be executed, continue to wait for the next polling time to arrive, and check again whether there are any tasks to be executed in the message queue;

[0043] Step D106, end condition: when it is found during the polling process that the message queue is empty and all tasks have been completed, the polling process is ended and the checking of the message queue is stopped.

[0044] The beneficial effects of the present invention are: determining the execution of a coroutine task, adding coroutine-related code to the original bytecode through bytecode weaving technology, and creating a coroutine scheduler, a worker thread pool, and a message queue, using the coroutine scheduler to obtain the coroutine task to be executed from the message queue, and assigning it to the worker thread to execute the coroutine task, wherein the coroutine task includes switching and state management operations on the coroutine, and when the coroutine task is executed, the worker thread returns the result to the coroutine scheduler, and after receiving the result returned by the worker thread, the coroutine scheduler performs the next step of operation to determine whether to continue scheduling other coroutine tasks, and monitors whether there are tasks to be executed in the message queue, and when all coroutine tasks are executed, the entire coroutine scheduling and execution process ends. The present invention simplifies the difficulty of writing code and improves the concurrent processing capability of the program through the bytecode weaving technology during compilation. BRIEF DESCRIPTION OF THE DRAWINGS

[0045] Figure 1 The present invention is a flow chart of the method. DETAILED DESCRIPTION

[0046] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of this application.

[0047] In the description of this application, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of the features. In the description of this application, "plurality" means two or more, unless otherwise clearly and specifically defined.

[0048] In the description of the present application, the term "for example" is used to mean "used as an example, illustration or description". Any embodiment described as "for example" in the present application is not necessarily to be construed as being more preferred or advantageous than other embodiments. The following description is given to enable any technician in the field to implement and use the present invention. In the following description, details are listed for the purpose of explanation. It should be understood that a person of ordinary skill in the art can recognize that the present invention can be implemented without using these specific details. In other examples, well-known structures and processes will not be elaborated in detail to avoid unnecessary details to obscure the description of the present invention. Therefore, the present invention is not intended to be limited to the embodiments shown, but is consistent with the widest scope consistent with the principles and features disclosed in the present application.

[0049] Example 1

[0050] This embodiment provides Figure 1 A coroutine implementation method is shown, which specifically includes the following steps:

[0051] Step S1, determine to execute the coroutine task, add the coroutine related code to the original bytecode through the bytecode weaving technology, and create a coroutine scheduler, a worker thread pool, and a message queue;

[0052] Step S2: using the coroutine scheduler to obtain the coroutine task to be executed from the message queue, and assigning it to the worker thread to execute the coroutine task, wherein the coroutine task includes switching and state management operations of the coroutine;

[0053] Step S3: After the coroutine task is executed, the worker thread returns the result to the coroutine scheduler. After receiving the result returned by the worker thread, the coroutine scheduler performs the next operation to determine whether to continue scheduling other coroutine tasks.

[0054] Step S4, repeat steps S1 to S3 until all coroutine tasks are completed, and monitor whether there are tasks to be executed in the message queue. When all coroutine tasks are completed, the entire coroutine scheduling and execution process ends.

[0055] Preferably, in step S1, a coroutine task is defined, the subcode of the coroutine task is added to the original bytecode through bytecode weaving technology, and a coroutine scheduler, a worker thread pool, and a message queue are created. Through the cooperation of the coroutine task and the coroutine scheduler, multiple coroutine tasks are executed concurrently in the same thread, thereby improving the concurrent performance of the program. The specific steps are as follows:

[0056] Step A1: determine to execute the coroutine task, and use bytecode weaving technology to insert coroutine-related codes into the bytecode of the coroutine task, including coroutine switching and coroutine state management;

[0057] Step A2: Create a coroutine scheduler and a thread pool, which are used to manage worker threads of tasks and execute coroutine tasks respectively. The number of worker threads initialized is the number of CPU cores, and all idle threads are added to a ring queue;

[0058] Step A3: Create a message queue to save coroutine tasks waiting to be executed, and initialize a counter to record the number of tasks currently to be executed. When a new task is executed, add the task to the message queue and increment the counter of the task to be executed.

[0059] Step A3, registering the coroutine task: registering the coroutine task to be executed in the message queue, and starting the worker thread to start executing the coroutine task.

[0060] Preferably, in step S2, a coroutine scheduler is used to obtain the coroutine task to be executed from the message queue, and the coroutine task is assigned to a worker thread to execute the coroutine task, wherein the coroutine task includes switching and state management operations of the coroutine, and the specific steps are as follows:

[0061] Step B1, task scheduling: when the coroutine scheduler obtains the coroutine task to be executed from the message queue, it associates the task with the execution context, and the execution context includes the execution status and stack information of the task;

[0062] Step B2, coroutine switching: Use static code analysis tools to determine potential interruptible points, insert coroutine switching logic at the determined interruptible points, and when these interruptible points are reached, the coroutine scheduler determines the state of the current task according to the execution context and suspends the current task;

[0063] Step B3, coroutine state management: when switching coroutines, save the execution state of the current coroutine and load the execution state of the target coroutine;

[0064] Step B4, resume execution: After the coroutine is switched, the coroutine scheduler resumes the execution state of the suspended task according to the previously saved execution context, thereby implementing the switching and resumption of the coroutine;

[0065] Among them, in the step B2 of coroutine switching, a static code analysis tool is used to determine potential interruptible points, and the logic of coroutine switching is inserted at the determined interruptible points, further comprising the following steps:

[0066] Step B201, formulate I / O operation rules to identify potential interruptible points in the code, wherein the interruptible points represent code fragments that cause coroutine blocking;

[0067] Step B202: Use a static code analysis tool to analyze the project code, run the rules to detect potential interruptible points, and output code snippets that meet the rules and the location where the coroutine switching logic is inserted;

[0068] Step B203: insert the coroutine switching logic at the potential interruptible point according to the result output by the static code analysis tool, wherein the coroutine switching logic includes saving the execution state of the current task, suspending the current task, and selecting the next task to be executed.

[0069] Preferably, in step S3, when the coroutine task is executed, the worker thread returns the result to the coroutine scheduler. After receiving the result returned by the worker thread, the coroutine scheduler performs the next operation to determine whether to continue scheduling other coroutine tasks. The specific steps are as follows:

[0070] Step C1, result return: When the worker thread completes a coroutine task, it returns the execution result including the return value and exception information to the coroutine scheduler;

[0071] Step C2, processing the returned result: After receiving the result returned by the worker thread, the coroutine scheduler stores the returned value into the corresponding variable and handles the exception;

[0072] Step C3: When the task execution fails, the error handling logic is triggered to retry the task operation;

[0073] Step C4: When the task is successfully executed, the next task to be executed is selected from the task queue and assigned to an available worker thread;

[0074] Wherein, when the task execution fails, the step C3 triggers the error handling logic to retry the task operation, further comprising the following steps:

[0075] Step C301, capturing exceptions: when an exception occurs during task execution, the worker thread captures the exception and passes the exception information to the coroutine scheduler;

[0076] Step C302, error handling logic: After receiving the message of task execution failure, the coroutine scheduler handles the abnormal situation according to the pre-set error handling logic;

[0077] Step C303: Before retrying the task, set a limit on the number of retries to avoid wasting resources due to infinite retries.

[0078] Preferably, in step S4, steps S1 to S3 are repeatedly executed until all coroutine tasks are executed, and the message queue is monitored to see whether there are tasks to be executed, to ensure that all coroutine tasks can be executed and completed. When all coroutine tasks are executed, the entire coroutine scheduling and execution process ends. The specific steps are as follows:

[0079] Step D1, monitor task status: during the coroutine execution process, check whether there are tasks to be executed in the message queue by polling the message queue;

[0080] Step D2, judging whether the task is completed: when the counter of the tasks to be executed is reduced to zero, it means that all tasks have been completed;

[0081] Step D3, end the coroutine scheduling: when all tasks are executed, send a termination signal to each coroutine to end the scheduling and execution process of the coroutine;

[0082] Step D4, clean up resources: After the entire coroutine scheduling and execution process is completed, clean up resources to ensure that the program exits normally;

[0083] Wherein, in the step D1, in which the task status is monitored, during the process of the coroutine executing the task, by polling the message queue to check whether there are any tasks to be executed in the message queue, the steps include:

[0084] Step D101, set polling interval: define a time interval for polling the message queue to check whether there are tasks to be executed in the message queue;

[0085] Step D102, start polling: in the main loop of the coroutine execution task, set a timer to make the program perform polling operations at set intervals;

[0086] Step D103, check the message queue: at each polling, check whether there are any tasks to be executed in the message queue by querying the length of the message queue;

[0087] Step D104, processing pending tasks: when it is found that there are still pending tasks in the message queue, the tasks are obtained from the message queue and assigned to idle coroutines for execution.

[0088] Step D105, continue polling: after processing the tasks to be executed, continue to wait for the next polling time to arrive, and check again whether there are any tasks to be executed in the message queue;

[0089] Step D106, end condition: when it is found during the polling process that the message queue is empty and all tasks have been completed, the polling process is ended and the checking of the message queue is stopped.

[0090] It should be noted that in the above embodiments, the description of each embodiment has its own emphasis, and for parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0091] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0092] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded computer, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0093] These computer program instructions may also be stored in a computer readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0094] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.

[0095] Although the preferred embodiments of the present invention have been described, those skilled in the art may make other changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present invention.

[0096] Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.

Claims

1. A coroutine implementation method, characterized in that: The specific steps include: Step S1, determine to execute the coroutine task, add the coroutine related code to the original bytecode through the bytecode weaving technology, and create a coroutine scheduler, a worker thread pool, and a message queue; Step S2: using the coroutine scheduler to obtain the coroutine task to be executed from the message queue, and assigning it to the worker thread to execute the coroutine task, wherein the coroutine task includes switching and state management operations of the coroutine; Step S3: After the coroutine task is executed, the worker thread returns the result to the coroutine scheduler. After receiving the result returned by the worker thread, the coroutine scheduler performs the next operation to determine whether to continue scheduling other coroutine tasks. Step S4, repeat steps S1 to S3 until all coroutine tasks are completed, and monitor whether there are tasks to be executed in the message queue. When all coroutine tasks are completed, the entire coroutine scheduling and execution process ends.

2. A method for implementing a coroutine according to claim 1, characterized in that: In step S1, a coroutine task is defined, the subcode of the coroutine task is added to the original bytecode through bytecode weaving technology, and a coroutine scheduler, a worker thread pool, and a message queue are created. The specific steps are as follows: Step A1: determine to execute the coroutine task, and use the bytecode weaving technology to insert the coroutine code into the bytecode of the coroutine task; Step A2: Create a coroutine scheduler and a thread pool, which are used to manage worker threads of tasks and execute coroutine tasks respectively; Step A3: Create a message queue to save coroutine tasks waiting to be executed, and initialize a counter to record the number of tasks currently to be executed. When a new task is executed, add the task to the message queue and increment the counter of the task to be executed. Step A3, registering the coroutine task: registering the coroutine task to be executed in the message queue, and starting the worker thread to start executing the coroutine task.

3. A coroutine implementation method according to claim 1, characterized in that: In step S2, the coroutine scheduler is used to obtain the coroutine task to be executed from the message queue, and it is assigned to the worker thread to execute the coroutine task. The coroutine task includes switching and state management operations of the coroutine. The specific steps are as follows: Step B1, task scheduling: When the coroutine scheduler obtains the coroutine task to be executed from the message queue, it associates the task with the execution context; Step B2, coroutine switching: Use static code analysis tools to determine interruptible points, insert coroutine switching logic at the determined interruptible points, and when reaching the interruptible points, the coroutine scheduler determines the state of the current task according to the execution context and suspends the current task; Step B3, coroutine state management: when switching coroutines, save the execution state of the current coroutine and load the execution state of the target coroutine; Step B4, resume execution: After the coroutine is switched, the coroutine scheduler resumes the execution state of the suspended task according to the previously saved execution context.

4. A method for implementing a coroutine according to claim 3, characterized in that: In the step B2 of coroutine switching, a static code analysis tool is used to determine an interruptible point, and at the determined interruptible point, the logic of coroutine switching is inserted, further comprising the following steps: Step B201, formulate I / O operation rules to identify potential interruptible points in the code; Step B202: Use a static code analysis tool to analyze the project code, run the rules to detect potential interruptible points, and output code snippets that meet the rules and the location where the coroutine switching logic is inserted; Step B203: insert the logic of coroutine switching at the potential interruptible point according to the result output by the static code analysis tool.

5. A coroutine implementation method according to claim 1, characterized in that: In step S3, when the coroutine task is executed, the worker thread returns the result to the coroutine scheduler. After receiving the result returned by the worker thread, the coroutine scheduler performs the next operation to determine whether to continue scheduling other coroutine tasks. The specific steps are as follows: Step C1, result return: When the worker thread completes a coroutine task, it returns the execution result including the return value and exception information to the coroutine scheduler; Step C2, processing the returned result: After receiving the result returned by the worker thread, the coroutine scheduler stores the returned value and handles the exception; Step C3: When the task execution fails, the error handling logic is triggered to retry the task operation; Step C4: When the task is successfully executed, the next task to be executed is selected from the task queue and assigned to an available worker thread.

6. A coroutine implementation method according to claim 5, characterized in that: When the task execution fails, the step C3 triggers the error handling logic and performs a task retry operation, further comprising the following steps: Step C301, capturing exceptions: when an exception occurs during task execution, the worker thread captures the exception and passes the exception information to the coroutine scheduler; Step C302, error handling logic: After receiving the message of task execution failure, the coroutine scheduler handles the abnormal situation according to the pre-set error handling logic; Step C303: Before retrying the task, set a limit on the number of retries.

7. A coroutine implementation method according to claim 1, characterized in that: In step S4, steps S1 to S3 are repeatedly executed until all coroutine tasks are executed, and the message queue is monitored to see whether there are tasks to be executed. When all coroutine tasks are executed, the entire coroutine scheduling and execution process ends. The specific steps are as follows: Step D1, monitor task status: during the coroutine execution process, check whether there are tasks to be executed in the message queue by polling the message queue; Step D2, judging whether the task is completed: when the counter of the task to be executed is reduced to zero, all tasks have been completed; Step D3, end the coroutine scheduling: when all tasks are executed, send a termination signal to each coroutine to end the scheduling and execution process of the coroutine; Step D4, clean up resources: After the entire coroutine scheduling and execution process is completed, perform resource cleanup.

8. A coroutine-implemented terminal device, characterized in that: The terminal device includes: a coroutine scheduler and a worker thread, and executes instructions in the coroutine scheduler and the worker thread to implement a coroutine implementation method according to any one of claims 1-7.

9. A coroutine implementation storage medium, on which operation data and a computer program are stored, characterized in that: When the computer program is executed, a coroutine implementation method according to any one of claims 1 to 7 is implemented.