A method, device and electronic equipment for processing a time-delayed task
By calculating the time difference between delayed tasks and the system, and using a combination of time wheels and databases to process delayed tasks, the problems of inaccurate execution of delayed tasks and high database pressure are solved, achieving high-precision task execution and system performance optimization.
Patent Information
- Application Number
- CN202210803517.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-07-07
- Publication Date
- 2026-01-30
- Estimated Expiration
- 2042-07-07
AI Technical Summary
In existing technologies, the execution accuracy of delayed tasks is not high, and frequent database polling has a significant impact on performance. As the number of tasks increases, the accurate execution of massive delayed tasks becomes a problem.
By calculating the time difference between the delayed task and the current system time, the task to be executed is determined and delivered to the slot of the time wheel. The time wheel is used to execute the task, reducing database polling. A sub-time wheel is set up to handle task overload in the slot. Combined with database storage of delayed tasks, task execution is optimized.
It improves the accuracy of delayed task execution, reduces database operation pressure, and ensures system performance and task execution accuracy.
Smart Images

Figure CN115168005B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and more specifically, to a method, processing device, and electronic device for processing delayed tasks. Background Technology
[0002] With the development of internet technology, some business scenarios involve numerous delayed tasks that must be executed at specified times. Examples include checking payment status 10 minutes after an order is placed, or checking usage of an activity invitation code one hour after it is sent. If the execution time of a delayed task has not yet arrived, the task needs to be suspended until then.
[0003] Currently, the method for handling delayed tasks involves saving these tasks, which require execution at a specified time, to a database and recording the execution time of each task. The database is periodically polled to check if a delayed task has been executed; if so, it is retrieved for further processing. However, short polling intervals significantly impact database performance, while longer intervals prevent execution from adhering to the actual execution time of the delayed tasks. With the ever-increasing number of tasks, the accurate execution of massive amounts of delayed tasks has become a pressing issue. Summary of the Invention
[0004] In view of this, the purpose of this application is to provide a method, processing device and electronic device for processing delayed tasks, which improves the accuracy of delayed task execution when processing a large number of delayed tasks with different execution times, and solves the problem of low accuracy of delayed task execution in the prior art.
[0005] In a first aspect, embodiments of this application provide a method for processing delayed tasks, the method comprising:
[0006] Obtain at least one delayed task and the execution time corresponding to each delayed task;
[0007] For each delayed task, calculate the time difference between the execution time of the delayed task and the current system time, and determine whether the time difference is less than or equal to the delay threshold.
[0008] If so, then the delayed task will be designated as a task to be executed.
[0009] For each task to be executed, based on the execution time corresponding to the task, the task is delivered to the slot corresponding to the time wheel; wherein, the time period represented by the time wheel is the same as the delay threshold.
[0010] The task to be executed in the slot of the time wheel is executed at the corresponding time.
[0011] Furthermore, for each delayed task, when it is determined that the time difference between the execution time corresponding to the delayed task and the current system time is greater than the delay threshold, the processing method further includes:
[0012] Store the delayed task in the database.
[0013] Furthermore, after executing the task to be executed in the corresponding time slot of the time wheel, the processing method further includes:
[0014] Clear the pending tasks from each slot in the time wheel;
[0015] For each delayed task in the database, return to the step of calculating the time difference between the execution time of the delayed task and the current system time, and determining whether the time difference is less than or equal to the delay threshold.
[0016] Furthermore, for each task to be executed, based on the execution time corresponding to that task, the task to be executed is submitted to the slot corresponding to the time wheel, including:
[0017] The time period represented by each slot in the time wheel is determined based on the execution time corresponding to each task to be executed;
[0018] The number of slots in the time wheel is determined based on the time period represented by the time wheel and the time period represented by each slot, and a corresponding slot number is assigned to each slot.
[0019] For each slot, the time range of the slot under the current system time is determined based on the slot number and the time period represented by the slot.
[0020] For each task to be executed, the execution time corresponding to the task to be executed is compared with the time range of the slot under the current system time to determine whether the execution time corresponding to the task to be executed is within the time range;
[0021] If so, the task to be executed will be delivered to that slot.
[0022] Furthermore, the processing method also includes:
[0023] For each slot, determine whether the number of tasks to be executed in that slot is greater than or equal to N;
[0024] If so, a sub-time wheel is set in the slot; wherein the time period represented by the sub-time wheel is the same as the time period represented by the slot.
[0025] The time period represented by each sub-slot in the sub-time wheel is determined based on the execution time corresponding to each task to be executed in the slot.
[0026] The number of sub-slots in the sub-time wheel is determined based on the time period represented by the sub-time wheel and the time period represented by each sub-slot, and a corresponding sub-slot number is assigned to each sub-slot.
[0027] For each sub-slot, the time range of the sub-slot under the current system time is determined based on the sub-slot number and the time period represented by the sub-slot.
[0028] For each task to be executed in the slot, the execution time corresponding to the task to be executed is compared with the time range of the sub-slot under the current system time to determine whether the execution time corresponding to the task to be executed is within the time range.
[0029] If so, the task to be executed will be delivered to that sub-slot.
[0030] Furthermore, executing the task to be executed in the slot at the corresponding time of the slot in the time wheel includes:
[0031] Obtain the current system time;
[0032] For each task to be executed in the slot, determine whether the execution time of the task to be executed is the same as the current system time;
[0033] If so, then execute the pending task.
[0034] Secondly, embodiments of this application also provide a processing apparatus for delayed tasks, the processing apparatus comprising:
[0035] The delayed task acquisition module is used to acquire at least one delayed task and the execution time corresponding to each delayed task;
[0036] The judgment module is used to calculate the time difference between the execution time of each delayed task and the current system time, and to determine whether the time difference is less than or equal to the delay threshold.
[0037] The task to be executed determination module is used to determine the delayed task as a task to be executed if the condition is met.
[0038] The task delivery module is used to deliver each task to be executed to the slot corresponding to the time wheel based on the execution time of the task; wherein the time period represented by the time wheel is the same as the delay threshold.
[0039] The task execution module is used to execute the task to be executed in the slot at the corresponding time of the slot in the time wheel.
[0040] Furthermore, the processing device also includes a storage module;
[0041] The storage module is used to store each delayed task in the database when it is determined that the time difference between the execution time of the delayed task and the current system time is greater than the delay threshold.
[0042] Thirdly, embodiments of this application also provide an electronic device, including: a processor, a memory, and a bus. The memory stores machine-readable instructions executable by the processor. When the electronic device is running, the processor communicates with the memory via the bus. When the machine-readable instructions are executed by the processor, the steps of the delay task processing method described above are performed.
[0043] Fourthly, embodiments of this application also provide a computer-readable storage medium storing a computer program, which, when executed by a processor, performs the steps of the delay task processing method described above.
[0044] This application provides a method, apparatus, and electronic device for processing delayed tasks. The method involves acquiring at least one delayed task and its corresponding execution time. For each delayed task, the method calculates the time difference between its execution time and the current system time, and determines whether this time difference is less than or equal to a delay threshold. If so, the delayed task is identified as a task to be executed. For each task to be executed, based on its execution time, it is submitted to a slot corresponding to a time wheel. The task to be executed in the slot is executed at the corresponding time. According to the processing method provided, at least one delayed task meeting the delay threshold is identified as a task to be executed and stored in a slot corresponding to a time wheel. By driving the time wheel, the task to be executed in the slot is executed at the corresponding time. When processing a large number of delayed tasks with different execution times, it eliminates the need to periodically poll the database to detect whether delayed tasks are executed. This not only improves the accuracy of delayed task execution and solves the problem of low accuracy in delayed task execution in the prior art, but also reduces the pressure on database operations.
[0045] To make the above-mentioned objectives, features and advantages of this application more apparent and understandable, preferred embodiments are described below in detail with reference to the accompanying drawings. Attached Figure Description
[0046] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0047] Figure 1 A flowchart illustrating a method for processing delayed tasks provided in an embodiment of this application;
[0048] Figure 2 This is a flowchart illustrating a method for delivering a task to be executed to a corresponding slot, as provided in an embodiment of this application.
[0049] Figure 3 A schematic diagram of the structure of a delayed task processing device provided in an embodiment of this application;
[0050] Figure 4 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0051] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. The components of the embodiments of this application described and shown in the accompanying drawings can generally be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of this application provided in the accompanying drawings is not intended to limit the scope of the claimed application, but merely represents selected embodiments of this application. Based on the embodiments of this application, every other embodiment obtained by those skilled in the art without inventive effort falls within the scope of protection of this application.
[0052] With the development of internet technology, some business scenarios involve numerous delayed tasks that must be executed at specified times. Examples include checking payment status 10 minutes after an order is placed, or checking usage of an activity invitation code one hour after it is sent. If the execution time of a delayed task has not yet arrived, the task needs to be suspended until then.
[0053] Research has shown that current methods for handling delayed tasks involve storing these tasks, which require execution at specified times, in a database and recording the execution time of each task. The database is periodically polled to check if a task has been executed; if so, it is retrieved for further processing. However, short polling intervals significantly impact database performance, while longer intervals fail to ensure tasks are executed according to their actual execution times. With the increasing number of tasks, the accurate execution of massive amounts of delayed tasks has become a pressing issue.
[0054] Based on this, the present application provides a method for processing delayed tasks to improve the accuracy of delayed task execution and solve the problem of low accuracy of delayed task execution in the prior art.
[0055] Please see Figure 1 , Figure 1 This is a flowchart illustrating a method for processing delayed tasks provided in an embodiment of this application. Figure 1 As shown in the embodiments of this application, the method for processing delayed tasks includes:
[0056] S101, obtain at least one delayed task and the execution time corresponding to each delayed task.
[0057] It should be noted that a delayed task refers to a task that needs to be executed with a delay during business processing. Delayed tasks include, but are not limited to, sending a message, checking whether an order has been paid, verifying whether information has been approved, etc. This application does not specifically limit the scope of such tasks. Execution time refers to the actual execution time of the delayed task. For example, the execution time of delayed task A could be 2022-04-06 15:00.
[0058] Regarding step S101 above, in specific implementation, at least one delayed task sent by the user and the execution time corresponding to each delayed task are obtained. Here, it is common practice to obtain all delayed tasks sent by the user within 24 hours and the execution time corresponding to each delayed task, or it is possible to obtain all delayed tasks sent by the user within 48 hours and the execution time corresponding to each delayed task. This application does not specifically limit the method.
[0059] S102, for each delayed task, calculate the time difference between the execution time of the delayed task and the current system time, and determine whether the time difference is less than or equal to the delay threshold.
[0060] It should be noted that the current system time refers to the system time corresponding to the current moment obtained by the system. The time difference refers to the difference between the execution time of the delayed task and the current system time. The delay threshold refers to a pre-set time threshold used to determine whether the delayed task needs to be executed. For example, the delay threshold can be 10 minutes or 30 minutes, etc., and this application does not make specific limitations on it.
[0061] Regarding step S102 above, in specific implementation, firstly, the current system time is obtained. Then, for each obtained delayed task, the time difference between the execution time of the delayed task and the current system time is calculated. Specifically, the execution time of the delayed task is subtracted from the current system time to determine the time difference. For example, if the execution time of delayed task A is 15:00 on 2022-04-06, and the obtained current system time is 14:53 on 2022-04-06, the calculated time difference is 7 minutes. Then, the determined time difference is compared with a preset delay threshold to determine whether the time difference is less than or equal to the delay threshold. If the time difference is less than or equal to the delay threshold, it is considered that the delayed task needs to be executed, and then step S103 is executed.
[0062] S103, if so, then the delayed task is determined as a task to be executed.
[0063] It should be noted that the tasks to be executed refer to delayed tasks that need to be executed and stored in the time wheel.
[0064] Regarding step S103 above, when it is determined that the time difference between the execution time of the delayed task and the current system time is less than or equal to the delay threshold, the delayed task is determined as a task to be executed.
[0065] As an optional implementation, for each delayed task, when it is determined that the time difference between the execution time corresponding to the delayed task and the current system time is greater than the delay threshold, the processing method provided in this application further includes:
[0066] Store the delayed task in the database.
[0067] It should be noted that the database mentioned here is a database capable of quickly querying massive amounts of data. For example, the database here could be a Hive database, and this application does not specifically limit it.
[0068] Regarding the above steps, in practical implementation, for each delayed task, when the time difference between the execution time corresponding to the delayed task and the current system time is greater than the delay threshold as determined in step S102, the delayed task is considered to need to be delayed, and the delayed tasks that need to be delayed are written to the database. This solves the data storage problem. In practical implementation, as the number of delayed tasks and the number of users sending delayed tasks increase, the number of delayed tasks will also increase. Using a database to store delayed tasks that need to be delayed can store a large amount of task data, solving the problem of large storage requirements. By storing delayed tasks that need to be delayed in the database, the system only needs to process the tasks to be executed when executing tasks, which also reduces the pressure on the system when executing tasks.
[0069] S104: For each task to be executed, based on the execution time corresponding to the task, deliver the task to the slot corresponding to the time wheel.
[0070] It's important to note that the time wheel is a circular queue storing scheduled tasks, implemented using an array. Each element in the array can hold a scheduled task to be executed. The list of tasks to be executed is a circular doubly linked list, where each item represents a scheduled task. Each scheduled task encapsulates the actual task to be executed; when the pointer reaches the corresponding cell, the corresponding task is checked. Here, the time period represented by the time wheel is the same as the preset delay threshold. For example, if the preset delay threshold is 60 minutes, then the time period represented by the time wheel is also 60 minutes. The time wheel can be understood as a clock face, divided into multiple cells, with each cell representing a specific time precision. For instance, if one time wheel represents one minute, it might have 60 cells, each representing one second, and one rotation completes one minute.
[0071] Regarding step S104 above, in specific implementation, after at least one task to be executed is determined in step S103, for each task to be executed, based on the execution time corresponding to the task, the task to be executed is delivered to the slot corresponding to the time wheel.
[0072] Please see Figure 2 , Figure 2 This is a flowchart illustrating a method for delivering a task to be executed to a corresponding slot, as provided in an embodiment of this application. Figure 2 As shown, regarding step S104 above, the step of submitting each task to be executed to the slot corresponding to the time wheel based on the execution time of the task includes:
[0073] S201, determine the time period represented by each slot in the time wheel based on the execution time corresponding to each task to be executed.
[0074] Regarding step S201 above, in specific implementation, the time period represented by each slot in the time wheel is determined based on the execution time corresponding to each task to be executed. Specifically, the number of slots in the time wheel and the time period together determine the accuracy of the execution time determination. For example, with the same time period, the more data in the slots, the more accurate the execution time; similarly, with the same number of slots, the shorter the time period, the more accurate the execution time. Determining the time period and the corresponding number of slots based on actual business needs enables the tasks to be executed to be executed at a more accurate time. According to the embodiments provided in this application, the time period represented by each slot in the time wheel can be determined based on the time precision of the execution time corresponding to each task to be executed. Specifically, for each task to be executed, the minimum time unit corresponding to the execution time of that task is determined, and the time period represented by each slot is determined based on the minimum time unit of the execution time. For example, when the minimum time unit for the execution time of a task to be executed is seconds, the time period represented by each slot in the time wheel can be set to 1 minute; when the minimum time unit for the execution time of a task to be executed is milliseconds, the time period represented by each slot in the time wheel can be set to 1 second. Therefore, the time period represented by each slot in the time wheel can be determined based on the minimum time unit for the execution time of each task to be executed. Generally, the time unit of the time period represented by each slot is larger than the minimum time unit for the execution time of the task to be executed.
[0075] S202, determine the number of slots in the time wheel based on the time period represented by the time wheel and the time period represented by each slot, and set a corresponding slot number for each slot.
[0076] It should be noted that the slot number refers to the number to which each slot in the time wheel belongs. Specifically, each slot can be assigned its own slot number according to the order of the slots in the time wheel, either clockwise or counterclockwise.
[0077] Regarding step S202 above, in specific implementation, after determining the time period represented by each slot, the number of slots in the time wheel is determined based on the time period represented by the time wheel and the time period represented by each slot. Specifically, the quotient between the time period represented by the time wheel and the time period represented by each slot can be determined as the number of slots in the time wheel. For example, if the time period represented by the time wheel is 60 minutes and the time period represented by each slot is 1 minute, then the number of slots in the time wheel can be determined to be 60. After determining the number of slots in the time wheel, a corresponding slot number is assigned to each slot. Specifically, the slot number can be assigned to each slot according to the order of the slots in the time wheel, either clockwise or counterclockwise.
[0078] S203, for each slot, determine the time range of the slot under the current system time based on the slot number and the time period represented by the slot.
[0079] Regarding step S203 above, in specific implementation, for each slot, after determining the slot number and the time period it represents, the time range of that slot under the current system time is determined based on the slot number and the time period. For example, if the current system time is 15:00:00, the slot number is 5, the time period it represents is 1 minute, and the time range of that slot under the current system time is 15:04:00-15:05:00.
[0080] S204, for each task to be executed, compare the execution time corresponding to the task to be executed with the time range of the slot under the current system time, and determine whether the execution time corresponding to the task to be executed is within the time range.
[0081] Regarding step S204 above, in specific implementation, after the time range of each slot under the current system time is determined, for each task to be executed, the execution time corresponding to the task to be executed is compared with the time range of the slot under the current system time in order of the slot number. Specifically, starting from the first slot, it is compared whether the execution time corresponding to the task to be executed is within the time range of the first slot under the current system time. If yes, step S205 is executed; if not, the next slot is obtained in sequence, and the execution time corresponding to the task to be executed is compared whether it is within the time range of the next slot under the current system time, until it is determined that the execution time corresponding to the task to be executed is within the time range of a certain slot.
[0082] S205, if so, then deliver the task to be executed to the slot.
[0083] Regarding step S205 above, in specific implementation, if it is determined that the execution time of the task to be executed is within the time range of a certain slot under the current system time, then the task to be executed is delivered to that slot.
[0084] As an optional implementation, the processing method provided in this application further includes:
[0085] Step 301: For each slot, determine whether the number of tasks to be executed in that slot is greater than or equal to N.
[0086] Regarding step 301, in practical implementation, after each task to be executed has been delivered to its corresponding slot, for each slot, it is determined whether the number of tasks to be executed in that slot is greater than or equal to N. Specifically, N can be 2. When N equals 2, it is determined whether the number of tasks to be executed in that slot is greater than or equal to 2. If so, step 302 is executed.
[0087] Step 302: If yes, then set a sub-time wheel in the slot.
[0088] It should be noted that the sub-time wheel refers to the time wheel set in the slot. Here, the time period represented by the sub-time wheel is the same as the time period represented by the slot. For example, if the time period represented by the slot is 1 minute, then the time period represented by the sub-time wheel is also 1 minute.
[0089] Regarding step 302 above, in specific implementation, if it is determined whether the data of the task to be executed in a certain slot is greater than or equal to N, then a sub-time wheel is set in that slot to store multiple tasks to be executed in that slot.
[0090] Step 303: Determine the time period represented by each sub-slot in the sub-time wheel based on the execution time corresponding to each task to be executed in the slot.
[0091] It should be noted that a sub-slot refers to a slot set in a sub-time wheel.
[0092] Regarding step 303 above, in practical implementation, the time period represented by each sub-slot in the sub-time wheel can be determined based on the time precision of the execution time corresponding to each task to be executed within the slot. Specifically, for each task to be executed within the slot, the minimum time unit of the execution time corresponding to that task is determined, and the time period represented by each sub-slot is determined based on the minimum time unit of the execution time. For example, when the minimum time unit of the execution time corresponding to the task to be executed within the slot is milliseconds, the time period represented by each sub-slot in the sub-time wheel can be set to 1 millisecond. Therefore, the time period represented by each sub-slot in the sub-time wheel can be determined based on the minimum time unit of the execution time corresponding to each task to be executed within the slot. Generally, the time unit of the time period represented by each sub-slot is the same as the minimum time unit of the execution time corresponding to the task to be executed.
[0093] Step 304: Determine the number of sub-slots in the sub-time wheel based on the time period represented by the sub-time wheel and the time period represented by each sub-slot, and set a corresponding sub-slot number for each sub-slot.
[0094] It should be noted that the sub-slot number refers to the number to which each sub-slot in the sub-time wheel belongs. Specifically, the slot number can be assigned to each sub-slot according to the order of the sub-slots in the sub-time wheel, either clockwise or counterclockwise.
[0095] Regarding step 304 above, in specific implementation, after determining the time period represented by each sub-slot, the number of sub-slots in the sub-time wheel is determined based on the time period represented by the sub-time wheel and the time period represented by each sub-slot. Specifically, the quotient between the time period represented by the sub-time wheel and the time period represented by the sub-slot can be determined as the number of sub-slots in the sub-time wheel. For example, if the time period represented by the sub-time wheel is 1 minute and the time period represented by each sub-slot is 1 second, then the number of sub-slots in the sub-time wheel can be determined to be 60. After determining the number of sub-slots in the sub-time wheel, a corresponding sub-slot number is assigned to each sub-slot. Specifically, the sub-slot number can be assigned to each sub-slot according to the order of the sub-slots in the sub-time wheel, either clockwise or counterclockwise.
[0096] Step 305: For each sub-slot, determine the time range of the sub-slot under the current system time based on the sub-slot number and the time period represented by the sub-slot.
[0097] Regarding step 305 above, in specific implementation, for each sub-slot, after determining the sub-slot number and the time period it represents, the time range of the sub-slot under the current system time is determined based on the sub-slot number and the time period. For example, if the current system time is 15:00:00, the sub-slot is located in the 5th slot, the time period it represents is 1 minute, the slot number is 5, and the time period it represents is 1 second, then the time range of the sub-slot under the current system time is 15:04:04-15:04:05.
[0098] Step 306: For each task to be executed in the slot, compare the execution time of the task to be executed with the time range of the sub-slot under the current system time to determine whether the execution time of the task to be executed is within the time range.
[0099] Regarding step 306 above, in specific implementation, after the time range of each sub-slot under the current system time is determined, for each task to be executed within that slot, the execution time corresponding to the task to be executed is compared with the time range of that sub-slot under the current system time in order of the sub-slot number. Specifically, starting from the first sub-slot, it is compared whether the execution time corresponding to the task to be executed is within the time range of the first sub-slot under the current system time. If yes, step 307 is executed; if not, the next sub-slot is obtained in sequence, and the execution time corresponding to the task to be executed is compared whether it is within the time range of the next sub-slot under the current system time, until it is determined that the execution time corresponding to the task to be executed is within the time range of a certain sub-slot.
[0100] Step 307: If yes, then deliver the task to be executed to the sub-slot.
[0101] Regarding step 307 above, in specific implementation, if it is determined that the execution time of the task to be executed is within the time range of a certain sub-slot under the current system time, then the task to be executed is delivered to that sub-slot.
[0102] S105, the task to be executed in the slot is executed at the corresponding time of the slot in the time wheel.
[0103] Regarding step S105 above, in specific implementation, after all the tasks to be executed are added to the time wheel, the tasks to be executed in the slots are executed at the corresponding times in the slots of the time wheel.
[0104] As an optional implementation, regarding step S105 above, executing the task to be executed in the slot at the corresponding time of the slot in the time wheel includes:
[0105] Step 1051: Obtain the current system time.
[0106] Step 1052: For each task to be executed in the slot, determine whether the execution time corresponding to the task to be executed is the same as the current system time.
[0107] Step 1053: If yes, then execute the task to be executed.
[0108] Regarding steps 1051-1053 above, firstly, the current system time is obtained. The thread continuously pushes the time wheel, and when the pointer reaches the corresponding cell, the corresponding slot is checked. For each slot in the time wheel, the pointer rotates according to the slot number order. When it rotates to a certain slot, it is determined whether there is a task to be executed in that slot. If so, for each task to be executed in that slot, it is determined whether the execution time corresponding to the task to be executed is the same as the current system time. If so, it is considered that the task to be executed has reached its execution time, and step 1053 is executed to execute the task to be executed.
[0109] For slots with sub-time wheels, the method for executing tasks within those sub-time wheels is the same as shown in steps 1051-1053. The thread continuously pushes the time wheel, and when the pointer reaches the corresponding cell, it checks the corresponding slot. For each slot in the time wheel, when the pointer reaches that slot and it's determined that a sub-time wheel exists within it, the thread continuously pushes that sub-time wheel. The sub-time wheel also contains a pointer, which rotates according to the sub-slot numbers. When it reaches a sub-slot, it checks if there is a task to be executed within that sub-slot. If so, it checks if the execution time of that task is the same as the current system time. If so, it considers that the task has reached its execution time and executes the task.
[0110] As an optional implementation, after executing the task to be executed in the slot at the corresponding time of the slot in the time wheel, the processing method provided in this application further includes:
[0111] A: Clear the pending tasks in each slot of the time wheel;
[0112] B: For each delayed task in the database, return to the step of calculating the time difference between the execution time of the delayed task and the current system time, and determining whether the time difference is less than or equal to the delay threshold.
[0113] Regarding steps A and B above, in specific implementation, after all tasks to be executed stored in the time wheel have been completed, the tasks to be executed in each slot of the time wheel need to be cleared. Then, at least one delayed task that meets the delay threshold is extracted from the database, and the delayed task that meets the delay threshold is identified as a task to be executed and stored in the corresponding slot of the time wheel. Specifically, after all tasks to be executed stored in the time wheel have been completed, for each delayed task in the database, the step of "calculating the time difference between the execution time corresponding to the delayed task and the current system time, and determining whether the time difference is less than or equal to the delay threshold" in step S102 is returned to execution. Thus, according to the embodiments provided in this application, for systems with a large workload, delayed tasks that need to be executed are stored in a database, and pending tasks that need to be executed are stored in a time wheel. After all pending tasks in the time wheel have been executed, the task to be executed at the current moment is retrieved from the database. Based on the characteristics of the database, a large number of delayed tasks can be stored, and the database has the characteristics of fast query speed and large data storage capacity, which meets the storage and response requirements of big data. Executing pending tasks through a time wheel solves the problem of executing massive delayed tasks on schedule, thereby ensuring the system's operating performance, improving the accuracy of delayed task execution, and reducing the pressure on the database to poll delayed tasks.
[0114] The delayed task processing method provided in this application involves obtaining at least one delayed task and the execution time corresponding to each delayed task; for each delayed task, calculating the time difference between the execution time corresponding to the delayed task and the current system time, and determining whether the time difference is less than or equal to a delay threshold; if so, the delayed task is identified as a task to be executed; for each task to be executed, based on the execution time corresponding to the task to be executed, the task to be executed is submitted to the slot corresponding to the time wheel; the task to be executed in the slot is executed at the corresponding time of the slot in the time wheel. According to the processing method provided in this application, at least one delayed task that meets the delay threshold is identified as a task to be executed and stored in the slot corresponding to the time wheel. By driving the time wheel to rotate, the task to be executed in the slot is executed when the corresponding time is reached. When processing a large number of delayed tasks with different execution times, it is not necessary to detect whether the delayed tasks have been executed by periodically polling the database, which not only improves the accuracy of delayed task execution and solves the problem of low accuracy of delayed task execution in the prior art, but also reduces the pressure on database operations.
[0115] Please see Figure 3 , Figure 3 This is a schematic diagram of a processing apparatus for delayed tasks provided in an embodiment of this application. Figure 3 As shown, the processing device 300 includes:
[0116] The delayed task acquisition module 301 is used to acquire at least one delayed task and the execution time corresponding to each delayed task;
[0117] The judgment module 302 is used to calculate the time difference between the execution time of each delayed task and the current system time, and to determine whether the time difference is less than or equal to the delay threshold.
[0118] The task to be executed determination module 303 is used to determine the delayed task as a task to be executed if the condition is met.
[0119] The task delivery module 304 is used to deliver each task to be executed to the slot corresponding to the time wheel based on the execution time corresponding to the task; wherein the time period represented by the time wheel is the same as the delay threshold.
[0120] The task execution module 305 is used to execute the task to be executed in the slot at the corresponding time of the slot in the time wheel.
[0121] Furthermore, the processing device 300 also includes a storage module;
[0122] The storage module is used to store each delayed task in the database when it is determined that the time difference between the execution time of the delayed task and the current system time is greater than the delay threshold.
[0123] Furthermore, the processing device 300 also includes a task clearing and filtering module;
[0124] After the task to be executed in the slot of the time wheel is executed at the corresponding time, the task clearing and filtering module is used to:
[0125] Clear the pending tasks from each slot in the time wheel;
[0126] For each delayed task in the database, return to the step of calculating the time difference between the execution time of the delayed task and the current system time, and determining whether the time difference is less than or equal to the delay threshold.
[0127] Furthermore, when the task delivery module 304, for each task to be executed, delivers the task to the slot corresponding to the time wheel based on the execution time of the task, the task delivery module 304 is also used to:
[0128] The time period represented by each slot in the time wheel is determined based on the execution time corresponding to each task to be executed;
[0129] The number of slots in the time wheel is determined based on the time period represented by the time wheel and the time period represented by each slot, and a corresponding slot number is assigned to each slot.
[0130] For each slot, the time range of the slot under the current system time is determined based on the slot number and the time period represented by the slot.
[0131] For each task to be executed, the execution time corresponding to the task to be executed is compared with the time range of the slot under the current system time to determine whether the execution time corresponding to the task to be executed is within the time range;
[0132] If so, the task to be executed will be delivered to that slot.
[0133] Furthermore, the task delivery module 304 is also used for:
[0134] For each slot, determine whether the number of tasks to be executed in that slot is greater than or equal to N;
[0135] If so, a sub-time wheel is set in the slot; wherein the time period represented by the sub-time wheel is the same as the time period represented by the slot.
[0136] The time period represented by each sub-slot in the sub-time wheel is determined based on the execution time corresponding to each task to be executed in the slot.
[0137] The number of sub-slots in the sub-time wheel is determined based on the time period represented by the sub-time wheel and the time period represented by each sub-slot, and a corresponding sub-slot number is assigned to each sub-slot.
[0138] For each sub-slot, the time range of the sub-slot under the current system time is determined based on the sub-slot number and the time period represented by the sub-slot.
[0139] For each task to be executed in the slot, the execution time corresponding to the task to be executed is compared with the time range of the sub-slot under the current system time to determine whether the execution time corresponding to the task to be executed is within the time range.
[0140] If so, the task to be executed will be delivered to that sub-slot.
[0141] Furthermore, when the task execution module 305 executes the task to be executed in the slot at the corresponding time of the slot in the time wheel, it is also used to:
[0142] Obtain the current system time;
[0143] For each task to be executed in the slot, determine whether the execution time of the task to be executed is the same as the current system time;
[0144] If so, then execute the pending task.
[0145] Please see Figure 4 , Figure 4 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Figure 4 As shown, the electronic device 400 includes a processor 410, a memory 420, and a bus 430.
[0146] The memory 420 stores machine-readable instructions executable by the processor 410. When the electronic device 400 is running, the processor 410 communicates with the memory 420 via the bus 430. When the machine-readable instructions are executed by the processor 410, they can perform the operations described above. Figure 1 as well as Figure 2 The steps of the delayed task processing method in the method embodiment shown solve the problem of low accuracy in the execution of delayed tasks in the prior art. For specific implementation, please refer to the method embodiment, which will not be repeated here.
[0147] This application also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, can perform the above-described actions. Figure 1 as well as Figure 2 The steps of the delayed task processing method in the method embodiment shown solve the problem of low accuracy in the execution of delayed tasks in the prior art. For specific implementation, please refer to the method embodiment, which will not be repeated here.
[0148] Those skilled in the art will understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0149] In the several embodiments provided in this application, it should be understood that the disclosed systems, apparatuses, and methods can be implemented in other ways. The apparatus embodiments described above are merely illustrative. For example, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. Furthermore, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Additionally, the shown or discussed mutual couplings, direct couplings, or communication connections may be through some communication interfaces; indirect couplings or communication connections between devices or units may be electrical, mechanical, or other forms.
[0150] The units described as separate components may or may not be physically separate. The 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 the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0151] In addition, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0152] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a processor-executable, non-volatile, computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0153] It should be noted that similar labels and letters in the following figures indicate similar items. Therefore, once an item is defined in one figure, it does not need to be further defined and explained in subsequent figures. In addition, the terms "first", "second", "third", etc. are used only to distinguish descriptions and should not be construed as indicating or implying relative importance.
[0154] Finally, it should be noted that the above-described embodiments are merely specific implementations of this application, used to illustrate the technical solutions of this application, and not to limit them. The scope of protection of this application is not limited thereto. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that any person skilled in the art can still modify or easily conceive of changes to the technical solutions described in the foregoing embodiments, or make equivalent substitutions for some of the technical features, within the scope of the technology disclosed in this application. Such modifications, changes, or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application, and should all be covered within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A method for processing a time-delayed task, characterized in that, The processing method comprises: acquiring at least one delay task and an execution time corresponding to each delay task; for each delay task, calculating a time difference between the execution time corresponding to the delay task and a current system time, and judging whether the time difference is less than or equal to a delay threshold value; if yes, determining the delay task as a to-be-executed task; for each to-be-executed task, delivering the to-be-executed task to a slot corresponding to a time wheel based on the execution time corresponding to the to-be-executed task; wherein a time period represented by the time wheel is the same as the delay threshold value; executing the to-be-executed task in the slot of the time wheel at a corresponding time of the slot; The processing method further comprises: for each slot, judging whether a number of to-be-executed tasks in the slot is greater than or equal to N; if yes, setting a sub-time wheel in the slot; wherein a time period represented by the sub-time wheel is the same as a time period represented by the slot; determining a time period represented by each sub-slot in the sub-time wheel according to the execution time corresponding to each to-be-executed task in the slot; wherein for each to-be-executed task in the slot, a minimum time unit of the execution time corresponding to the to-be-executed task is determined, and the time period represented by each sub-slot is determined according to the minimum time unit of the execution time; determining a number of sub-slots in the sub-time wheel based on the time period represented by the sub-time wheel and the time period represented by each sub-slot, and setting a corresponding sub-slot number for each sub-slot; for each sub-slot, determining a time range of the sub-slot at the current system time based on the sub-slot number of the sub-slot and the time period represented by the sub-slot; for each to-be-executed task in the slot, comparing the execution time corresponding to the to-be-executed task with the time range of the sub-slot at the current system time, and judging whether the execution time corresponding to the to-be-executed task is located in the time range; if yes, delivering the to-be-executed task to the sub-slot.
2. The treatment method according to claim 1, characterized in that, for each delay task, when judging that the time difference between the execution time corresponding to the delay task and the current system time is greater than the delay threshold value, the processing method further comprises: storing the delay task in a database.
3. The treatment method according to claim 2, characterized in that, After the step of executing the to-be-executed task in the slot of the time wheel at the corresponding time of the slot, the processing method further comprises: clearing the to-be-executed tasks in each slot in the time wheel; for each delay task in the database, returning to the step of calculating the time difference between the execution time corresponding to the delay task and the current system time, and judging whether the time difference is less than or equal to the delay threshold value.
4. The treatment method of claim 1, wherein The step of, for each to-be-executed task, delivering the to-be-executed task to a slot corresponding to a time wheel based on the execution time corresponding to the to-be-executed task, comprises: determining a time period represented by each slot in the time wheel according to the execution time corresponding to each to-be-executed task. determine the number of slots in the time wheel based on the time period represented by the time wheel and the time period represented by each slot, and set a corresponding slot number for each slot; for each slot, determine the time range of the slot at the current system time based on the slot number of the slot and the time period represented by the slot; for each to-be-executed task, compare the execution time corresponding to the to-be-executed task with the time range of the slot at the current system time, and determine whether the execution time corresponding to the to-be-executed task is located within the time range; if yes, deliver the to-be-executed task to the slot.
5. The treatment method of claim 1, wherein the executing the to-be-executed task in the slot of the time wheel at the corresponding moment of the slot, comprising: obtaining the current system time; for each to-be-executed task in the slot, determining whether the execution time corresponding to the to-be-executed task is the same as the current system time; if yes, executing the to-be-executed task.
6. A processing device for deferring a task, characterized by the processing device comprises: a delay task acquisition module configured to acquire at least one delay task and an execution time corresponding to each delay task; a judgment module configured to, for each delay task, calculate a time difference between the execution time corresponding to the delay task and the current system time, and determine whether the time difference is less than or equal to a delay threshold; a to-be-executed task determination module configured to, if yes, determine the delay task as a to-be-executed task; a task delivery module configured to, for each to-be-executed task, deliver the to-be-executed task to a slot corresponding to a time wheel based on the execution time corresponding to the to-be-executed task; wherein the time period represented by the time wheel is the same as the delay threshold; a task execution module configured to execute the to-be-executed task in the slot of the time wheel at the corresponding moment of the slot; the task delivery module 304 is further configured to: for each slot, determine whether the number of to-be-executed tasks in the slot is greater than or equal to N; if yes, set a sub-time wheel in the slot; wherein the time period represented by the sub-time wheel is the same as the time period represented by the slot; determine the time period represented by each sub-slot in the sub-time wheel based on the execution time corresponding to each to-be-executed task in the slot; wherein for each to-be-executed task in the slot, determine the smallest time unit of the execution time corresponding to the to-be-executed task, and determine the time period represented by each sub-slot based on the smallest time unit of the execution time; determine the number of sub-slots in the sub-time wheel based on the time period represented by the sub-time wheel and the time period represented by each sub-slot, and set a corresponding sub-slot number for each sub-slot; for each sub-slot, determine the time range of the sub-slot at the current system time based on the sub-slot number of the sub-slot and the time period represented by the sub-slot; for each to-be-executed task in the slot, compare the execution time corresponding to the to-be-executed task with the time range of the sub-slot at the current system time, and determine whether the execution time corresponding to the to-be-executed task is located within the time range; If yes, the to-be-executed task is delivered to the sub-slot.
7. The processing device of claim 6, wherein, The processing device further comprises a storage module; The storage module is configured to, for each delay task, when a time difference between an execution time corresponding to the delay task and a current system time is greater than the delay threshold, store the delay task into a database.
8. An electronic device, comprising: Comprise: A processor, a memory and a bus, the memory stores machine readable instructions executable by the processor, when the electronic device is running, the processor and the memory communicate through the bus, the machine readable instructions are executed by the processor to execute the steps of the delay task processing method as claimed in any one of claims 1 to 5.
9. A computer-readable storage medium, characterized in that, The computer readable storage medium stores a computer program, and the computer program is executed by the processor to execute the steps of the delay task processing method as claimed in any one of claims 1 to 5.
Citation Information
Patent Citations
Delay task processing method and device, computer equipment and storage medium
CN111026524A
Delay scheduling method and system for cultural tourism industry platform, and storage medium
CN113961369A