Asynchronous task processing method and device, equipment, medium and program product
By using the timer control and execution pointer variable in the QML asynchronous task queue to asynchronously schedule tasks, the race conditions and deadlock problems caused by frequent communication in traditional asynchronous task processing are solved, and stability and flexibility are improved.
Patent Information
- Application Number
- CN202510745799.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-04
- Publication Date
- 2025-10-10
AI Technical Summary
Traditional asynchronous task processing in QML leads to race conditions or deadlocks due to frequent communication, and thread management is complex. In particular, QML's user interface elements can only be accessed in the main thread, resulting in interface lag.
Use the timer control to set the execution pointer variable and loop trigger variable in the asynchronous task queue, and asynchronously schedule tasks through event callback signals. The execution function contains a unique identification code, and the task order is determined according to the function return value. It supports custom execution order and generates an execution history stack to handle exceptions.
It reduces the occurrence of race conditions and deadlocks, simplifies thread management, supports custom execution order, avoids main thread access restrictions, and improves program stability and flexibility.
Smart Images

Figure CN120762832A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application belongs to the technical field of data communication, and particularly relates to a processing method and device of asynchronous tasks, equipment, medium and program product. BACKGROUND
[0002] At present, the implementation of the asynchronous task queue of the declarative programming language QML (Qt Meta-Object Language) originates from the urgent demand of the cross-platform application development framework Qt for high-performance and responsive interface development. With the popularity of multi-core processors, QML, as the declarative user interface language of Qt, needs to process time-consuming tasks such as image rendering, file input / output, network request, etc., but the traditional single-threaded model will cause the main event loop to be blocked, causing interface lag. Qt early solves this problem through the user script WorkerScript, the concurrent module QtConcurrent and other scattered multi-threaded solutions, but these methods have problems such as code fragmentation and complex thread management.
[0003] Moreover, since the user interface UI elements and some components of QML can only be accessed in the main thread, directly operating QML objects in the sub-thread will cause a crash, and cross-thread communication needs to be performed through the signal-slot mechanism, but frequent communication may introduce race conditions or deadlocks. SUMMARY
[0004] The embodiments of the present application provide a processing method, device, equipment, medium and program product of asynchronous tasks, which can solve the technical problems of race conditions or deadlocks introduced due to frequent communication and complex thread management in the traditional asynchronous task processing process.
[0005] In a first aspect, the embodiments of the present application provide a processing method of asynchronous tasks, which comprises:
[0006] In the case that there are multiple to-be-executed tasks in the asynchronous task queue, a timer is started, wherein the timer is provided with an execution pointer variable and a loop trigger variable, and the execution pointer variable is used to identify the index of the current to-be-executed task;
[0007] After the timer is started for a preset time interval, the parameter value of the execution pointer variable in the timer is obtained in response to an event callback signal in the timer;
[0008] According to the parameter value of the execution pointer variable, an execution function corresponding to the current to-be-executed task is executed, and a function return value corresponding to the current to-be-executed task is obtained; wherein the execution function contains a unique identification code, and the unique identification code is associated with the parameter value of the execution pointer variable;
[0009] The execution order of the current task to be executed and the next task to be executed is determined according to the function return value, and the timer is stopped when the parameter value of the execution pointer variable meets the preset end condition.
[0010] In a possible implementation of the first aspect, determining the execution order of the current task to be executed and the next task to be executed according to the function return value includes:
[0011] If the function return value meets the expected value, the parameter value of the execution pointer variable is automatically increased by a first preset value, and the next task to be executed is executed;
[0012] If the function return value does not meet the expected value, the parameter value of the execution pointer variable remains unchanged, and the execution function corresponding to the current task to be executed continues to be executed to obtain the second function return value corresponding to the current task to be executed.
[0013] In a possible implementation manner of the first aspect, before starting the timer, the method further includes:
[0014] The execution pointer variable, the loop trigger variable, the callback function in the event callback signal, and the time interval variable are set in the timer through declarative syntax; wherein the time interval variable is set to the preset time interval, the parameter value of the execution pointer variable is initialized, and the parameter value of the loop trigger variable is set to true;
[0015] Define the execution functions corresponding to multiple tasks to be executed.
[0016] In a possible implementation manner of the first aspect, before starting the timer, the method further includes:
[0017] Setting a safety fallback variable in the timer;
[0018] When the parameter value of the execution pointer variable exceeds a second preset value, the parameter value of the safety fallback variable is reset.
[0019] In a possible implementation of the first aspect, after executing the execution function corresponding to the current task to be executed according to the parameter value of the execution pointer variable and obtaining the function return value corresponding to the current task to be executed, the method further includes:
[0020] An execution history stack is generated so that when the task to be executed fails or is in an abnormal state, a rollback operation or a breakpoint resume operation is performed according to the execution history stack, wherein the execution history stack is used to store the historical parameter values of the execution pointer variable.
[0021] In a second aspect, an embodiment of the present application provides a device for processing asynchronous tasks, the device comprising:
[0022] A starting module is used to start a timer when there are multiple tasks to be executed in the asynchronous task queue, wherein the timer is set with an execution pointer variable and a loop trigger variable, and the execution pointer variable is used to identify the index of the current task to be executed;
[0023] an acquisition module, configured to acquire a parameter value of the execution pointer variable in the timer in response to an event callback signal in the timer after the timer starts for a preset time interval;
[0024] an execution module, configured to execute an execution function corresponding to the current task to be executed according to the parameter value of the execution pointer variable, and obtain a function return value corresponding to the current task to be executed; wherein the execution function includes a unique identification code, and the unique identification code is associated with the parameter value of the execution pointer variable;
[0025] The determination module is used to determine the execution order of the current task to be executed and the next task to be executed according to the function return value, and stop the timer when the parameter value of the execution pointer variable meets the preset end condition.
[0026] In a possible implementation of the second aspect, the apparatus further includes:
[0027] A history stack creation module is used to generate an execution history stack so that when the task to be executed fails or is in an abnormal state, a rollback operation or a breakpoint resume operation can be performed according to the execution history stack, wherein the execution history stack is used to store the historical parameter values of the execution pointer variable.
[0028] In a third aspect, an embodiment of the present application provides a terminal device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements any of the above-described methods for processing asynchronous tasks when executing the computer program.
[0029] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method for processing an asynchronous task described in any one of the above items is implemented.
[0030] In a fifth aspect, an embodiment of the present application provides a computer program product, which, when running on a terminal device, enables the terminal device to execute the asynchronous task processing method described in any one of the above-mentioned first aspects.
[0031] Compared with the prior art, the embodiments of the present application have the following beneficial effects:
[0032] An embodiment of the present application provides a method for processing asynchronous tasks, comprising: when multiple pending tasks exist in an asynchronous task queue, starting a timer, wherein the timer is configured with an execution pointer variable and a loop trigger variable, the execution pointer variable being used to identify the index of the current pending task. Then, after a preset time interval has elapsed since the timer was started, in response to an event callback signal in the timer, obtaining a parameter value of the execution pointer variable in the timer. Based on the parameter value of the execution pointer variable, executing an execution function corresponding to the current pending task to obtain a function return value corresponding to the current pending task; wherein the execution function includes a unique identification code associated with the parameter value of the execution pointer variable. Finally, based on the function return value, determining the execution order of the current pending task and the next pending task, and stopping the timer until the parameter value of the execution pointer variable meets a preset termination condition. This method uses a timer to asynchronously schedule tasks, significantly reducing race conditions or deadlocks because it does not require thread initiation. It also circumvents the problem that QML user interface elements and some components can only be accessed from the main thread. This solves the technical problems of frequent communication in traditional asynchronous task processing, which can introduce race conditions or deadlocks and complex thread management. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the embodiments or descriptions of the prior art. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0034] Figure 1 This is a flowchart of a method for processing asynchronous tasks provided by an embodiment of the present application;
[0035] Figure 2 This is a flowchart of a method for processing an asynchronous task provided by another embodiment of the present application;
[0036] Figure 3 This is a schematic diagram of the structure of an asynchronous task processing device provided by an embodiment of the present application;
[0037] Figure 4 This is a structural diagram of a terminal device provided in one embodiment of the present application. DETAILED DESCRIPTION
[0038] In the following description, for purposes of explanation and not limitation, specific details are set forth such as particular architectures, techniques, etc. in order to provide a thorough understanding of the embodiments of the present application. However, it will be apparent to those skilled in the art that the present application can be practiced in other embodiments that depart from these specific details. In other instances, detailed descriptions of well-known methods, devices, circuits, and
[0039] It is to be understood that the terminology "includes", "has", "holds", "contains" or "comprising", "including", "having" and the like, when used in the present specification and in the accompanying claims, are used in the sense of "including but not limited to", "including but not limited to", "including but not limited to" and "including but not limited to" respectively, and should be construed as specifically setting forth the stated features, integers, steps or components but not precluding one or more additional features, integers, steps, components and / or groups thereof.
[0040] It is also to be understood that the terminology "and / or" when used in the present specification and in the accompanying claims, refers to one and / or all possible combinations of one or more of the associated listed items, and includes all possible combinations.
[0041] As used in the present specification and in the accompanying claims, the term "if" can be interpreted as meaning "when" or "once" or "in response to a determination" or "in response to detecting" depending on the context. Similarly, the phrase "if it is determined" or "if [a described condition or event] is detected" can be interpreted to mean "once it is determined" or "in response to a determination" or "once [the described condition or event] is detected" or "in response to detecting [a described condition or event]", depending on the context.
[0042] In addition, the terms "first", "second", "third", etc. in the description of the present specification and the accompanying claims are only used to distinguish descriptions, and cannot be understood as indicating or implying relative importance.
[0043] Reference in the specification to "one embodiment" or "some embodiments" means that a particular feature, structure, or characteristic described in connection with the embodiment is included in at least one embodiment of the application. The appearances of the phrase "in one embodiment" or "in some embodiments" in various places in the specification are not necessarily all referring to the same embodiment, although it can. The terms "including", "containing", "having" and variations thereof mean "including but not limited to", unless expressly specified otherwise.
[0044] QML (Qt Meta-Object Language) is a declarative programming language for designing user interfaces and applications. QML is part of the cross-platform application development framework Qt and provides a concise and intuitive way to describe user interfaces and interaction logic.
[0045] Currently, there are some major problems with the implementation of the QML asynchronous task queue in the Qt framework:
[0046] (1) QML's user interface UI elements and some components can only be accessed in the main thread. Directly operating QML objects in child threads will cause crashes. Cross-thread communication is required through the signal-slot mechanism, but frequent communication may introduce race conditions or deadlocks.
[0047] (2) When combining the concurrent module QtConcurrent or thread pool QThreadPool in the Qt framework, it is necessary to deal with the complexity of the interaction between QML and C++ objects, such as exposing the interface through the context property. However, this operation is too complicated and the logical implementation is difficult to control.
[0048] (3) The default task queue is usually executed in a first-in-first-out (FIFO) manner and lacks a priority mechanism, so a custom scheduling algorithm needs to be implemented.
[0049] To address the above problems, an embodiment of the present application provides a custom asynchronous control process based on a timer control. Since this control process does not require starting a thread, it can significantly reduce race conditions or deadlocks, while bypassing the problem that QML's UI elements and some components can only be accessed in the main thread. Moreover, this control process supports custom execution order, avoiding QML's unreasonable optimization of the priority of some functions.
[0050] See also Figure 1 , Figure 1 This is a flow chart of a method for processing asynchronous tasks provided by an embodiment of the present application. The method includes:
[0051] S11. When there are multiple tasks to be executed in the asynchronous task queue, start a timer, wherein the timer is set with an execution pointer variable and a loop trigger variable, and the execution pointer variable is used to identify the index of the current task to be executed.
[0052] S12: After the timer starts for a preset time interval, in response to an event callback signal in the timer, obtain a parameter value of the execution pointer variable in the timer.
[0053] S13, execute an execution function corresponding to the current to-be-executed task according to the parameter value of the execution pointer variable, to obtain a function return value corresponding to the current to-be-executed task; wherein the execution function comprises a unique identification code, and the unique identification code is associated with the parameter value of the execution pointer variable.
[0054] S14, determine the execution order of the current to-be-executed task and the next to-be-executed task according to the function return value, and stop the timer when the parameter value of the execution pointer variable meets a preset end condition.
[0055] It should be noted that in the embodiment, the execution subject can be a terminal device such as a server, and no specific limitation is made thereto.
[0056] In step S11, when there are multiple to-be-executed tasks in the QML asynchronous task queue, a timer is started. The timer is a custom timer Timer control created in the QML framework, which is a mechanism for periodically triggering a callback function. The Timer control is provided with an execution pointer variable currentIndex and a loop triggering variable repeat.
[0057] Further, before starting the timer, the method further comprises:
[0058] By declarative syntax, the execution pointer variable currentIndex, the loop triggering variable repeat, the callback function in the event callback signal onTriggered, and the time interval variable are set in the timer; wherein the time interval variable is set to a preset time interval, the parameter value of the execution pointer variable currentIndex is initialized, and the parameter value of the loop triggering variable repeat is set to true.
[0059] Define a plurality of execution functions corresponding to the to-be-executed tasks.
[0060] Specifically, the execution pointer variable currentIndex is the current selected item, i.e. the current to-be-executed task. The parameter value of the execution pointer variable currentIndex is the index of the current to-be-executed task, which can locate the current to-be-executed task, thereby controlling the execution logic of the asynchronous task queue. When the timer is started, the parameter value of the execution pointer variable currentIndex needs to be initialized, and generally the parameter value of the execution pointer variable currentIndex is initialized to 0. At the same time, the Number type of the script JavaScript is used to ensure that the execution pointer variable currentIndex is always treated as a numerical type during numerical operation, rather than a string or other type. It should be understood that by using the Number type, numerical operation errors caused by type errors or illegal operations can be avoided, thereby ensuring the stability and reliability of the program.
[0061] The loop trigger variable repeat controls whether the timer triggers continuously to avoid invalid polling. When starting the timer, set the loop trigger variable repeat parameter value to true (i.e., repeat: True). When the repeat parameter value is set to True, the Timer control automatically resets after each trigger and waits for the next time interval, implementing the timer's continuous polling mechanism.
[0062] The time interval variable is set to a preset time interval. In the timer, the preset time interval is set by setting the Interval property to implement a mechanism for periodically triggering the callback function. That is, by setting the preset time interval, the time interval for the Timer control to trigger a signal or execute a function can be set. For example, to ensure response speed, the preset time interval can be 200ms-500ms.
[0063] The event callback signal onTriggered is a signal processing function in the timer. When the timer starts, onTriggered is automatically triggered, calling the corresponding function. This function is the execution function corresponding to multiple tasks to be executed.
[0064] In step S12, when the timer starts the preset time interval, the event callback signal onTriggered is triggered, and the parameter value of the execution pointer variable currentIndex is obtained to determine the execution function corresponding to the current task to be executed according to the parameter value of the execution pointer variable currentIndex.
[0065] In step S13, these execution functions contain unique identification codes, which are globally unique identifiers for the tasks to be executed and are used to associate the tasks with the execution logic. This unique identification code is associated with the parameter value of the execution pointer variable currentIndex, so that the corresponding execution function can be determined based on the parameter value of the execution pointer variable currentIndex. The return values of these execution functions are clearly defined in advance to determine whether to proceed to the next function when the function is repeatedly called.
[0066] When defining execution functions for multiple pending tasks, you can construct an expandable functionQueue array to store references to the execution functions for these pending tasks. In the onTriggered callback function, you can select and execute the corresponding function from the functionQueue array based on the value of the execution pointer variable currentIndex. The function return value is the result of the function execution. Function return values are typically Boolean values (true or false), but other types of values can be returned depending on specific needs. Function return values can be used to control the execution flow of the program, identify errors, and implement appropriate error handling, making the program more flexible and stable, and better able to handle various execution situations and abnormal conditions.
[0067] It should also be noted that in this embodiment, in order to determine whether the parameter values and return values of the executed function are legal and whether the function call is time-consuming, the console.time variable can also be set. The console.time variable is used to detect the execution time of the executed function, ensuring that the function is not too complex and resource-intensive, thereby avoiding main thread lag.
[0068] In step S14, the function presets the end condition as the condition for stopping the timer, such as the queue being empty, the number of execution errors exceeding the limit, a forced termination signal, etc.
[0069] Specifically, after the timer is started, the time callback signal onTriggered is automatically called after the timer's preset time interval expires. At this time, the parameter value of the execution pointer variable currentIndex in the timer is obtained. Based on the correlation between the parameter value of the execution pointer variable currentIndex and the unique identification code of the execution function, the execution function corresponding to the current pending task is determined. This execution function is then executed to obtain the function return value corresponding to the current pending task. Finally, based on the function return value, it is determined whether to execute the next pending task or continue executing the current pending task in the next cycle. The timer is stopped until the parameter value of the execution pointer variable meets the preset end condition.
[0070] It will be appreciated that embodiments of the present application provide a method for processing asynchronous tasks, comprising: when multiple pending tasks exist in an asynchronous task queue, starting a timer, wherein the timer is configured with an execution pointer variable and a loop trigger variable, the execution pointer variable being used to identify the index of the current pending task. Thereafter, after a preset time interval has elapsed since the timer was started, in response to an event callback signal from the timer, obtaining a parameter value of the execution pointer variable in the timer. Based on the parameter value of the execution pointer variable, executing an execution function corresponding to the current pending task, obtaining a function return value corresponding to the current pending task; wherein the execution function includes a unique identification code associated with the parameter value of the execution pointer variable. Finally, based on the function return value, determining the execution order of the current pending task and the next pending task, and stopping the timer until the parameter value of the execution pointer variable meets a preset termination condition. This method uses a timer to asynchronously schedule tasks. Because it does not require thread initiation, it can significantly reduce race conditions or deadlocks, while also circumventing the issue that QML user interface elements and some components can only be accessed from the main thread. This solves the technical issues of frequent communication in traditional asynchronous task processing, which can introduce race conditions or deadlocks and complex thread management.
[0071] In one possible implementation, determining the execution order of the current task to be executed and the next task to be executed based on the function return value includes:
[0072] If the function return value meets the expected value, the parameter value of the execution pointer variable is automatically increased by the first preset value, and the next task to be executed is executed.
[0073] If the function return value does not meet the expected value, the parameter value of the execution pointer variable remains unchanged, and the execution function corresponding to the current task to be executed continues to be executed to obtain the second function return value corresponding to the current task to be executed.
[0074] It should be noted that the expected value is the value expected after the current pending task completes. If the function return value meets the expected value, i.e., the current pending task is completed, the parameter value of the execution pointer variable currentIndex is automatically increased by a first preset value, thereby proceeding to the next pending task. The first preset value is a preset value and can be 1 or other values.
[0075] When the function return value does not meet the expected value, that is, the execution result of the current task to be executed is not satisfactory, the current task to be executed will continue to be executed in the next cycle, and the execution result obtained is the second function return value. At this time, the parameter value of the execution pointer variable currentIndex remains unchanged.
[0076] When the parameter value of the execution pointer variable currentIndex meets the preset end condition, the timer stops and the processing of the asynchronous task ends.
[0077] It should also be noted that during the execution process, you can also customize the execution interval of the next function, and when the execution interval is reached, the signal for the execution of the next function is triggered.
[0078] like Figure 2 As shown, Figure 2 This is a flowchart of a method for processing asynchronous tasks provided in another embodiment of the present application. Figure 2 In the asynchronous task queue, if there are multiple pending tasks, the Timer control is enabled. Before the Timer control is enabled in step 21, a preset time interval is set using the Interval property, and the parameter value of the loop trigger variable repeat is configured to true to implement a continuous polling mechanism. Simultaneously, the parameter value of the execution pointer variable currentIndex is initialized to 0, and the JavaScript Number type is used to ensure numerical calculation safety. In step S22, when the event callback signal onTriggered is triggered, the parameter value of the execution pointer variable currentIndex is determined. In step S23, the execution function for the current pending task is selected based on the parameter value of the execution pointer variable currentIndex. In this step, any number and order of execution functions can be customized. After the execution function is executed, the return value of the function execution is used to determine whether it meets the expected value. If the function return value meets the expected value, the process proceeds to step S24. In step S24, the parameter value of the execution pointer variable currentIndex is determined based on the function return value to determine whether it is self-incremented. This step is crucial for determining whether to proceed to the next function. After the parameter value of currentIndex is incremented, proceed to step S25. If the function return value does not meet the expected value, the next few steps will be automatically ignored according to the logic, and the process will return to step S22 to continue executing the current task to be executed. In step S25, a preset end condition that defines the entire logic or the execution interval of the next function can be customized. It can be set to trigger the end signal finish() when the execution pointer variable currentIndex reaches the last valid index terminalIndex. This end signal can be bound to the timer end variable Timer.stop() or perform resource recovery operations.
[0079] It should be understood that this embodiment uses timers to asynchronously schedule tasks, eliminating the need to start threads. This significantly reduces race conditions and deadlocks, while also addressing the issue of QML user interface elements and some components being accessible only from the main thread. Furthermore, support for customizable execution order prevents QML from irrationally optimizing the priorities of some functions. This resolves the technical issues of traditional asynchronous task processing, which often result from frequent communication leading to race conditions and deadlocks, as well as complex thread management.
[0080] Before starting the timer, the method further includes:
[0081] Set the safety fallback variable in the timer.
[0082] When the parameter value of the execution pointer variable exceeds a second preset value, the parameter value of the safety fallback variable is reset.
[0083] It should be noted that, in this embodiment, a safety fallback variable fallbackIndex may also be set in the timer control. The safety fallback variable may serve as a safety fallback point in an abnormal state to prevent the overflow of the execution pointer variable currentIndex from causing a program crash.
[0084] Specifically, when the parameter value of the execution pointer variable currentIndex exceeds a second preset value, the parameter value of the safety fallback variable fallbackIndex is reset to prevent the overflow of the execution pointer variable currentIndex from causing a program crash. The second preset value is a pre-set value, and generally, the second preset value can be the total number of tasks to be executed in the abnormal task queue.
[0085] In one possible implementation, after executing the execution function corresponding to the current task to be executed according to the parameter value of the execution pointer variable and obtaining the function return value corresponding to the current task to be executed, the method further includes:
[0086] An execution history stack is generated so that when the task to be executed fails or is in an abnormal state, a rollback operation or a breakpoint resume operation can be performed according to the execution history stack, wherein the execution history stack is used to store historical parameter values of the execution pointer variable.
[0087] It should be noted that the execution history stack can store the historical parameter values of the execution pointer variable, that is, it can store the index track of each pending task. Through this execution history stack, error rollback or breakpoint resumption can be performed when the pending task fails or is in an abnormal state.
[0088] Specifically, after onTriggered is triggered, the execution history stack begins recording the current state. The execution history stack can be dynamically updated after the function executes successfully or fails. If a pending task fails, it can be rolled back to the previous valid state in the execution history stack. Before the program exits, the execution history stack can be saved to a file. When the program is restarted after an interruption, the execution history stack can be loaded from the file and the unfinished execution flow can be resumed. Furthermore, the information recorded in the execution history stack, such as the task execution path, facilitates analysis of any issues.
[0089] It should be understood that by properly setting the execution history stack, the maintainability of the timer polling logic can be significantly improved.
[0090] It should be noted that the embodiments of the present application can be applied to a variety of scenarios, such as: (1) Scenarios where tasks need to be executed asynchronously (such as network requests, animation sequences, data loading) under the QML framework and where main thread blocking needs to be avoided, such as implementing multi-step animation switching and dynamic UI element loading in lightweight user interface framework Qt Quick applications. (2) Application development that requires compatibility with multiple platforms such as Android, iOS, and Windows, especially resource-constrained mobile terminals, such as initializing plug-ins in stages and loading local database content on demand. (3) Device status polling, sensor data acquisition and processing (such as segmented analysis of temperature sensor data), hardware control instruction sequence execution, etc. (4) Step-by-step execution of game logic (such as character skill combos, level task triggering), and synchronous playback control of audio and video streams. (5) Scenarios with low concurrency and high maintainability requirements, such as lightweight UI animation, IoT device polling, and data acquisition.
[0091] It should be understood that the size of the serial numbers of the steps in the above embodiments does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application.
[0092] Corresponding to a method for processing an asynchronous task in the above embodiment, Figure 3 A structural diagram of an asynchronous task processing device provided by an embodiment of the present application is shown. For ease of explanation, only the parts related to the embodiment of the present application are shown.
[0093] Reference Figure 3 The asynchronous task processing device 3 of this embodiment includes:
[0094] The starting module 31 is used to start a timer when there are multiple tasks to be executed in the asynchronous task queue, wherein the timer is set with an execution pointer variable and a loop trigger variable, and the execution pointer variable is used to identify the index of the current task to be executed.
[0095] The acquisition module 32 is configured to acquire a parameter value of an execution pointer variable in the timer in response to an event callback signal in the timer after the timer starts for a preset time interval.
[0096] The execution module 33 is used to execute the execution function corresponding to the current task to be executed according to the parameter value of the execution pointer variable, and obtain the function return value corresponding to the current task to be executed; wherein the execution function includes a unique identification code, and the unique identification code is associated with the parameter value of the execution pointer variable.
[0097] The determination module 34 is used to determine the execution order of the current task to be executed and the next task to be executed according to the function return value, and stop the timer when the parameter value of the execution pointer variable meets the preset end condition.
[0098] It will be appreciated that in this embodiment, the asynchronous task processing device 3 starts a timer via a startup module 31 when multiple pending tasks exist in the asynchronous task queue. The timer is configured with an execution pointer variable and a loop trigger variable, and the execution pointer variable is used to identify the index of the current pending task. Subsequently, after a preset time interval has passed since the timer was started, the acquisition module 32, in response to an event callback signal from the timer, acquires the parameter value of the execution pointer variable in the timer. Based on the parameter value of the execution pointer variable, the execution module 33 executes the execution function corresponding to the current pending task and obtains the function return value corresponding to the current pending task. The execution function includes a unique identification code that is associated with the parameter value of the execution pointer variable. Finally, the determination module 34 determines the execution order of the current pending task and the next pending task based on the function return value, and stops the timer until the parameter value of the execution pointer variable meets a preset termination condition. This device uses a timer to asynchronously schedule tasks. Because it does not require thread initiation, it can significantly reduce race conditions or deadlocks and circumvents the issue of QML user interface elements and some components being accessible only in the main thread. This solves the technical problems of introducing race conditions or deadlocks and complex thread management due to frequent communication in traditional asynchronous task processing.
[0099] Furthermore, the determination module 34 includes:
[0100] The first determining subunit is used to automatically increase the parameter value of the execution pointer variable by a first preset value and execute the next task to be executed if the function return value meets the expected value.
[0101] The second determining subunit is used to keep the parameter value of the execution pointer variable unchanged if the function return value does not meet the expected value, and continue to execute the execution function corresponding to the current task to be executed to obtain the second function return value corresponding to the current task to be executed.
[0102] Furthermore, the asynchronous task processing device 3 further includes:
[0103] The timer establishment module is used to set the execution pointer variable, loop trigger variable, callback function in the event callback signal and time interval variable in the timer through declarative syntax; wherein the time interval variable is set to the preset time interval, the parameter value of the execution pointer variable is initialized, and the parameter value of the loop trigger variable is set to true.
[0104] The function definition module is used to define the execution functions corresponding to multiple tasks to be executed.
[0105] Furthermore, the asynchronous task processing device 3 further includes:
[0106] A safety fallback variable setting module is used to set a safety fallback variable in a timer;
[0107] The safety fallback variable resetting module is used to reset the parameter value of the safety fallback variable when the parameter value of the execution pointer variable exceeds a second preset value.
[0108] In a possible implementation, the asynchronous task processing device 3 further includes:
[0109] The history stack creation module is used to generate an execution history stack so that when the task to be executed fails or is in an abnormal state, a rollback operation or a breakpoint resume operation can be performed according to the execution history stack, wherein the execution history stack is used to store the historical parameter values of the execution pointer variable.
[0110] It should be noted that the information interaction, execution process, etc. between the modules in the above-mentioned asynchronous task processing device 3 are based on the same concept as the method embodiment of the present application. Their specific functions and technical effects can be found in the method embodiment part and will not be repeated here.
[0111] The present application also provides a terminal device, such as Figure 4 As shown, Figure 4 This is a schematic diagram of the structure of a terminal device provided in one embodiment of the present application. Figure 4 The terminal device 4 of this embodiment includes: a memory 41, a processor 42, and a computer program stored in the memory 41 and executable on the processor 42. When the processor 42 executes the computer program, the steps of any one of the above-mentioned asynchronous task processing method embodiments are implemented.
[0112] An embodiment of the present application further provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, the steps in the above-mentioned method embodiments can be implemented.
[0113] An embodiment of the present application provides a computer program product. When the computer program product is run on a mobile terminal, the mobile terminal can implement the steps in the above-mentioned various method embodiments when executing the computer program product.
[0114] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present application can implement all or part of the process steps in the above-mentioned method embodiments by using a computer program to instruct the relevant hardware. The computer program can be stored in a computer-readable storage medium. When executed by a processor, the computer program can implement the steps of each of the above-mentioned method embodiments. The computer program includes computer program code, which can be in source code form, object code form, executable file, or some intermediate form. The computer-readable medium can at least include: any entity or device capable of carrying computer program code to the camera / terminal device, recording medium, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal, and software distribution medium. Examples include USB flash drives, mobile hard drives, magnetic disks, or optical disks. In some jurisdictions, based on legislation and patent practice, computer-readable media cannot be electric carrier signals or telecommunication signals.
[0115] In the above embodiments, the description of each embodiment has its own focus. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant description of other embodiments.
[0116] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0117] In the embodiments provided in this application, it should be understood that the disclosed devices / network equipment and methods can be implemented in other ways. For example, the device / network equipment embodiments described above are merely schematic. For example, the division of modules or units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0118] Units described as separate components may or may not be physically separate, and components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of these units may be selected to achieve the purpose of this embodiment according to actual needs.
[0119] The above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the various embodiments of the present application, and should all be included in the scope of protection of the present application.
Claims
1. A method for processing asynchronous tasks, characterized in that: include: When there are multiple tasks to be executed in the asynchronous task queue, start a timer, wherein the timer is set with an execution pointer variable and a loop trigger variable, and the execution pointer variable is used to identify the index of the current task to be executed; After the timer starts for a preset time interval, in response to an event callback signal in the timer, obtaining a parameter value of the execution pointer variable in the timer; Executing an execution function corresponding to the current task to be executed according to the parameter value of the execution pointer variable to obtain a function return value corresponding to the current task to be executed; wherein the execution function includes a unique identification code, and the unique identification code is associated with the parameter value of the execution pointer variable; The execution order of the current task to be executed and the next task to be executed is determined according to the function return value, and the timer is stopped when the parameter value of the execution pointer variable meets the preset end condition.
2. The method for processing asynchronous tasks according to claim 1, wherein: Determining the execution order of the current task to be executed and the next task to be executed according to the function return value includes: If the function return value meets the expected value, the parameter value of the execution pointer variable is automatically increased by a first preset value, and the next task to be executed is executed; If the function return value does not meet the expected value, the parameter value of the execution pointer variable remains unchanged, and the execution function corresponding to the current task to be executed continues to be executed to obtain the second function return value corresponding to the current task to be executed.
3. The method for processing an asynchronous task according to claim 2, wherein: Before starting the timer, the method further includes: The execution pointer variable, the loop trigger variable, the callback function in the event callback signal, and the time interval variable are set in the timer through declarative syntax; wherein the time interval variable is set to the preset time interval, the parameter value of the execution pointer variable is initialized, and the parameter value of the loop trigger variable is set to true; Define the execution functions corresponding to multiple tasks to be executed.
4. The method for processing an asynchronous task according to claim 1, wherein: Before starting the timer, the method further includes: Setting a safety fallback variable in the timer; When the parameter value of the execution pointer variable exceeds a second preset value, the parameter value of the safety fallback variable is reset.
5. The method for processing an asynchronous task according to any one of claims 1 to 4, characterized in that: After executing the execution function corresponding to the current task to be executed according to the parameter value of the execution pointer variable and obtaining the function return value corresponding to the current task to be executed, the method further includes: An execution history stack is generated so that when the task to be executed fails or is in an abnormal state, a rollback operation or a breakpoint resume operation is performed according to the execution history stack, wherein the execution history stack is used to store the historical parameter values of the execution pointer variable.
6. A device for processing asynchronous tasks, characterized in that: include: A starting module is used to start a timer when there are multiple tasks to be executed in the asynchronous task queue, wherein the timer is set with an execution pointer variable and a loop trigger variable, and the execution pointer variable is used to identify the index of the current task to be executed; an acquisition module, configured to acquire a parameter value of the execution pointer variable in the timer in response to an event callback signal in the timer after the timer starts for a preset time interval; an execution module, configured to execute an execution function corresponding to the current task to be executed according to the parameter value of the execution pointer variable, and obtain a function return value corresponding to the current task to be executed; wherein the execution function includes a unique identification code, and the unique identification code is associated with the parameter value of the execution pointer variable; The determination module is used to determine the execution order of the current task to be executed and the next task to be executed according to the function return value, and stop the timer when the parameter value of the execution pointer variable meets the preset end condition.
7. The asynchronous task processing device according to claim 6, characterized in that: The device further comprises: A history stack creation module is used to generate an execution history stack so that when the task to be executed fails or is in an abnormal state, a rollback operation or a breakpoint resume operation can be performed according to the execution history stack, wherein the execution history stack is used to store the historical parameter values of the execution pointer variable.
8. A terminal device, characterized in that: The method comprises a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the method according to any one of claims 1 to 5 when executing the computer program.
9. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 5 is implemented.
10. A computer program product, characterized in that The invention comprises a computer program which, when executed, causes the method according to any one of claims 1 to 5 to be performed.