A task scheduling method, device, electronic device and computer readable medium
By abstracting the timing tasks into triples and independent of system time, the timing task scheduling complexity and accuracy problems are solved, and the time accuracy and performance of task scheduling are improved.
Patent Information
- Application Number
- CN202111593458.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-23
- Publication Date
- 2024-12-20
- Estimated Expiration
- 2041-12-23
AI Technical Summary
The existing timed task scheduling is implemented in complex ways, affecting the time accuracy and performance of task scheduling, and is susceptible to system time.
By receiving task scheduling requests, obtaining a timed task storage array, calculating the time difference between the task execution time and the current time, generating triplets (task function, cycle time, time difference) to schedule timed tasks, and abstracting tasks as triplets to be independent of the system Time, improve scheduling accuracy and performance.
The timed task scheduling process is simplified, time accuracy and performance are improved, task scheduling is not affected by system time, and more accurate task execution is achieved.
Smart Images

Figure CN114281504B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a task scheduling method, device, electronic device and computer-readable medium. Background Art
[0002] The existing scheduled task scheduling is based on system time and adopts complex scheduling algorithms. It is easily affected by system time. Changing the system time will affect the normal execution of tasks. In addition, the implementation is complex, which affects the time accuracy and performance of task scheduling.
[0003] In the process of implementing this application, the inventors found that there are at least the following problems in the prior art:
[0004] The existing scheduled task scheduling is complex to implement, which affects the time accuracy and performance of task scheduling. Summary of the invention
[0005] In view of this, the embodiments of the present application provide a task scheduling method, device, electronic device and computer-readable medium, which can solve the problem that the existing scheduled task scheduling is complex to implement and affects the time accuracy and performance of task scheduling.
[0006] To achieve the above object, according to one aspect of an embodiment of the present application, a task scheduling method is provided, including:
[0007] Receive a task scheduling request, obtain a scheduled task storage array, and determine the task cycle time and task execution time corresponding to each scheduled task in the scheduled task storage array;
[0008] Get the current time, and then calculate the time difference between each task execution time and the current time;
[0009] Based on the cycle time of each task and each time difference, a triplet corresponding to each scheduled task is generated;
[0010] Based on each triplet, the corresponding scheduled task in the scheduled task storage array is scheduled.
[0011] Optionally, the time difference between each task execution time and the current time is calculated, including:
[0012] Calculate the difference between the current time and the execution time of each task;
[0013] The number of seconds corresponding to each difference is determined, and then each number of seconds is determined as the time difference between each task execution time and the current time.
[0014] Optionally, a triplet corresponding to each scheduled task is generated, including:
[0015] Get the task function corresponding to each scheduled task;
[0016] Determine one-time tasks and periodic tasks based on task cycle time;
[0017] The task cycle time corresponding to the one-time task is set to 0, and the task cycle time corresponding to the periodic task is converted into seconds, and then a triplet corresponding to each scheduled task is generated according to each task function and each time difference.
[0018] Optionally, scheduling corresponding scheduled tasks in the scheduled task storage array based on each triplet includes:
[0019] Based on each triplet, obtain the time difference of each corresponding scheduled task;
[0020] Get the value of the time accumulator, and then update the time difference of each scheduled task based on the value of the time accumulator;
[0021] Traverse each scheduled task in the scheduled task array, determine the scheduled task corresponding to the updated time difference less than or equal to 0 as the target scheduled task and execute it;
[0022] In response to the target scheduled task being executed and determining that the task cycle time corresponding to the target scheduled task is not 0, the time difference in the triplet corresponding to the target scheduled task is reset to the task cycle time in the triplet corresponding to the target scheduled task.
[0023] Optionally, scheduling corresponding scheduled tasks in the scheduled task storage array based on each triplet further includes:
[0024] In response to the target scheduled task being executed and determining that the task cycle time corresponding to the target scheduled task is 0, the target scheduled task is deleted.
[0025] Optionally, delete the target scheduled task, including:
[0026] Get the last scheduled task in the scheduled task storage array;
[0027] Move the last scheduled task to the position of the target scheduled task in the scheduled task storage array, and delete the target scheduled task at the same time.
[0028] Optionally, after resetting the time difference in the triplet corresponding to the target scheduled task to the task cycle time in the triplet corresponding to the target scheduled task, the method further includes:
[0029] After traversing each scheduled task in the scheduled task storage array, obtain the timer value and the value of the time accumulator;
[0030] The timer value is added to the value of the time accumulator, thereby updating the value of the time accumulator.
[0031] In addition, the present application also provides a task scheduling device, including:
[0032] A receiving unit is configured to receive a task scheduling request, obtain a scheduled task storage array, and determine a task cycle time and a task execution time corresponding to each scheduled task in the scheduled task storage array;
[0033] A time difference calculation unit is configured to obtain the current time and then calculate the time difference between each task execution time and the current time;
[0034] A triplet generation unit is configured to generate a triplet corresponding to each scheduled task based on each task cycle time and each time difference;
[0035] The scheduling unit is configured to schedule the corresponding scheduled tasks in the scheduled task storage array based on each triple.
[0036] Optionally, the time difference calculation unit is further configured to:
[0037] Calculate the difference between the current time and the execution time of each task;
[0038] The number of seconds corresponding to each difference is determined, and then each number of seconds is determined as the time difference between each task execution time and the current time.
[0039] Optionally, the triple generation unit is further configured to:
[0040] Get the task function corresponding to each scheduled task;
[0041] Determine one-time tasks and periodic tasks based on task cycle time;
[0042] The task cycle time corresponding to the one-time task is set to 0, and the task cycle time corresponding to the periodic task is converted into seconds, and then a triplet corresponding to each scheduled task is generated according to each task function and each time difference.
[0043] Optionally, the scheduling unit is further configured to:
[0044] Based on each triplet, obtain the time difference of each corresponding scheduled task;
[0045] Get the value of the time accumulator, and then update the time difference of each scheduled task based on the value of the time accumulator;
[0046] Traverse each scheduled task in the scheduled task array, determine the scheduled task corresponding to the updated time difference less than or equal to 0 as the target scheduled task and execute it;
[0047] In response to the target scheduled task being executed and determining that the task cycle time corresponding to the target scheduled task is not 0, the time difference in the triplet corresponding to the target scheduled task is reset to the task cycle time in the triplet corresponding to the target scheduled task.
[0048] Optionally, the scheduling unit is further configured to:
[0049] In response to the target scheduled task being executed and determining that the task cycle time corresponding to the target scheduled task is 0, the target scheduled task is deleted.
[0050] Optionally, the scheduling unit is further configured to:
[0051] Get the last scheduled task in the scheduled task storage array;
[0052] Move the last scheduled task to the position of the target scheduled task in the scheduled task storage array, and delete the target scheduled task at the same time.
[0053] Optionally, the task scheduling device further includes an updating unit configured to:
[0054] After traversing each scheduled task in the scheduled task storage array, obtain the timer value and the value of the time accumulator;
[0055] The timer value is added to the value of the time accumulator, thereby updating the value of the time accumulator.
[0056] In addition, the present application also provides a task scheduling electronic device, including: one or more processors; a storage device for storing one or more programs, when the one or more programs are executed by one or more processors, the one or more processors implement the task scheduling method as described above.
[0057] In addition, the present application also provides a computer-readable medium on which a computer program is stored, and when the program is executed by a processor, the task scheduling method as described above is implemented.
[0058] To achieve the above objective, according to another aspect of the embodiments of the present application, a computer program product is provided.
[0059] A computer program product according to an embodiment of the present application includes a computer program, and when the program is executed by a processor, the task scheduling method provided by the embodiment of the present application is implemented.
[0060] One embodiment of the above invention has the following advantages or beneficial effects: the present application receives a task scheduling request, obtains a timed task storage array, determines the task cycle time and task execution time corresponding to each timed task in the timed task storage array; obtains the current time, and then calculates the time difference between each task execution time and the current time; generates a triple corresponding to each timed task based on each task cycle time and each time difference; and schedules the corresponding timed task in the timed task storage array based on each triple. By abstracting the timed task into a triple, the timed task scheduling is not affected by the system time, thereby improving the time accuracy and task scheduling performance of the task scheduling.
[0061] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with specific implementation examples. BRIEF DESCRIPTION OF THE DRAWINGS
[0062] The accompanying drawings are used to better understand the present application and do not constitute an improper limitation on the present application.
[0063] Figure 1 is a schematic diagram of the main process of the task scheduling method according to the first embodiment of the present application;
[0064] Figure 2 is a schematic diagram of the main process of the task scheduling method according to the second embodiment of the present application;
[0065] Figure 3 is a schematic diagram of an application scenario of a task scheduling method according to a third embodiment of the present application;
[0066] Figure 4 is a schematic diagram of a task scheduling thread according to a task scheduling method according to a third embodiment of the present application;
[0067] Figure 5 is a schematic diagram of task deletion according to the task scheduling method of the fourth embodiment of the present application;
[0068] Figure 6 is a schematic diagram of the main units of the task scheduling device according to an embodiment of the present application;
[0069] Figure 7 is an exemplary system architecture diagram to which the embodiments of the present application can be applied;
[0070] Figure 8 It is a structural diagram of a computer system of a terminal device or server suitable for implementing an embodiment of the present application. DETAILED DESCRIPTION
[0071] The following is a description of exemplary embodiments of the present application in conjunction with the accompanying drawings, including various details of the embodiments of the present application to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present application. Similarly, for the sake of clarity and conciseness, the description of well-known functions and structures is omitted in the following description. The acquisition, storage, use, and processing of data in the technical solution of the present application are in compliance with the relevant provisions of national laws and regulations.
[0072] Figure 1 is a schematic diagram of the main process of the task scheduling method according to the first embodiment of the present application, such as Figure 1 As shown, the task scheduling method includes:
[0073] Step S101, receiving a task scheduling request, obtaining a scheduled task storage array, and determining a task cycle time and a task execution time corresponding to each scheduled task in the scheduled task storage array.
[0074] In this embodiment, the execution subject of the task scheduling method (for example, it can be a server) can receive the task scheduling request by wired connection or wireless connection. For example, the execution subject detects that the task scheduling thread is started, responds to the task scheduling request corresponding to the task scheduling thread, and obtains the timed task storage array corresponding to the task scheduling request. In the task storage array, the existence form of each stored task can be text, for example: execute once after 5 seconds, execute once at 16:00:00, execute once every 15 seconds, execute once every day at 16:00:00, etc. Of course, the existence form of each stored task can also be a keyword obtained after extracting key information from the text of the task, for example, it can be once every 5 seconds, once at 16:00:00, once per 15 seconds, once per 16:00:00, etc. Specifically, the execution subject can also convert the above keywords into corresponding binary numbers or corresponding vectors that can be recognized by the computer, and then store them in the timed task array for the task scheduling thread to call. The embodiment of the present application does not specifically limit the existence form of the timed task in the timed task array. Scheduled tasks, for example, can be some low-priority, low-frequency tasks. For example, they can be tasks such as checking system updates, cleaning junk files, or changing the screen saver at a certain time every day.
[0075] The scheduled task storage array may store relevant information about the execution of each task, such as the execution frequency, execution time, and functions to be called when executing each task. This information may be mixed together, and the execution subject needs to parse the scheduled task storage array to accurately execute each corresponding scheduled task.
[0076] Step S102, obtaining the current time, and then calculating the time difference between each task execution time and the current time.
[0077] Specifically, for each task, the time difference is used to represent how many seconds are left from the current time to the next execution of the task.
[0078] Step S103: generating a triplet corresponding to each scheduled task based on each task cycle time and each time difference.
[0079] In the scheduled task storage array, the period time period of each task can be obtained according to the execution frequency information of each task, and the time difference count between the next execution time of the task and the current time can be determined according to the execution time point of each task, and the time difference is a non-negative value. In addition, the execution subject can obtain the execution function f corresponding to each scheduled task from the scheduled task storage array. Specifically, the execution function f is a function (or a closure, an executable object that packages a function and its parameters), and executing a task is equivalent to executing the function), and each task corresponds to an execution function. Furthermore, after obtaining the execution function f of each scheduled task, the period time period of the task, and the time difference count, the execution subject can generate a triple (f, period, count) corresponding to each scheduled task to characterize the specific configuration parameters for executing the scheduled task.
[0080] Specifically, a triplet corresponding to each scheduled task is generated, including:
[0081] Get the task function corresponding to each scheduled task; determine the one-time task and the periodic task based on the task cycle time. The execution subject can judge whether it is a one-time task or a periodic task based on whether the task cycle time extracted from the scheduled task storage array is 0. If it is 0, it is a one-time task, and if it is not 0, it is a periodic task. When it is not 0, the unit of the task cycle time can be months, days, hours, minutes, seconds, etc. The embodiment of the present application does not specifically limit the unit of the task cycle time. The execution subject can set the corresponding task cycle time of the one-time task to 0, and convert the task cycle time corresponding to the periodic task into seconds (if the original unit of the task cycle time is seconds, there is no need to convert, and it can be directly called), and then generate a triple corresponding to each scheduled task according to each task function and each time difference.
[0082] For example, a scheduled task is abstracted into a triple (f, period, count), which can represent various forms of scheduled tasks. Among them, f is a function (or a closure, an executable object that packages a function and its parameters), and executing a task is equivalent to executing the function; period is the cycle time of the task, in seconds, and a value of 0 means that the task is non-periodic, and a value of 86400 means that the task will be executed once a day; count is used for counting, which indicates how many seconds are left before the next execution of the task.
[0083] Assuming the current time is 15:00:00, the execution subject can now add the following tasks to the task scheduler:
[0084] Execute f once after 5 seconds; Execute f once at 16:00:00; Execute f once every 15 seconds; Execute f once every day at 16:00:00.
[0085] The above tasks can be represented by triplets as: (f, 0, 5); (f, 0, 3600); (f, 15, 15); (f, 86400, 3600).
[0086] The first two tasks are one-time tasks, so the period value of the task is 0; the second two are periodic tasks, and the period value is not 0. The fourth task means that f is executed once after 1 hour (3600 seconds), and then once every other day. f is the execution function corresponding to the scheduled task, which is an executable object that packages the function and its parameters.
[0087] Step S104: scheduling the corresponding scheduled tasks in the scheduled task storage array based on each triplet.
[0088] Specifically, based on each triplet, the corresponding scheduled task in the scheduled task storage array is scheduled, including:
[0089] Based on each triplet, the time difference of each corresponding scheduled task is obtained.
[0090] For example, for each scheduled task, the execution subject can determine the count number in the triple (f, period, count) corresponding to the scheduled task, that is, the time difference. The count number is used for counting, which indicates how many seconds are left until the next execution of the scheduled task.
[0091] Get the value of the time accumulator, and then update the time difference of each scheduled task based on the value of the time accumulator.
[0092] Traverse each scheduled task in the scheduled task array, determine the scheduled task corresponding to the updated time difference less than or equal to 0 as the target scheduled task and execute it.
[0093] For example, the core idea of task scheduling is: every 1 second, the task count is reduced by 1, after n seconds, the count is reduced by n, and the task is executed when the count is reduced to less than or equal to 0. Figure 4 As shown in the figure, the task scheduler uses two dynamically growing arrays to store scheduled tasks. For the convenience of description, these two arrays are called M and N. The added tasks are first placed in M. Multiple threads can add tasks to M concurrently. This process requires conventional mutex locks and other mechanisms to resolve concurrency conflicts. Specifically, the task scheduler uses single-threaded scheduling, that is, each task scheduler creates a thread (called a task scheduling thread).
[0094] The thread scheduling process is shown in steps (1) to (4):
[0095] Step (1) of the thread scheduling process: reset the timer T, calculate the number of seconds t of the time accumulator dt (milliseconds), and subtract the number of seconds from dt (e.g. 1200 milliseconds minus 1 second becomes 200 milliseconds), and then update the time difference of each scheduled task (i.e. the count number in the corresponding triplet). The specific update method can be that every time the time accumulator dt (milliseconds) is subtracted by 1 second (the premise is that the time accumulator dt (milliseconds) accumulates to 1 second before subtracting 1 second, and when it has not accumulated to 1 second, it needs to wait until it accumulates to 1 second before subtracting 1 second), the time difference of each scheduled task is subtracted by 1 second accordingly. If dt accumulates to n seconds, the time difference (i.e. the count number in the corresponding triplet) is reduced by n;
[0096] Step (2) of the thread scheduling process: swap out the tasks in array M, add them to array N, and then traverse the tasks in array N, subtract the number of seconds t from the count of each task (the number of seconds t is determined by the number of whole seconds reached by the time accumulator, that is, it is determined according to the number of 1 seconds reached by the time accumulator, where 1 second is considered a whole second). When the count is less than or equal to 0, execute the task, otherwise skip the task. The target scheduled task is the scheduled task corresponding to the count in the corresponding triplet is less than or equal to 0, specifically the task to be executed immediately.
[0097] In response to the completion of execution of the target scheduled task and determining that the task cycle time corresponding to the target scheduled task is not 0, the target scheduled task can be determined as a periodic task, and the execution entity can reset the time difference in the triplet corresponding to the target scheduled task to the task cycle time in the triplet corresponding to the target scheduled task.
[0098] After executing the target scheduled task, if the task cycle time period of the target scheduled task is not 0, the target scheduled task can be determined as a periodic task. The execution subject can reset the count number in the triple (f, period, count) corresponding to the task to the task cycle time period, otherwise delete the task.
[0099] Specifically, based on each triplet, scheduling the corresponding scheduled task in the scheduled task storage array also includes:
[0100] In response to the target scheduled task being completed and determining that the task cycle time corresponding to the target scheduled task is 0, it can be determined that the target scheduled task is a one-time task, and the execution subject can execute deletion of the target scheduled task.
[0101] Specifically, after resetting the value of the time accumulator corresponding to the target timed task to the preset time accumulation value corresponding to the triplet corresponding to the target timed task, the method further includes:
[0102] After traversing and completing each scheduled task in the scheduled task storage array, the timer value and the value of the time accumulator are obtained; the timer value is added to the value of the time accumulator, and the value of the time accumulator is updated.
[0103] Specifically, step (3) of the thread scheduling process: after traversing N, wait for 1 second; step (4) of the thread scheduling process: use timer T to calculate the elapsed time (in milliseconds), add the time to the time accumulator dt, and then return to step (1) of the thread scheduling process in the instance corresponding to step S104 to repeat steps (1) to (4).
[0104] Specifically, the timer T and the time accumulator dt have different functions. T is only used to measure the time (milliseconds) used in steps (1) to (4) of the above thread scheduling process, which includes the waiting time in (3). The time measured by T will be accumulated to dt, and the number of seconds in dt is the number that needs to be subtracted from the task count. The remaining time (milliseconds) less than 1 second after dt is subtracted from the number of seconds will continue to participate in subsequent accumulation operations to improve the time accuracy of task scheduling.
[0105] In addition, the time measured by timer T includes the waiting time in (3) because waiting for 1 second in a computer program also has errors, and the actual waiting time may be slightly longer than 1 second. Therefore, timer T is used to measure the time of the entire process from (1) to (4), rather than measuring the time of (1), (2), (4) plus 1 second (this application does not make any specific limitation on the added 1 second, and it can also be 2 seconds, 3 seconds or n seconds). This can improve the time accuracy of task scheduling.
[0106] Figure 5FIG. 4 is a flowchart of a task deletion process according to a task scheduling method according to a fourth embodiment of the present application. Figure 5 As shown, delete the target scheduled task, including:
[0107] Get the last scheduled task in the scheduled task storage array; move the last scheduled task to the position of the target scheduled task in the scheduled task storage array, and delete the target scheduled task at the same time.
[0108] like Figure 5 As shown, when deleting the target scheduled task, such as Task 2, directly move the last task k in the scheduled task storage array to the position of Task 2. This method only needs to move one task in the scheduled task storage array, and the performance is much better than the traditional task deletion method (if the traditional deletion method is used, after deleting Task 2, all tasks after Task 2 need to be moved, and the performance is extremely poor). The task scheduling algorithm of the embodiment of the present application is independent of the order of the tasks in the array N, so the above optimization operation can be performed.
[0109] The embodiment of the present application abstracts the scheduled task into a triple (f, period, count). After the task is added, it is not affected by the system time. The count number is used to represent the number of seconds until the next execution of the task, and the count number is reduced by 1 every second, which simplifies the design of the task scheduler; the timer T that is not affected by the system time is used to measure the time, and the time accumulator dt is used to calibrate the time (time less than 1 second will be accumulated with the subsequent time), thereby improving the time accuracy of task scheduling.
[0110] This embodiment receives a task scheduling request, obtains a timed task storage array, determines the task cycle time and task execution time corresponding to each timed task in the timed task storage array; obtains the current time, and then calculates the time difference between each task execution time and the current time; generates a triple corresponding to each timed task based on each task cycle time and each time difference; and schedules the corresponding timed task in the timed task storage array based on each triple. By abstracting the timed task into a triple, the timed task scheduling is not affected by the system time, thereby improving the time accuracy and task scheduling performance of the task scheduling.
[0111] Figure 2 is a schematic diagram of the main flow of the task scheduling method according to the second embodiment of the present application, such as Figure 2 As shown, the task scheduling method includes:
[0112] Step S201, receiving a task scheduling request, obtaining a scheduled task storage array, and determining a task cycle time and a task execution time corresponding to each scheduled task in the scheduled task storage array.
[0113] It can be understood that the scheduled tasks in the scheduled task storage array may be scheduled tasks in various data forms, for example, they may be scheduled tasks in text form, binary digital form, or vector form.
[0114] Step S202, obtaining the current time, and then calculating the difference between each task execution time and the current time.
[0115] Step S203, determining the number of seconds corresponding to each difference value, and then determining each number of seconds as the time difference between each task execution time and the current time.
[0116] The time difference is the difference between the task execution time of each task in the scheduled task storage array and the current time. The difference can be in months, days, hours, minutes, seconds, etc., and is a non-negative value. Finally, the execution subject can convert the difference into a time difference in seconds. For example, the current time is 15:00:00, and the task execution time of a task in the scheduled task storage array is 16:00:00. The difference between the current time and the execution time of each task is 1 hour, which is converted to 3600 seconds. The time difference of the task (that is, the count number in the initial triple (f, period, count)) is 3600 seconds.
[0117] The embodiment of the present application schedules scheduled tasks by the time difference between the execution time of each scheduled task and the current time, so that task scheduling is not affected by system time and the accuracy of scheduled task scheduling is improved.
[0118] Step S204: generating a triplet corresponding to each scheduled task based on each task cycle time and each time difference.
[0119] For each scheduled task, the execution function f, the task cycle time period and the time difference count corresponding to the scheduled task are combined into a tuple (f, period, count) of the corresponding task.
[0120] Step S205: scheduling the corresponding scheduled tasks in the scheduled task storage array based on each triplet.
[0121] Of course, as another implementation method of the embodiment of the present application, the execution subject can also directly put the task converted into the triple form into Figure 4 In the array M shown, the execution subject can then swap out the tasks added in the array M, add them to N, and then traverse the tasks in N to perform task scheduling. The embodiment of the present application does not specifically limit the timing of converting tasks into triples.
[0122] The embodiment of the present application abstracts the scheduled task into a triple (f, period, count). After the task is added, it is not affected by the system time. The count number is used to represent the number of seconds until the next execution of the task, and the count number is reduced by 1 every second, which simplifies the design of the task scheduler; the timer T that is not affected by the system time is used to measure the time, and the time accumulator dt is used to calibrate the time (time less than 1 second will be accumulated with the subsequent time), thereby improving the time accuracy of task scheduling.
[0123] Figure 3 Schematic diagram of an application scenario of the task scheduling method according to the third embodiment of the present application. The task scheduling method of the embodiment of the present application can be applied to the scenario of scheduling scheduled tasks. Figure 3 As shown in the figure, the running process of scheduled task scheduling includes the following steps:
[0124] Step 1), start the task scheduling thread and set the time accumulator dt to 0;
[0125] Step 2), reset the timer T, calculate the number of seconds t of dt, and subtract the second part from dt (the unit of dt is milliseconds, 2300 milliseconds minus 2 seconds is 300 milliseconds);
[0126] Step 3), swap out the tasks in array M and put them into array N;
[0127] Step 4), traverse the tasks in array N (i.e. Figure 3 For each task, subtract t from the task count. If the count is less than or equal to 0, execute the task. After executing the task, if its period is not 0 (periodic task), reset the task count to period. Otherwise, delete the task from N.
[0128] Step 5), wait for 1 second.
[0129] Step 6), use timer T (milliseconds) to calculate the time (milliseconds) consumed from step 1) to step 5), and add the time to dt, then return to step 2) and repeat this process.
[0130] The operation flow chart of the scheduled task scheduling of the embodiment of the present application is shown above. The aforementioned steps 1) to 6) are the execution process of the task scheduling thread, and the process of adding a scheduled task is independent of the task scheduling process and is completed by other threads. The process is relatively simple. Figure 3 In the figure, only the dotted line is used to indicate the process of adding scheduled tasks to the array M. Step 4) is the process of scheduling threads to traverse the tasks in the array N. Figure 3 This process is marked with a dotted box.
[0131] In the embodiment of the present application, various timed tasks are abstracted into a triple (f, period, count), and the count number decreases by 1 per second. When it decreases to less than or equal to 0, the task is executed. In the triple, period represents the cycle time of the task, and count represents the time until the next execution of the task. Once the task is added, it will not be affected by the system time. By converting the tasks added by the user into a triple (f, period, count), using a timer T that is not affected by the system time, and using the timer T in conjunction with the time accumulator dt to calibrate the time, the time accuracy of task scheduling is improved.
[0132] Figure 6 Schematic diagram of the main units of the task scheduling device according to an embodiment of the present application. Figure 6 As shown, the task scheduling device includes a receiving unit 601, a time difference calculation unit 602, a triplet generation unit 603 and a scheduling unit 604.
[0133] The receiving unit 601 is configured to receive a task scheduling request, obtain a scheduled task storage array, and determine a task cycle time and a task execution time corresponding to each scheduled task in the scheduled task storage array.
[0134] The time difference calculation unit 602 is configured to obtain the current time and then calculate the time difference between each task execution time and the current time.
[0135] The triplet generation unit 603 is configured to generate a triplet corresponding to each scheduled task based on each task cycle time and each time difference.
[0136] The scheduling unit 604 is configured to schedule the corresponding scheduled tasks in the scheduled task storage array based on each triplet.
[0137] In some embodiments, the time difference calculation unit 602 is further configured to: calculate the difference between the current time and each task execution time; determine the number of seconds corresponding to each difference, and then determine each number of seconds as the time difference between each task execution time and the current time.
[0138] In some embodiments, the triplet generation unit 603 is further configured to: obtain the task function corresponding to each scheduled task; determine the one-time task and the periodic task based on the task cycle time; set the task cycle time corresponding to the one-time task to 0, and convert the task cycle time corresponding to the periodic task into seconds, and then generate a triplet corresponding to each scheduled task according to each task function and each time difference.
[0139] In some embodiments, the scheduling unit 604 is further configured to: obtain the time difference of each corresponding scheduled task based on each triplet; obtain the value of the time accumulator, and then update the time difference of each scheduled task based on the value of the time accumulator; traverse each scheduled task in the scheduled task array, and determine the corresponding scheduled task when the updated time difference is less than or equal to 0 as the target scheduled task and execute it; in response to the completion of the execution of the target scheduled task and determining that the task cycle time corresponding to the target scheduled task is not 0, reset the time difference in the triplet corresponding to the target scheduled task to the task cycle time in the triplet corresponding to the target scheduled task.
[0140] In some embodiments, the scheduling unit 604 is further configured to: in response to the target scheduled task being completed and determining that the task cycle time corresponding to the target scheduled task is 0, delete the target scheduled task.
[0141] In some embodiments, the scheduling unit 604 is further configured to: obtain the last scheduled task in the scheduled task storage array; move the last scheduled task to the position of the target scheduled task in the scheduled task storage array, and delete the target scheduled task.
[0142] In some embodiments, the task scheduling device further includes Figure 6 The update unit not shown in the figure is configured to: after traversing and completing each scheduled task in the scheduled task storage array, obtain the timer value and the value of the time accumulator, add the timer value to the value of the time accumulator, and then update the value of the time accumulator.
[0143] It should be noted that the task scheduling method and the task scheduling device in the present application have a corresponding relationship in terms of specific implementation contents, so the repeated contents will not be described again.
[0144] Figure 7 An exemplary system architecture 700 is shown to which the task scheduling method or task scheduling device according to the embodiments of the present application can be applied.
[0145] like Figure 7 As shown, system architecture 700 may include terminal devices 701, 702, 703, network 704 and server 705. Network 704 is used to provide a medium for communication links between terminal devices 701, 702, 703 and server 705. Network 704 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.
[0146] Users can use terminal devices 701, 702, and 703 to interact with server 705 through network 704 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 701, 702, and 703, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc. (only examples).
[0147] The terminal devices 701 , 702 , and 703 may be various electronic devices having a task scheduling processing screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, desktop computers, and the like.
[0148] Server 705 may be a server that provides various services, such as a background management server that supports task scheduling requests submitted by users using terminal devices 701, 702, and 703 (for example only). The background management server may receive a task scheduling request, obtain a timed task storage array, determine the task cycle time and task execution time corresponding to each timed task in the timed task storage array; obtain the current time, and then calculate the time difference between each task execution time and the current time; based on each task cycle time and each time difference, generate a triple corresponding to each timed task; based on each triple, schedule the corresponding timed task in the timed task storage array. By abstracting the timed task into a triple, the timed task scheduling is not affected by the system time, thereby improving the time accuracy and task scheduling performance of the task scheduling.
[0149] It should be noted that the task scheduling method provided in the embodiment of the present application is generally executed by the server 705 , and accordingly, the task scheduling device is generally set in the server 705 .
[0150] It should be understood that Figure 7 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.
[0151] Reference below Figure 8 , which shows a schematic diagram of the structure of a computer system 800 of a terminal device suitable for implementing an embodiment of the present application. Figure 8 The terminal device shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0152] like Figure 8As shown, the computer system 800 includes a central processing unit (CPU) 801, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 802 or a program loaded from a storage part 808 into a random access memory (RAM) 803. In the RAM 803, various programs and data required for the operation of the computer system 800 are also stored. The CPU 801, the ROM 802, and the RAM 803 are connected to each other via a bus 804. An input / output (I / O) interface 805 is also connected to the bus 804.
[0153] The following components are connected to the I / O interface 805: an input section 806 including a keyboard, a mouse, etc.; an output section 807 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 808 including a hard disk, etc.; and a communication section 809 including a network interface card such as a LAN card, a modem, etc. The communication section 809 performs communication processing via a network such as the Internet. A drive 810 is also connected to the I / O interface 805 as needed. A removable medium 811, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 810 as needed, so that a computer program read therefrom is installed into the storage section 808 as needed.
[0154] In particular, according to the embodiments disclosed in the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present application include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 809, and / or installed from the removable medium 811. When the computer program is executed by the central processing unit (CPU) 801, the above-mentioned functions defined in the system of the present application are executed.
[0155] It should be noted that the computer-readable medium shown in the present application may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. Computer-readable storage media may include, for example, but are not limited to electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices or devices, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present application, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present application, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, in which a computer-readable program code is carried. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.
[0156] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present application. In this regard, each box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the above-mentioned module, program segment or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0157] The units involved in the embodiments described in the present application may be implemented by software or hardware. The units described may also be set in a processor, for example, it may be described as: a processor includes a receiving unit, a time difference calculation unit, a triplet generation unit and a scheduling unit. The names of these units do not constitute limitations on the units themselves in some cases.
[0158] As another aspect, the present application also provides a computer-readable medium, which may be included in the device described in the above embodiment; or it may exist independently and not be assembled into the device. The above computer-readable medium carries one or more programs. When the above one or more programs are executed by a device, the device receives a task scheduling request, obtains a timed task storage array, determines the task cycle time and task execution time corresponding to each timed task in the timed task storage array; obtains the current time, and then calculates the time difference between each task execution time and the current time; based on each task cycle time and each time difference, generates a triple corresponding to each timed task; based on each triple, schedules the corresponding timed task in the timed task storage array. By abstracting the timed task into a triple, the timed task scheduling is not affected by the system time, thereby improving the time accuracy and task scheduling performance of the task scheduling.
[0159] The computer program product of the present application includes a computer program, and when the computer program is executed by a processor, the task scheduling method in the embodiment of the present application is implemented.
[0160] According to the technical solution of the embodiment of the present application, by abstracting the scheduled tasks into triplets, the scheduled tasks are not affected by the system time during scheduling, thereby improving the time accuracy and task scheduling performance of the task scheduling.
[0161] The above specific implementations do not constitute a limitation on the protection scope of this application. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of this application should be included in the protection scope of this application.
Claims
1. A task scheduling method, characterized in that: include: Receive a task scheduling request, obtain a scheduled task storage array, and determine a task cycle time and a task execution time corresponding to each scheduled task in the scheduled task storage array; Obtaining the current time, and then calculating the time difference between the execution time of each task and the current time; Based on each of the task cycle times and each of the time differences, generating a triplet corresponding to each of the scheduled tasks; The generating of the triples corresponding to the scheduled tasks includes: obtaining the task functions corresponding to the scheduled tasks; determining the one-time tasks and the periodic tasks based on the task cycle time; setting the task cycle time corresponding to the one-time tasks to 0, converting the task cycle time corresponding to the periodic tasks into seconds, and then generating the triples corresponding to the scheduled tasks according to the task functions and the time differences; Based on each of the triples, the corresponding scheduled tasks in the scheduled task storage array are scheduled; the scheduling of the corresponding scheduled tasks in the scheduled task storage array based on each of the triples includes: based on each of the triples, obtaining the time difference of each corresponding scheduled task; obtaining the value of the time accumulator, and then updating the time difference of each scheduled task based on the value of the time accumulator; traversing each of the scheduled tasks in the scheduled task array, determining the corresponding scheduled task when the updated time difference is less than or equal to 0 as the target scheduled task and executing it; in response to the completion of the execution of the target scheduled task and determining that the task cycle time corresponding to the target scheduled task is not 0, resetting the time difference in the triplet corresponding to the target scheduled task to the task cycle time in the triplet corresponding to the target scheduled task.
2. The method according to claim 1, characterized in that The calculating the time difference between the execution time of each task and the current time includes: Calculating the difference between the current time and the execution time of each task; The number of seconds corresponding to each of the difference values is determined, and then each of the seconds is determined as the time difference between each of the task execution times and the current time.
3. The method according to claim 1, characterized in that The scheduling of the corresponding scheduled tasks in the scheduled task storage array based on each of the triples further includes: In response to the target scheduled task being completed and determining that the task cycle time corresponding to the target scheduled task is 0, the target scheduled task is deleted.
4. The method according to claim 3, characterized in that The deleting the target scheduled task includes: Get the last scheduled task in the scheduled task storage array; The last scheduled task is moved to the position of the target scheduled task in the scheduled task storage array, and the target scheduled task is deleted at the same time.
5. The method according to claim 1, characterized in that After resetting the time difference in the triplet corresponding to the target scheduled task to the task cycle time in the triplet corresponding to the target scheduled task, the method further includes: After traversing and completing each scheduled task in the scheduled task storage array, obtaining a timer value and a value of a time accumulator; The timer value is added to the value of the time accumulator, thereby updating the value of the time accumulator.
6. A task scheduling device, characterized in that: include: A receiving unit is configured to receive a task scheduling request, obtain a scheduled task storage array, and determine a task cycle time and a task execution time corresponding to each scheduled task in the scheduled task storage array; A time difference calculation unit is configured to obtain the current time and then calculate the time difference between each task execution time and the current time; The triplet generation unit is configured to generate a triplet corresponding to each of the scheduled tasks based on each of the task cycle times and each of the time differences; the triplet generation unit is further configured to: obtain a task function corresponding to each of the scheduled tasks; determine a one-time task and a periodic task based on the task cycle time; set the task cycle time corresponding to the one-time task to 0, convert the task cycle time corresponding to the periodic task into seconds, and then generate a triplet corresponding to each of the scheduled tasks according to each of the task functions and each of the time differences; The scheduling unit is configured to schedule the corresponding scheduled tasks in the scheduled task storage array based on each of the triples; the scheduling unit is further configured to: obtain the time difference of each corresponding scheduled task based on each of the triples; obtain the value of the time accumulator, and then update the time difference of each scheduled task based on the value of the time accumulator; traverse each scheduled task in the scheduled task array, determine the corresponding scheduled task when the updated time difference is less than or equal to 0 as the target scheduled task and execute it; In response to the target scheduled task being executed and determining that the task cycle time corresponding to the target scheduled task is not 0, the time difference in the triplet corresponding to the target scheduled task is reset to the task cycle time in the triplet corresponding to the target scheduled task.
7. The device according to claim 6, characterized in that The time difference calculation unit is further configured to: Calculating the difference between the current time and the execution time of each task; The number of seconds corresponding to each of the difference values is determined, and then each of the seconds is determined as the time difference between each of the task execution times and the current time.
8. The device according to claim 6, characterized in that The scheduling unit is further configured to: In response to the target scheduled task being completed and determining that the task cycle time corresponding to the target scheduled task is 0, the target scheduled task is deleted.
9. The device according to claim 8, characterized in that The scheduling unit is further configured to: Get the last scheduled task in the scheduled task storage array; The last scheduled task is moved to the position of the target scheduled task in the scheduled task storage array, and the target scheduled task is deleted at the same time.
10. A task scheduling electronic device, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 5.
11. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 5 is implemented.
12. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the method according to any one of claims 1 to 5 is implemented.
Citation Information
Patent Citations
Timed task execution method, device and equipment and storage medium
CN111381947A
Data processing task scheduling method, device and system
CN113127225A
Periodic task processing method and apparatus, computer program, and readable medium
WO2017107843A1
Task processing method, apparatus, and system
WO2020034879A1
