Business processing method and device, electronic equipment and storage medium

By setting the upper limit of execution time for the target coroutine in the operating system, the hunger problem caused by long-term suspension of coroutines is solved, and the business response speed is improved.

CN119415191AInactive Publication Date: 2025-02-11BEIJING DIGITAL INFORMATION CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202510023716.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-01-07
Publication Date
2025-02-11
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

In a multitasking environment, coroutines cause hunger due to long-term hangs, resulting in slow service response.

Method used

By setting the upper limit of the execution time of the target coroutine (first preset time), it is ensured that any business execution coroutine must give up execution rights after the operation reaches the upper limit of the time, thereby avoiding coroutine hunger.

Benefits of technology

It effectively alleviates coroutine hunger, improves the overall business response speed, and ensures that each coroutine has a chance to be executed.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119415191A_ABST
    Figure CN119415191A_ABST
Patent Text Reader

Abstract

The invention provides a service processing method and device, electronic equipment and a storage medium. The method comprises the steps that a scheduler is used for determining an operable target coroutine from multiple service execution coroutines according to current state parameters of all service execution coroutines; generating a first jump instruction based on the context information of the target coroutine by using the scheduler; executing the first jump instruction to enable the target coroutine to execute a corresponding service; using the target coroutine to generate a second jump instruction based on the context information of the scheduler when the duration of executing the corresponding service by the target coroutine reaches a first preset duration; and executing the second jump instruction to transfer the control right of the target coroutine for the CPU to the scheduler. Through the method, after each service execution coroutine reaches the duration upper limit, the scheduler selects other service execution coroutines again for execution, so that the hunger phenomenon of the service execution coroutines is avoided, the service corresponding to each service execution coroutine can be processed in time, and the response speed of the whole service is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a business processing method, device, electronic device and storage medium. Background Art

[0002] At present, in operating systems, multitasking has become an important means to improve system performance and response speed. With the rapid development of technologies such as cloud computing, big data, and distributed systems, application systems often need to handle complex business processes at the same time. These business processes are usually decomposed into multiple sub-businesses and executed in the operating system in the form of coroutines.

[0003] As a lightweight alternative to threads, coroutines can implement task switching in user mode without the intervention of the operating system kernel, thereby reducing the overhead of context switching and improving execution efficiency. However, when scheduling coroutines corresponding to multiple sub-businesses, the coroutine library usually relies on event loops to schedule coroutines, and usually when a coroutine is scheduled for execution, the coroutine will execute its corresponding subtasks as much as possible until the task is completed or suspended, and then other coroutines will execute the corresponding subtasks. This scheduling and execution method will cause some coroutines to be unable to resume operation for a long time after being suspended, resulting in starvation of these coroutines, resulting in slow business response. Summary of the invention

[0004] In view of the above problems, the present application provides a business processing method, device, electronic device and storage medium. By setting the upper limit of the execution time of the target coroutine (a first preset time), it is ensured that any business execution coroutine must give up the execution right after reaching the upper limit of the execution time, thereby effectively alleviating the coroutine starvation phenomenon and improving the response speed of the overall business.

[0005] The embodiment of the present application is implemented by adopting the following technical solutions: In a first aspect, an embodiment of the present application provides a business processing method, which is applied to an operating system, wherein the operating system is deployed with a multi-coroutine framework, the multi-coroutine framework includes a scheduler and a plurality of business execution coroutines, the scheduler is used to schedule the plurality of business execution coroutines, and each of the business execution coroutines corresponds to a business, the method comprising: using the scheduler to obtain the current state parameters of each of the business execution coroutines; using the scheduler to determine an executable target coroutine from the plurality of business execution coroutines according to the current state parameters of each business execution coroutine; using the scheduler to generate a first jump instruction based on the context information of the target coroutine, The first jump instruction carries the code position of the business corresponding to the target coroutine, and the first jump instruction is used to instruct the execution of the business from the code position of the business corresponding to the target coroutine; the first jump instruction is executed to make the target coroutine execute the corresponding business; when the duration of the execution of the corresponding business by the target coroutine reaches a first preset duration, the target coroutine is used to generate a second jump instruction based on the context information of the scheduler, the second jump instruction carries the code position of the business corresponding to the scheduler, and the second jump instruction is used to instruct the execution of the business from the code position of the business corresponding to the scheduler; the second jump instruction is executed.

[0006] In a second aspect, an embodiment of the present application provides a business processing device, which is applied to an operating system, wherein the operating system is deployed with a multi-coroutine framework, wherein the multi-coroutine framework includes a scheduler and multiple business execution coroutines, wherein the scheduler is used to schedule the multiple business execution coroutines, and each of the business execution coroutines corresponds to a business. The device includes: a parameter acquisition module, which is used to use the scheduler to obtain the current state parameters of each of the business execution coroutines; a coroutine determination module, which is used to use the scheduler to determine an executable target coroutine from the multiple business execution coroutines according to the current state parameters of each business execution coroutine; and a first instruction generation module, which is used to use the scheduler to generate a first jump instruction based on the context information of the target coroutine. The first jump instruction carries the code position of the business corresponding to the target coroutine, and the first jump instruction is used to indicate the execution of the business from the code position of the business corresponding to the target coroutine; a first instruction execution module is used to execute the first jump instruction so that the target coroutine executes the corresponding business; a second instruction generation module is used to generate a second jump instruction based on the context information of the scheduler using the target coroutine when the execution time of the corresponding business by the target coroutine reaches a first preset time, the second jump instruction carries the code position of the business corresponding to the scheduler, and the second jump instruction is used to indicate the execution of the business from the code position of the business corresponding to the scheduler; a second instruction execution module is used to execute the second jump instruction.

[0007] In one possible implementation, the device also includes: a signal sending module, which is used to call a system timing function to send a target instruction to the operating system while executing the first jump instruction, and the target instruction is used to instruct the operating system to send a switching indication signal to the target coroutine after receiving the target instruction for a first preset time; if the target coroutine generates a second jump instruction based on the context information of the scheduler when receiving the switching indication signal, it is confirmed that the duration of the target coroutine executing the corresponding business reaches the first preset time.

[0008] In one possible implementation, the device also includes: a timer registration module, which is used to call an execution switching processing function to register a timer of a certain specified duration in the target coroutine when it is determined that the duration of the target coroutine executing the corresponding business reaches a first preset duration, wherein if the timing duration of the timer indicates that the target coroutine can run.

[0009] In one possible implementation, the multiple business execution coroutines are created in the target thread, and the current state parameters of the business execution coroutine include the priority of the business execution coroutine, the timing duration of the timer in the business execution coroutine, and the state parameters of the trigger event for the business execution coroutine; the coroutine determination module includes a first determination submodule and a second determination submodule. The first determination submodule is used to determine a candidate business execution coroutine from multiple business execution coroutines based on the time parameters of the timer in each of the business execution coroutines and the state parameters of the corresponding trigger event, the timing duration of the timer in the candidate business execution coroutine indicating that the timer has timed out, or the state parameters of the trigger event for the candidate business indicating that the trigger event has occurred; the second determination submodule is used to determine a candidate business execution coroutine with the highest priority from the multiple candidate business execution coroutines as an executable target coroutine when there are multiple candidate business execution coroutines.

[0010] In one possible implementation, the second determination submodule is also used to determine the candidate business execution coroutine with the highest priority from the multiple candidate business execution coroutines; if there are multiple candidate business execution coroutines with the highest priority, the candidate business execution coroutine with the longest suspension time is selected from the candidate business execution coroutines with the highest priority as the target coroutine.

[0011] In one possible implementation, the current state parameters of the business execution coroutine include the priority of the business execution coroutine, and the device also includes: an operation count acquisition module and a priority adjustment module, the operation count acquisition module is used to obtain the operation count of each business execution coroutine within the life cycle of the target thread every second preset time interval; the priority adjustment module is used to adjust the priority of each business execution coroutine according to the corresponding operation count of each business execution coroutine, and the operation count is negatively correlated with the priority.

[0012] In one possible implementation, the device further includes a coroutine recycling module, which is used to recycle the target coroutine when it is determined that the execution duration corresponding to the target coroutine has not reached a first preset duration and the business corresponding to the target coroutine has been completed.

[0013] In a third aspect, an embodiment of the present application provides an electronic device, comprising: one or more processors; a memory; one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs are configured to execute the above-mentioned business processing method.

[0014] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a program code, and the program code can be called by a processor to execute the above-mentioned business processing method.

[0015] The business processing method, device, electronic device and storage medium provided in the embodiments of the present application, in the business processing method, by utilizing the scheduler to determine the executable target coroutine from the multiple business execution coroutines according to the current state parameters of each business execution coroutine, it is possible to flexibly respond to the real-time needs of different coroutines, ensuring that each coroutine has a chance to be executed, thereby avoiding the problem of other tasks waiting too long due to some tasks occupying resources for a long time. Further, by utilizing the scheduler to generate a first jump instruction based on the context information of the target coroutine, thereafter, executing the first jump instruction can transfer the control of the CPU to the target coroutine, so that the target coroutine executes the corresponding business. When the duration of executing the corresponding business using the target coroutine reaches a first preset duration, the target coroutine is used to generate a second jump instruction based on the context information of the scheduler; thereafter, executing the second jump instruction can transfer the control of the target coroutine over the CPU to the scheduler, so as to achieve the upper limit of the execution time of the target coroutine (the first preset duration) by setting the execution time of the target coroutine, so that each coroutine is selected by the scheduler to execute other coroutines again after reaching the upper limit, thereby effectively alleviating the coroutine starvation phenomenon, so that each business corresponding to the execution coroutine of each business can be processed in time, and the response speed of the overall business is improved.

[0016] These and other aspects of the present application will become more clearly understood in the description of the following embodiments. BRIEF DESCRIPTION OF THE DRAWINGS

[0017] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.

[0018] Figure 1 It is a flowchart of a business processing method provided in an embodiment of the present application.

[0019] Figure 2 This is a schematic diagram of multiple coroutines in a target thread provided in an embodiment of the present application.

[0020] Figure 3 It is a flowchart of a business processing method provided in an embodiment of the present application.

[0021] Figure 4 This is another flowchart of a business processing method provided in an embodiment of the present application.

[0022] Figure 5 This is another flow chart of a business processing method provided in an embodiment of the present application.

[0023] Figure 6 It is a connection block diagram of a business processing device provided in an embodiment of the present application.

[0024] Figure 7 It is a schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0025] The embodiments of the present application are described in detail below, and examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present application, and cannot be understood as limiting the present application.

[0026] In order to enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without making creative work are within the scope of protection of the present application.

[0027] The embodiments of the present application will be described in detail below with reference to the accompanying drawings.

[0028] like Figure 1 , Figure 1A flowchart of a business processing method provided in an embodiment of the present application is provided, and the method can be applied to an operating system, wherein a multi-coroutine framework is deployed in the operating system, and the multi-coroutine framework includes a scheduler and multiple business execution coroutines, wherein the multiple business coroutines are created by a target thread in the operating system, such as Figure 2 As shown, the number of multiple business execution coroutines is specifically N, where N is a positive integer. The scheduler is used to schedule the multiple business execution coroutines, and each of the business execution coroutines corresponds to a business. The business corresponding to each business execution coroutine needs to have control over the CPU to use the resources in the CPU to execute the corresponding business. The specific business can be a computing task, an I / O operation task, a task for listening to and responding to specific events, a data processing task, or a timing or periodic task, etc. The business corresponding to different business execution coroutines may be different.

[0029] It should be noted that only one business execution coroutine can be run in the target thread at the same time. In addition, a coroutine pool is set in the target thread. After any coroutine completes the corresponding business processing, the coroutine can be suspended and stored in the coroutine pool, and its current state information can be saved to the coroutine pool. If a business execution coroutine is to be re-run later, the business execution coroutine can be restored from the coroutine pool, and the corresponding business can be processed according to the saved context information of the business execution coroutine.

[0030] The method is executed by the scheduler, and the method includes: Step S110: Utilize the scheduler to obtain the current state parameters of each of the business execution coroutines.

[0031] Among them, the scheduler is a coroutine or software program module in the operating system's multi-coroutine framework, which is responsible for starting, managing and scheduling other coroutines or threads.

[0032] The scheduler can obtain and analyze the current status parameters of all business execution coroutines. The current status parameters of the business execution coroutines include the running status of the business execution coroutines (such as ready, running, waiting, suspended, completed or blocked), and may also include the number of times each business execution coroutine runs within the life cycle of the target thread, the status parameters of the triggering events corresponding to each business execution coroutine, the priority of the business execution coroutine, the time parameters of the timer in the business execution coroutine, etc.

[0033] Among them, the ready state means that the business execution coroutine has been initialized, all necessary resources are ready, and it is waiting to be selected by the scheduler to obtain CPU execution time. In this state, the business execution coroutine can start running immediately; the running state means that the business execution coroutine is being executed by the processor. In the target thread, only one business execution coroutine is in the running state at any time; the waiting state means that the business execution coroutine is suspended because it is waiting for an external condition (such as I / O operation completion, lock release, timer expiration, etc.). During this period, the business execution coroutine will not consume CPU resources or participate in scheduling until the waiting condition is met; the suspended state means that the business coroutine actively gives up the right to use the CPU, usually because it has executed a yield operation or is required to be suspended by the scheduler's scheduling policy. The business execution coroutine can resume execution at any time through the timer. This state is often used to implement cooperative multitasking. The completed or terminated state means that the business execution coroutine has been successfully executed, all tasks are completed, and it is no longer involved in scheduling. In this state, the business execution coroutine will generally not be executed again unless it is reinitialized through some mechanism (such as restart). The blocking state is similar to the waiting state, but usually refers to the situation where execution cannot continue due to resource competition failure (such as failure to obtain a mutex lock). The main difference from the waiting state is that the blocking state is passive and is enforced by the system or runtime environment.

[0034] It should be noted that, among the multiple states of the business execution coroutine listed above, when the state of the business execution program is ready, when a trigger event is received in a non-ready state (such as a waiting state or a suspended state), or when the corresponding timer times out in a non-ready state (such as a waiting state or a suspended state), it indicates that the business execution coroutine can run.

[0035] The trigger event of the business execution coroutine can be an I / O completion event, which is an event triggered by the operating system or framework when the read or write operation is completed; a state change event, which is an event generated when the business state changes; or a timer event, which is a task expiration event generated by a delay function. The state parameter of the trigger event is used to indicate whether the trigger event has occurred. For example, the state parameter corresponding to the I / O completion event indicates whether the read or write operation has been completed; the state parameter corresponding to the state change event indicates whether the business state has changed; and the state parameter corresponding to the timer event indicates whether the timer has timed out.

[0036] The priority of each business execution coroutine can be preset, such as being set according to the importance and urgency of tasks in different business execution coroutines. After being preset, the priority of each business execution coroutine can be adjusted as the execution time increases during the business execution process, or according to the number of times the coroutine in each business is run. No specific limitation is made here.

[0037] It should be understood that when the state parameter of the triggering event of the service execution coroutine indicates that the triggering event has occurred, the service execution coroutine can be run, which means that the service execution coroutine can perform the next stage of processing of its service.

[0038] The above step S110 can be executed when the scheduler is running, that is, at the beginning of the business or the creation of the coroutine, the scheduler will collect the initial state parameters of all coroutines (including each business execution coroutine) as the current state parameters; it can also be executed when other business execution coroutines switch to the main thread; it can also be executed periodically at a fixed frequency or based on some event trigger; it can also be executed when system resources are tight or there are new high-priority tasks to be executed.

[0039] It should be noted that when the scheduler is running, it has control over each business execution coroutine in the target thread, such as deciding which business execution coroutine will be activated for execution and when to switch the business execution coroutine; when the scheduler is running, it can also be responsible for monitoring and managing system resources, including but not limited to memory allocation, file handles, network connections, etc., to ensure the reasonable allocation and recovery of resources.

[0040] Step S120: using the scheduler to determine an executable target coroutine from the multiple business execution coroutines according to the current state parameters of each business execution coroutine.

[0041] Among them, the business execution coroutine is executable means that the business execution coroutine is in a ready state. When a trigger event is received in a non-ready state (such as a waiting state or a suspended state), or when the corresponding timer times out in a non-ready state (such as a waiting state or a suspended state), it indicates that the business execution coroutine is executable.

[0042] In one possible implementation, if the current state parameter of each business execution coroutine only includes the running state of each business execution coroutine, the above step S120 may be to select at least one business execution coroutine from the business execution coroutines in the ready state as the executable target coroutine.

[0043] In another possible implementation, if the current state parameters of each business execution coroutine include the running state of each business execution coroutine, the state parameters of the trigger event corresponding to each business execution coroutine, and the time parameters of the timer in the business execution coroutine, then the above step S120 can select one or more of the business execution coroutines whose running state is ready, the business execution coroutine whose timer duration indicates that the timer has timed out, and the task execution coroutine whose state parameters of the trigger event indicate that the trigger event has occurred from multiple business execution coroutines as executable candidate business execution coroutines. After obtaining multiple executable candidate business execution coroutines, at least one target coroutine can be selected from the multiple candidate business execution coroutines.

[0044] Among them, the method of selecting at least one target coroutine from multiple candidate business execution coroutines can be to randomly select at least one target coroutine from multiple candidate business execution coroutines, or to select the target coroutine from the candidate business execution coroutines based on one or more of the priority of the business execution coroutine, the suspension duration, the number of runs within the life cycle of the target thread, etc.

[0045] In this case, if the current state parameters of the business execution coroutine include the priority of the business execution coroutine, the method of selecting at least one target coroutine from multiple candidate business execution coroutines can be to determine at least one candidate business execution coroutine with the highest priority from the multiple candidate business execution coroutines as the executable target coroutine.

[0046] By introducing priorities, high-priority business execution coroutines have priority, thereby ensuring that critical tasks can be responded to quickly to meet real-time requirements.

[0047] In this case, it should be noted that if the target coroutine is one, the candidate business execution coroutine with the highest priority is determined from the multiple candidate business execution coroutines; if there are multiple candidate business execution coroutines with the highest priority, the candidate business execution program with the longest suspension time is selected from the priority candidate business execution coroutines as the target business execution program. By identifying and executing high-priority business execution coroutines, it is ensured that the critical business or time-sensitive operations in the business execution coroutine can be responded to immediately. In the case where multiple tasks have the same highest priority, by further considering the suspension time, the coroutine with the longest suspension time is selected as the execution target, which can not only ensure that among tasks of equal importance, tasks with longer waiting time are processed, reducing the average waiting time of tasks, but also improve the fairness of task scheduling, and avoid the situation where some tasks are not served for a long time. Therefore, the dual consideration mechanism of priority and suspension time provides a more reasonable decision-making basis for task scheduling, makes system resource allocation more orderly, reduces the risk of system congestion or slow response due to improper task scheduling, and thus improves the stability and predictability of the overall system.

[0048] If the current state parameter of the business execution coroutine includes the suspension duration of the business execution coroutine, the method of selecting at least one target coroutine from multiple candidate business execution coroutines can be to determine at least one candidate business execution coroutine with the longest suspension duration from the candidate business execution coroutines as the executable target coroutine. By selecting the coroutine with the longest suspension time as the execution target, it is possible to effectively avoid certain tasks being ignored for a long time, thereby improving the fairness of scheduling various business execution coroutines.

[0049] If the current state parameter of the business execution coroutine includes the number of times the business execution coroutine has run in the life cycle of the target thread, the method of selecting at least one target coroutine from multiple candidate business execution coroutines can be to determine at least one candidate business execution coroutine with the least number of times each business execution coroutine has run in the life cycle of the target thread as the executable target coroutine. By considering the number of times the coroutine has run in the target thread, selecting the coroutine with the least number of runs to execute helps to balance the execution opportunities between different tasks.

[0050] In another possible implementation, if the current state parameters of each business execution coroutine include the running state and priority state of each business execution coroutine, then the above step S120 can also be to obtain the top K business execution coroutines with the highest priority among the business execution coroutines as candidate business execution coroutines, K is a positive integer, and select an executable target coroutine from the candidate business execution coroutines. It should be understood that in this case, if there is no executable target coroutine among the top K business execution coroutines with the highest priority, then the top N business execution coroutines with the highest priority among the unselected business execution coroutines can be selected as candidate business execution coroutines until an executable target coroutine is selected.

[0051] like Figure 3 As shown, there are N business execution coroutines, each of which corresponds to a priority and a running state, a timer timing duration and a state parameter of a trigger event for the business execution coroutine. If the current state of the business execution coroutine is a ready state, it means that the business execution coroutine can run; if the timer times out or a trigger event is received, it also means that the business execution coroutine can run; in this way, a business execution coroutine with the highest priority that can be run can be selected as the target coroutine according to the current state parameters of each business execution coroutine, and the CPU control right can be handed over to the target coroutine.

[0052] It should be understood that the above method of selecting the target coroutine is only illustrative, and there may be more selection methods, which will not be described in detail in this embodiment.

[0053] Step S130: Generate a first jump instruction based on the context information of the target coroutine using a scheduler.

[0054] The first jump instruction carries the code location of the business corresponding to the target coroutine, and the first jump instruction is used to instruct to execute the business from the code location of the business corresponding to the target coroutine.

[0055] Step S140: Execute the first jump instruction to enable the target coroutine to execute the corresponding business.

[0056] By executing the first jump instruction, the control right of the CPU can be transferred to the target coroutine.

[0057] Step S150: When the duration of the target coroutine executing the corresponding business reaches a first preset duration, using the target coroutine to generate a second jump instruction based on the context information of the scheduler.

[0058] The second jump instruction carries the code position of the service corresponding to the scheduler, and the second jump instruction is used to instruct to execute the service from the code position of the service corresponding to the scheduler.

[0059] Step S160: Execute the second jump instruction.

[0060] By executing the second jump instruction, the control right of the target coroutine with respect to the CPU can be transferred to the scheduler.

[0061] It should be noted that both the scheduler and the business execution coroutine have context information. In an embodiment of the present application, the context information may refer to the position to which the business was run before the business coroutine or scheduler was suspended. Specifically, the scheduler reads the stack and register status of the target coroutine, and determines according to the stack and register status of the target coroutine that before transferring the control of the CPU to the target coroutine, it is necessary to save the context information of the scheduler itself, including but not limited to the corresponding register contents, stack pointer, program counter, etc. Once the context information of the scheduler is properly saved, the next step is to load the context information of the target coroutine. This means restoring all necessary information of the target coroutine saved before, including the code position of the corresponding business when the target coroutine ended last time, so that the target coroutine can continue to execute from the place where the corresponding business was interrupted. Thereafter, a first jump instruction can be generated based on the code position of the business corresponding to the above target coroutine. The first jump instruction is used to instruct the controller to jump to the code position of the business corresponding to the target coroutine to start execution, thereby transferring the control of the CPU to the target coroutine, so that the target coroutine can continue to run from the code position where it was running when it was suspended last time according to the status information.

[0062] It is worth mentioning that after reading the stack and register status of the target coroutine to generate a jump instruction and executing the jump instruction to transfer the control of the CPU to the target coroutine, it means that the scheduler is "suspended".

[0063] When the duration of the target coroutine executing the corresponding business reaches a first preset duration, a second jump instruction is generated based on the context information of the scheduler, and the second jump instruction is executed to transfer the control of the target coroutine for the CPU to the scheduler, which means that the target coroutine is suspended. At this time, the target coroutine is stored in the coroutine pool again. At the same time, the scheduler can use the context information of the target coroutine when the execution duration reaches the first preset duration as the current state information of the target coroutine, and store the current state information of the target coroutine in the coroutine pool. Regarding the above-mentioned process of transferring the control of the target coroutine for the CPU to the scheduler and returning it to the scheduler, please refer to the aforementioned process of the scheduler transferring the control of the CPU to the target coroutine, which will not be repeated here.

[0064] It should be noted that if Figure 4 As shown, if there are multiple target coroutines, the scheduler can switch to each executable target coroutine in turn by allocating a fixed time slice to each coroutine. After each target coroutine reaches the first preset duration, it is forced to switch to the next executable target coroutine. Time slice allocation is to allocate a period of CPU time to each target coroutine. The target coroutine can be executed exclusively within the corresponding CPU time, and automatically switch to the next target coroutine after the CPU time ends. For each target coroutine, a timeout monitoring mechanism can also be maintained for the target coroutine. Once the first preset duration is reached, it switches back to the scheduler, and the scheduler switches to the next target coroutine to avoid a single business execution coroutine occupying resources for a long time, causing starvation of other business execution coroutines.

[0065] Next, the scheduler can return to execute the aforementioned steps S110-S160, or when there are multiple target businesses determined above, the scheduler can switch to the next target coroutine to enable the target coroutine to execute the corresponding business, until the first preset time is completed for all multiple target businesses, and then return to execute the aforementioned steps S110-S160.

[0066] By adopting the above method of the present application, the scheduler determines the executable target coroutine from the multiple business execution coroutines according to the current state parameters of each business execution coroutine, so as to flexibly respond to the real-time needs of different coroutines, ensure that each coroutine has the opportunity to be executed, thereby avoiding the problem of other tasks waiting too long due to some tasks occupying resources for a long time. Further, the control of the CPU is transferred to the target coroutine so that the target coroutine executes the corresponding business; if it is determined that the duration of the target coroutine to execute the corresponding business reaches the first preset duration, the control of the target coroutine over the CPU is recovered, so as to achieve the upper limit of the execution time of the target coroutine (the first preset duration), so that each coroutine is selected by the scheduler to execute other coroutines again after reaching the upper limit of the duration, which effectively alleviates the phenomenon of coroutine starvation, so that each business corresponding to each business execution coroutine can be processed in time, and the response speed of the overall business is improved.

[0067] The method also includes calling a system timing function to send a target instruction to the operating system while executing the first jump instruction, and the target instruction is used to instruct the operating system to send a switching indication signal to the target coroutine after receiving the target instruction for a first preset time; if the target coroutine generates a second jump instruction based on the context information of the scheduler when receiving the switching indication signal, it is confirmed that the duration of the target coroutine executing the corresponding business reaches the first preset time.

[0068] Among them, the system timing function can specifically include a system timer and a callback function. After the scheduler determines the target coroutine, it can select a timing function or an API (Application Programming Interface) to set the system timer according to the operating system and programming environment of the electronic device. For example, in a Unix / Linux system, the setitimer function can be used to set a system timer with a certain duration of a first preset duration (e.g., N milliseconds); in a Windows system, the CreateWaitableTimer and SetWaitableTimer functions can be used to set a timer with a system timing duration of a first preset duration. Afterwards, a callback function is specified (or registered), and the scheduler calls the callback function to instruct the operating system to send a switching indication signal when the system timer expires (when the timing reaches the first preset duration). This callback function can be a lightweight processing logic, and its main task is to send a switching indication signal (e.g., a SIGALARM signal) to the target coroutine. Among them, the callback function and the system timer may be encapsulated as a system timing function, and the operating system calls the system timing function to trigger an event or generate a signal (e.g., generate a switching indication signal) through the expiration mechanism of the timer.

[0069] Considering that for the target coroutine switched after the time runs out, the target coroutine should be an executable business execution coroutine after switching to the scheduler, so that the scheduler may select the business execution coroutine when selecting the target coroutine in the next cycle. Therefore, in one embodiment, after completing the switching of the target coroutine to the scheduler, the method also includes updating the information of the target coroutine in the coroutine pool and adjusting the running state of the target coroutine to the ready state. This means that although the target coroutine has temporarily stopped executing, it is ready to be scheduled for execution again without any additional conditions being met. This method simplifies state management. When the scheduler selects an executable coroutine in the next cycle, by checking whether the running state of the business execution coroutine is "ready", it is possible to re-select the business execution coroutine at the last execution as the target coroutine to continue execution, thereby improving the efficiency and flexibility of the scheduler in scheduling each business execution coroutine.

[0070] In another possible implementation, the method also includes: when it is determined that the duration of the target coroutine to execute the corresponding business reaches a first preset duration, calling an execution switching processing function to register a timer of a certain specified duration in the target coroutine, wherein if the timing duration of the timer indicates that it reaches the specified duration, it indicates that the target coroutine can run.

[0071] The specified duration can be 0 seconds, 5 milliseconds, 10 milliseconds, etc., and can be set according to actual needs. Through the above implementation, once the timer's timing duration indication reaches the specified duration, it can indirectly indicate that the target coroutine enters the "runnable state" again. This method allows the business execution coroutine to have a certain self-management ability, and can automatically resume execution under specific conditions (such as completing a short non-critical task or waiting for a certain condition to mature), without the direct intervention of the scheduler, which increases the responsiveness and autonomy of the system.

[0072] In some embodiments, the current state parameters of the business execution coroutine include the priority of the business execution coroutine, and the priority is used as the basis for selecting the target coroutine. If the priority set for the business execution coroutine may not be accurate enough, it will lead to inaccurate selection of the target coroutine. Therefore, the priority of each business execution coroutine can be adjusted according to the historical execution status of each business execution coroutine.

[0073] For example, for business execution coroutines that need to respond quickly, such as business execution coroutines that process user input or real-time events, even if these business execution coroutines are not run many times, they need to have a higher priority to ensure that the system can respond to external events in a timely manner. At this time, the scheduler will regularly scan the number of times each business execution coroutine has run in the life cycle of the target thread, and for coroutines with fewer runs, their priority can be appropriately increased.

[0074] CPU-intensive business execution coroutines usually occupy the CPU for a long time, which may cause other low-priority I / O-intensive business execution coroutines or user-interactive business coroutines to not be processed in time, affecting the system's responsiveness and user experience. Since the identification method of CPU-intensive business execution coroutines is relatively simple, generally speaking, CPU-intensive tasks will be suspended due to exhaustion of time slices, while IO-intensive tasks rarely exhaust time slices; the scheduler can count the number of times each business execution coroutine is suspended due to exhaustion of time slices. The more times, the more likely it is a CPU-intensive coroutine, and the priority of these coroutines needs to be appropriately lowered.

[0075] Among them, IO-intensive (I / O-bound) business execution coroutines refer to coroutines that spend most of the execution time waiting for external devices (such as hard disks, networks, and databases) to complete input / output operations. For example, file read and write operations, network data transmission and reception, database queries, etc.

[0076] CPU-bound business execution coroutines refer to coroutines that perform computing tasks most of the time during the execution process. These tasks are highly dependent on the computing power of the processor, such as complex mathematical operations, data encryption and decryption, image processing, etc.

[0077] Based on the above issue of inaccurate priority setting, please refer to Figure 5 As shown, in one possible implementation, the method further includes: utilizing a scheduler to perform the following steps S170-S180.

[0078] Step S170: Obtain the number of times each of the business execution coroutines runs within the life cycle of the target thread at intervals of a second preset time.

[0079] The second preset time length can be 1 second, 2 seconds, 5 seconds or 10 seconds, etc., and can be set according to actual needs.

[0080] Exemplarily, when the target thread is started or the coroutine is created, a running count counter is initialized for each business execution coroutine, and the initial value is 0. A timer of a second preset duration is set in the scheduler, and every time the second preset duration (such as 1 second, 2 seconds, 5 seconds or 10 seconds) passes, an operation of traversing all business execution coroutines is triggered, and the number of executions of each coroutine since the last adjustment is queried and recorded, thereby obtaining the number of runs of each business execution coroutine in the life cycle of the target thread.

[0081] Step S180: adjusting the priority of each business execution coroutine according to the number of executions corresponding to each business execution coroutine, wherein the number of executions is negatively correlated with the priority.

[0082] Exemplarily, the priority of the business execution coroutine is dynamically adjusted according to the number of runs obtained. The specific algorithm can be simply designed as follows: the more business execution coroutines that are run, the lower their priority. For example, the priority can be calculated by a simple formula: priority = lowest priority + (maximum number of runs - number of runs of the current coroutine) / step value, where the lowest priority is the minimum priority of all business execution coroutines, and the step value is used to control the magnitude of the priority adjustment. It should be understood that the updated priority should take effect immediately and be used as a basis for decision-making when selecting the next executable target coroutine. So that when the scheduler selects the next business execution coroutine to be executed based on the state parameters, those business execution coroutines with fewer runs and higher priorities will be given priority.

[0083] By setting the number of runs to be negatively correlated with the priority, each business execution coroutine is ensured to have roughly equal execution opportunities during the life cycle of the target thread. This allows the priority of each business execution coroutine to be adjusted dynamically, which helps to allocate system resources more reasonably and ensures that tasks in each business execution coroutine can be processed within a reasonable time, thereby improving system efficiency and response speed overall. This prevents some coroutines from occupying resources all the time due to frequent scheduling, causing other coroutines to wait for a long time, thereby improving the fairness of task execution.

[0084] In one possible implementation, the method further includes: if it is determined that the business corresponding to the target coroutine has been executed, recycling the target coroutine.

[0085] There are many ways to determine whether the business corresponding to the target coroutine has been executed. For example, when a business execution completion message sent by the target coroutine is received, it is determined that the business corresponding to the target coroutine has been executed. It can also be determined that the corresponding business has been executed when it is determined that the context information corresponding to the target coroutine has not changed before and after obtaining CPU control (or obtaining control for a preset number of consecutive times).

[0086] Among them, when recycling the target coroutine, the current state of the coroutine can also be updated to the completed state to ensure that it is no longer mistaken for an active or schedulable state. When recycling the target coroutine, the resources of the target coroutine can be released and cleaned up, including but not limited to closing open file handles, releasing memory resources, canceling timers or event subscriptions, etc. By timely recycling coroutines that are no longer needed, the operating burden of the system is reduced, allowing the operating system to reallocate resources to other active coroutines or new tasks, thereby improving overall processing power and response speed.

[0087] like Figure 6As shown, a business processing device 200 provided by an embodiment of the present application is applied to an operating system, wherein the operating system is deployed with a multi-coroutine framework, wherein the multi-coroutine framework includes a scheduler and a plurality of business execution coroutines, wherein the scheduler is used to schedule the plurality of business execution coroutines, wherein each of the business execution coroutines corresponds to a business, and the business processing device 200 includes: a parameter acquisition module 210, which is used to use the scheduler to obtain the current state parameters of each of the business execution coroutines; a coroutine determination module 220, which is used to use the scheduler to determine an executable target coroutine from the plurality of business execution coroutines according to the current state parameters of each business execution coroutine; and a first instruction generation module 230, which is used to use the scheduler to generate a first jump based on the context information of the target coroutine. Instruction, the first jump instruction carries the code position of the business corresponding to the target coroutine, and the first jump instruction is used to indicate the execution of the business from the code position of the business corresponding to the target coroutine; a first instruction execution module 240 is used to execute the first jump instruction so that the target coroutine executes the corresponding business; a second instruction generation module 250 is used to generate a second jump instruction based on the context information of the scheduler using the target coroutine when the execution time of the corresponding business by the target coroutine reaches a first preset time, the second jump instruction carries the code position of the business corresponding to the scheduler, and the second jump instruction is used to indicate the execution of the business from the code position of the business corresponding to the scheduler; a second instruction execution module 260 is used to execute the second jump instruction.

[0088] In one possible implementation, the business processing device 200 also includes: a signal sending module, which is used to call a system timing function to send a target instruction to the operating system while executing the first jump instruction, and the target instruction is used to instruct the operating system to send a switching indication signal to the target coroutine after receiving the target instruction for a first preset time; if the target coroutine generates a second jump instruction based on the context information of the scheduler when receiving the switching indication signal, it is confirmed that the duration of the target coroutine executing the corresponding business reaches the first preset time.

[0089] In one possible implementation, the business processing device 200 also includes: a timer registration module, which is used to call an execution switching processing function to register a timer of a certain specified duration in the target coroutine when it is determined that the duration of the target coroutine executing the corresponding business reaches a first preset duration, wherein if the timing duration of the timer indicates that the target coroutine can run.

[0090] In one possible implementation, the current state parameters of the business execution coroutine include the priority of the business execution coroutine, the timing duration of the timer in the business execution coroutine, and the state parameters of the trigger event for the business execution coroutine; the coroutine determination module 220 includes a first determination submodule and a second determination submodule. The first determination submodule is used to determine a candidate business execution coroutine from multiple business execution coroutines based on the time parameters of the timer in each of the business execution coroutines and the state parameters of the corresponding trigger event, the timing duration of the timer in the candidate business execution coroutine indicating that the timer has timed out, or the state parameters of the trigger event for the candidate business indicating that the trigger event has occurred; the second determination submodule is used to determine a candidate business execution coroutine with the highest priority from the multiple candidate business execution coroutines as an executable target coroutine when there are multiple candidate business execution coroutines.

[0091] In one possible implementation, the second determination submodule is also used to determine the candidate business execution coroutine with the highest priority from the multiple candidate business execution coroutines; if there are multiple candidate business execution coroutines with the highest priority, the candidate business execution coroutine with the longest suspension time is selected from the candidate business execution coroutines with the highest priority as the target coroutine.

[0092] In one possible implementation, the current state parameters of the business execution coroutine include the priority of the business execution coroutine, and the business processing device 200 also includes: an operation count acquisition module and a priority adjustment module, the operation count acquisition module is used to obtain the operation count of each business execution coroutine within the life cycle of the target thread at every second preset time interval; the priority adjustment module is used to adjust the priority of each business execution coroutine according to the corresponding operation count of each business execution coroutine, and the operation count is negatively correlated with the priority.

[0093] In one possible implementation, the business processing device 200 also includes a coroutine recycling module, which is used to recycle the target coroutine when it is determined that the execution time corresponding to the target coroutine has not reached a first preset time and the business corresponding to the target coroutine has been executed.

[0094] Correspondingly, such as Figure 7 The embodiment of the present application further provides an electronic device 500, which may be an electronic device capable of running applications, such as a smart phone, a tablet computer, or a server. The electronic device 500 may include one or more processors 510, a memory 520, and one or more applications, wherein the one or more applications are stored in the memory 520 and configured to be executed by the one or more processors 510, and the one or more applications are configured to execute the above-mentioned business processing method.

[0095] It can be understood that the processor 510 and the memory 520 are connected via a communication bus to complete mutual communication, and the processor 510 can call the logic instructions in the memory 520 to execute the speech template generation method in any of the aforementioned embodiments. Exemplarily, the processor 510 can be implemented in the form of a general-purpose CPU, a microprocessor, an application-specific integrated circuit (Application Specific Integrated Circuit.ASIC), or one or more integrated circuits, etc., for executing related programs to implement the technical solution provided in this application. The memory can include ROM (Read 0nly Memory, read-only memory), RAM (Random Access Memory, random access memory) static storage devices, dynamic storage devices, etc.

[0096] Correspondingly, an embodiment of the present application further provides a computer-readable storage medium, in which program codes are stored, and the program codes can be called by a processor to execute the above-mentioned business processing method.

[0097] It can be understood that the logic instructions in the above-mentioned memory can be implemented in the form of software functional units and can be stored in a computer-readable storage medium when sold or used as an independent product.

[0098] Part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes a number of instructions for enabling a computer device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods of various embodiments of the present invention. The aforementioned storage medium includes: a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and other media that can store program codes.

[0099] The above are only preferred embodiments of the present application, and are not intended to limit the present application in any form. Although the present application has been disclosed as above with preferred embodiments, it is not intended to limit the present application. Any technical personnel in the field can make some changes or modify the technical contents disclosed above into equivalent embodiments without departing from the scope of the technical solution of the present application. However, any brief modifications, equivalent changes and modifications made to the above embodiments based on the technical essence of the present application without departing from the content of the technical solution of the present application are still within the scope of the technical solution of the present application.

Claims

1. A business processing method, characterized in that: Applied to an operating system, the operating system is deployed with a multi-coroutine framework, the multi-coroutine framework includes a scheduler and a plurality of business execution coroutines, the scheduler is used to schedule the plurality of business execution coroutines, each of the business execution coroutines corresponds to a business, and the method includes: Using the scheduler to obtain the current state parameters of each of the business execution coroutines; Determine an executable target coroutine from the multiple business execution coroutines according to the current state parameters of each business execution coroutine by using the scheduler; Generate a first jump instruction using the scheduler based on the context information of the target coroutine, wherein the first jump instruction carries the code position of the business corresponding to the target coroutine, and the first jump instruction is used to instruct to execute the business from the code position of the business corresponding to the target coroutine; Execute the first jump instruction to enable the target coroutine to execute the corresponding business; When the duration of the target coroutine executing the corresponding service reaches a first preset duration, using the target coroutine to generate a second jump instruction based on the context information of the scheduler, the second jump instruction carries the code position of the service corresponding to the scheduler, and the second jump instruction is used to instruct to execute the service from the code position of the service corresponding to the scheduler; The second jump instruction is executed.

2. The service processing method according to claim 1, characterized in that: The method further comprises: While executing the first jump instruction, calling a system timing function to send a target instruction to the operating system, the target instruction is used to instruct the operating system to send a switching instruction signal to the target coroutine after receiving the target instruction for a first preset time; If the target coroutine generates a second jump instruction based on the context information of the scheduler when receiving the switching indication signal, it is confirmed that the duration of the target coroutine executing the corresponding business reaches a first preset duration.

3. The service processing method according to claim 2, characterized in that: The method also includes: When it is determined that the duration of the target coroutine executing the corresponding business reaches a first preset duration, the execution switching processing function is called to register a timer of a certain specified duration in the target coroutine, wherein if the timing duration of the timer indicates that the specified duration has been reached, it indicates that the target coroutine can be run.

4. The method according to claim 1, characterized in that The current state parameters of the business execution coroutine include the priority of the business execution coroutine, the timing duration of the timer in the business execution coroutine, and the state parameters of the triggering event for the business execution coroutine; Determining an executable target coroutine from the multiple business execution coroutines according to the current state parameters of each business execution coroutine using the scheduler includes: using the scheduler to perform the following steps: Determine a candidate business execution coroutine from the multiple business execution coroutines according to the time parameter of the timer in each of the business execution coroutines and the state parameter of the corresponding trigger event, wherein the timing duration of the timer in the candidate business execution coroutine indicates that the timer has timed out, or the state parameter of the trigger event for the candidate business indicates that the trigger event has occurred; If there are multiple candidate business execution coroutines, a candidate business execution coroutine with the highest priority is determined from the multiple candidate business execution coroutines as the executable target coroutine.

5. The method according to claim 4, characterized in that The step of determining the candidate service execution coroutine with the highest priority from among the plurality of candidate service execution coroutines as the executable target coroutine includes: Determine a candidate service execution coroutine with the highest priority from the plurality of candidate service execution coroutines; If there are multiple candidate business execution coroutines with the highest priority, the candidate business execution coroutine with the longest suspension time is selected from the candidate business execution coroutines with the highest priority as the target coroutine.

6. The method according to claim 1, characterized in that The multiple business execution coroutines are created in the target thread, the current state parameters of the business execution coroutines include the priority of the business execution coroutines, and the method further includes: using the scheduler to perform the following steps: Obtaining the number of times each of the business execution coroutines runs within the life cycle of the target thread at intervals of a second preset time; The priority of each business execution coroutine is adjusted according to the number of runs corresponding to each business execution coroutine, and the number of runs is negatively correlated with the priority.

7. The method according to claim 1, characterized in that The method further comprises: using the scheduler to perform the following steps: If it is determined that the business corresponding to the target coroutine has been executed, the target coroutine is recycled.

8. A service processing device, characterized in that: Applied to an operating system, the operating system is deployed with a multi-coroutine framework, the multi-coroutine framework includes a scheduler and a plurality of business execution coroutines, the scheduler is used to schedule the plurality of business execution coroutines, each of the business execution coroutines corresponds to a business, and the device includes: A parameter acquisition module, used to use the scheduler to obtain the current state parameters of each business execution coroutine; A coroutine determination module, configured to determine an executable target coroutine from the plurality of business execution coroutines according to the current state parameters of each business execution coroutine using the scheduler; A first instruction generation module, used to generate a first jump instruction based on the context information of the target coroutine by using the scheduler, wherein the first jump instruction carries the code position of the business corresponding to the target coroutine, and the first jump instruction is used to instruct to execute the business from the code position of the business corresponding to the target coroutine; A first instruction execution module, used for executing the first jump instruction to enable the target coroutine to execute the corresponding business; A second instruction generating module, configured to generate a second jump instruction using the target coroutine based on the context information of the scheduler when the duration of the target coroutine executing the corresponding service reaches a first preset duration, wherein the second jump instruction carries the code position of the service corresponding to the scheduler, and the second jump instruction is used to instruct to execute the service from the code position of the service corresponding to the scheduler; The second instruction execution module is used to execute the second jump instruction.

9. An electronic device, characterized in that: include: one or more processors; Memory; One or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs are configured to execute the method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores program codes, and the program codes can be called by a processor to execute the method according to any one of claims 1 to 7.

11. A computer program product comprising a computer program / instructions, characterized in that When the computer program / instructions are executed by a processor, the steps of the method described in any one of claims 1 to 7 are implemented.

Citation Information

Patent Citations

  • IPMI service execution method and device and storage medium

    CN113553172A

  • Coroutine switching method and device and equipment

    CN114090196A

  • Coroutine monitoring method, device and equipment and readable storage medium

    CN117519947A