Task processing method and device, electronic equipment and machine readable storage medium
By creating task queues for different resources and utilizing multi-threading, the issues of task processing speed and timing were resolved, achieving efficient and reliable task execution.
Patent Information
- Application Number
- CN202210220522.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-08
- Publication Date
- 2025-11-07
- Estimated Expiration
- 2042-03-08
AI Technical Summary
In existing technologies, task processing methods cannot simultaneously guarantee task processing speed and timing, which can easily lead to message blocking, especially when resource volume increases.
By creating task queues for different resources and using multiple task execution threads to process tasks in different task queues, and executing tasks in the same task queue in ascending order of task execution time, combined with task filtering and retry mechanisms, the task processing flow is optimized.
While improving task processing efficiency, it ensures the timing and reliability of task processing, reduces kernel resource consumption, and improves the system's task execution success rate and user experience.
Smart Images

Figure CN114691322B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of data processing, and in particular to a task processing method and device, electronic equipment and machine readable storage medium. BACKGROUND
[0002] At present, resource task (referred to as task) processing mainly includes two ways:
[0003] 1. Using a thread pool to be responsible for asynchronous delivery of tasks to perform task processing. Although the asynchronous multi-thread concurrent processing mode is used to improve the task processing speed, the processing time sequence of the task cannot be guaranteed.
[0004] 2. Using a single queue to guarantee the time sequence of the task, but the processing speed of the task cannot be guaranteed. In the case of resource quantity improvement and multiple increase of task quantity, message blocking is easily caused. SUMMARY
[0005] Therefore, the present application provides a task processing method and device, electronic equipment and machine readable storage medium.
[0006] According to a first aspect of the present application, a task processing method is provided, comprising:
[0007] receiving a task adding instruction; the task adding instruction includes a task to be added and a resource identifier, and the resource identifier is used to uniquely identify a resource for issuing the task to be added;
[0008] adding the task to be added to a target task queue according to the resource identifier of the task to be added, and the resource identifier of the target task queue is consistent with the resource identifier of the task to be added;
[0009] For any task queue, the first target task in the task queue that reaches the task execution time is executed by the task execution thread allocated for the task queue.
[0010] According to a second aspect of the present application, a task processing device is provided, comprising:
[0011] a receiving unit configured to receive a task adding instruction; the task adding instruction includes a task to be added and a resource identifier, and the resource identifier is used to uniquely identify a resource for issuing the task to be added;
[0012] an adding unit configured to add the task to be added to a target task queue according to the resource identifier of the task to be added, and the resource identifier of the target task queue is consistent with the resource identifier of the task to be added;
[0013] The processing unit is configured to execute, for any task queue, a first target task in the task queue that reaches a task execution time by a task execution thread allocated for the task queue.
[0014] According to a third aspect of the embodiments of the present application, an electronic device is provided, comprising a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory complete mutual communication through the communication bus;
[0015] The memory is configured to store a computer program.
[0016] The processor is configured to execute the program stored on the memory, and implement the method provided in the first aspect.
[0017] According to a fourth aspect of the embodiments of the present application, a computer readable storage medium is provided, and the computer readable storage medium stores a computer program, and the computer program is executed by the processor to implement the method provided in the first aspect.
[0018] According to a fifth aspect of the embodiments of the present application, a computer program is provided, and the computer program is stored in a computer readable storage medium, and when the processor executes the computer program, the processor is caused to execute the method provided in the first aspect.
[0019] The task processing method provided in the embodiments of the present application creates a task queue for resources, adds tasks of different resources to different task queues, and processes tasks in different task queues by multiple task execution threads. For tasks in the same task queue, the task execution threads execute the tasks in ascending order of task execution time, which improves the task processing efficiency and ensures the time sequence of task processing. BRIEF DESCRIPTION OF DRAWINGS
[0020] Figure 1 is a flowchart of a task processing method provided in the embodiments of the present application;
[0021] Figure 2 is a design schematic diagram of a task processor provided in the embodiments of the present application;
[0022] Figure 3 is a state schematic diagram of a task execution thread provided in the embodiments of the present application;
[0023] Figure 4 is a schematic diagram of a task adding process provided in the embodiments of the present application;
[0024] Figure 5 is a schematic diagram of a task reading process provided in the embodiments of the present application;
[0025] Figure 6is a structural schematic diagram of a task processing device provided by an embodiment of the present application.
[0026] Figure 7 is a hardware structural schematic diagram of an electronic device provided by an embodiment of the present application. DETAILED DESCRIPTION
[0027] The exemplary embodiments will be described in detail herein with reference to the attached drawings. The following description is with reference to the drawings, in which like numerals refer to like elements throughout. The embodiments described in the following exemplary embodiments are not meant to represent all implementations consistent with the present application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of the present application as detailed in the appended claims.
[0028] The terminology used in this application is for the purpose of describing particular embodiments only and is not intended to be limiting of the present application. As used in this application and the appended claims, the singular forms "a," "an" and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise.
[0029] In order for those skilled in the art to better understand the technical solutions provided by the embodiments of the present application, the following will first briefly describe some technical terms related to the embodiments of the present application.
[0030] 1. Queue: a special linear table, which only allows deletion operation at the front end of the table, and insertion operation at the rear end of the table, and is a kind of operation-restricted linear table.
[0031] 2. Thread: the smallest unit of operation scheduling by the operating system.
[0032] 3. Thread pool: a multi-thread processing method.
[0033] 4. Scheduling: balancing and reasonably allocating resource tasks, so that resource tasks can be processed in real time and effectively.
[0034] 5. Resource: a specific device that really exists or a service that is virtually defined.
[0035] 6. Resource identifier: a unique identifier of a resource, which can be used to uniquely identify a resource.
[0036] 7. Resource type: can include data source (resource reporting data), data target (resource storing data) and third-party resource (third type of resource other than data source and data target).
[0037] 8. Task: resource addition, deletion, modification, setting, reset, arming, disarming, subscription, etc. Each task has the following attributes: resource identifier, UUID (for uniquely identifying a task), generation time, execution time, retry flag, and dirty task flag.
[0038] 9. Task type: addition, deletion, configuration, arming, disarming, subscription, etc. The type is divided according to the operation.
[0039] 10. Task bucket (or queue bucket): A task bucket includes a task queue. Tasks with the same resource identifier are collected in the same task queue. Meanwhile, the task bucket can be set with a thread ID to identify the thread occupied by the task bucket.
[0040] 11. Task scheduling: The thread pool is divided by resource type, and resource tasks are clustered in task buckets by resource identifier. Multiple threads are allocated to consume tasks in parallel according to task type. Timed monitoring is set to patrol the task bucket, thereby achieving task scheduling, and supporting task filtering processing.
[0041] 12. Task processor: One resource identifier corresponds to one task bucket. All different task buckets are collected in a task container, which has task scheduling attributes. A task processor schedules and processes tasks in all task buckets in a task container.
[0042] 13. Dirty task: a task that has been consumed, which may be executing or may have completed execution.
[0043] In order to make the above-mentioned purposes, features and advantages of the embodiments of the present application more apparent and easy to understand, the technical solutions in the embodiments of the present application will be further described in detail below with reference to the drawings.
[0044] It should be noted that the sequence numbers of the steps in the embodiments of the present application do not mean the order of execution. The execution order of the processes should be determined according to their functions and inherent logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
[0045] Please refer to Figure 1 , a flowchart of a task processing method provided by the embodiments of the present application is shown in Figure 1 , which can include the following steps:
[0046] Step S100, receiving a task adding instruction; the task adding instruction includes a task to be added and a resource identifier, which is used to uniquely identify the resource to which the task to be added is issued.
[0047] Step S110, adding the task to be added to a target task queue according to the resource identifier of the task to be added. The resource identifier of the target task queue is consistent with the resource identifier of the task to be added.
[0048] In this embodiment of the application, in order to improve the efficiency of task processing and ensure the timing of task processing, tasks issued by different resources can be added to different task queues. The task processor can process the tasks in different task queues through multiple task execution threads. For tasks in the same task queue, the task processor can process them in ascending order of task execution time through task execution threads.
[0049] Accordingly, when a resource is assigned a task, it can carry a resource identifier in the task, which can be used to uniquely identify the resource for which the task is assigned.
[0050] When it is necessary to add a task (add the task to the task queue), the task processor can query the task queue (referred to as the target task queue in this article) whose resource identifier matches the resource identifier included in the received task add instruction, and add the task included in the task add instruction (which may be referred to as the task to be added) to the target task queue.
[0051] Step S120: For any task queue, execute the first target task in the task queue that has reached its task execution time by using the task execution thread allocated to the task queue.
[0052] In this embodiment of the application, for any task queue, when there is a task in the task queue that has reached its task execution time (referred to as the first target task in this document), the task processor can execute the first target task in the task queue through the task execution thread allocated to the task queue. Thus, it can be ensured that the tasks in each task queue are executed in ascending order of task execution time, thereby ensuring the timing of task processing.
[0053] It can be seen that, in Figure 1 In the method flow shown, task queues are created for resources, and tasks for different resources are added to different task queues. Multiple task execution threads process the tasks in different task queues respectively. For tasks in the same task queue, the task execution threads execute them in ascending order according to the task execution time. This improves task processing efficiency while ensuring the timing of task processing.
[0054] In some embodiments, adding the task to the target task queue based on the resource identifier of the task to be added may include:
[0055] Determine if a target task queue exists;
[0056] If it does not exist, create the target task queue and add the task to be added to the target task queue;
[0057] If the target task queue exists, the task to be added is added to the target task queue.
[0058] The task processing method provided by the embodiments of the present application can further include:
[0059] For any task queue, if there is no task in the task queue, the task queue is released.
[0060] For example, for any task identified by a resource, if there is a task, a corresponding task queue can be created, and the task can be added to the task queue; if there is no task and the corresponding task queue has been created, the created task queue can be released.
[0061] Correspondingly, when the task processor receives the task adding instruction, the task processor can determine whether there is a task queue (i.e., the target task queue) whose resource identifier is consistent with the resource identifier included in the task adding instruction.
[0062] If the task processor queries the target task queue, the task to be added can be added to the target task queue.
[0063] If the task processor does not query the target task queue, that is, the target task queue has not been created or has been released after being created, the task processor can create the target task queue and add the task to be added to the target task queue.
[0064] For example, for any created task queue, if the task processor determines that there is no task in the task queue, the task processor can release the task queue, so as to avoid that the task queue occupies storage space without adding tasks.
[0065] In some embodiments, before the task to be added is added to the target task queue, the method can further include:
[0066] If there is a second target task in the target task queue, the generation time of which is earlier than the generation time of the task to be added, the second target task is filtered according to the task type of the second target task and the task type of the task to be added.
[0067] For example, considering that there is an association between tasks of a specific task type, a task executed later can affect the execution result of a task executed earlier.
[0068] For example, for a resource adding task and a resource deleting task issued under the same resource, no matter the execution result of the resource adding task, the resource deleting task can clean up the result of the resource adding task.
[0069] Correspondingly, in order to improve the task execution efficiency and reduce the kernel resource consumption, for the tasks with the same resource identifier, the tasks can be filtered according to the generation time and the task type, so as to improve the effective task execution success rate.
[0070] When the task processor determines the target task queue corresponding to the to-be-added task, the task processor can determine whether there is a task (hereinafter referred to as a second target task) with a generation time earlier than the generation time of the to-be-added task in the target task queue.
[0071] When it is determined that there is a second target task with a generation time earlier than the generation time of the to-be-added task in the target task queue, the second target task can be filtered according to the task type of the second target task and the task type of the to-be-added task.
[0072] For example, the to-be-added task is a resource deletion task, and before the to-be-added task is added to the target task queue, the resource addition task with a generation time earlier than the generation time of the to-be-added task in the target task queue can be filtered out.
[0073] In some embodiments, the above adding the to-be-added task to the target task queue can include:
[0074] determining whether the to-be-added task is a retry task;
[0075] If the to-be-added task is a retry task, it is determined whether the current head task in the target task queue is a dirty task; if yes, the current head task in the target task queue is deleted, and the to-be-added task is added to the target task queue; wherein the retry task is a task in the target task queue that fails to execute and supports task retry; the execution time of each task in the task queue from the head to the tail is sequentially increased, and the execution time of the retry task is determined according to the latest addition time and a preset delay time;
[0076] If the to-be-added task is not a retry task, the to-be-added task is added to the target task queue.
[0077] For example, in order to improve the reliability of task execution, the task processor can support a task retry function, that is, for a task supporting retry, in the case of task execution failure, the task can be added to the task queue again by generating a retry task instead of reissuing the task by the resource.
[0078] For example, the execution time of the retry task can be determined according to the latest addition time (the time of adding to the task queue) and a preset delay time (which can be set according to the actual scene), so as to ensure that the execution time of the retry task is later than the execution time of the existing task in the current queue.
[0079] Correspondingly, the task processor can also determine whether the to-be-added task is a retry task before adding the to-be-added task to the target task queue.
[0080] For example, the retry task can include a retry task identifier, and the task processor can identify whether the to-be-added task is a retry task according to the retry task identifier.
[0081] For the case where the to-be-added task is a retry task, the task processor can first determine whether the current head task in the target task queue is a dirty task before adding the to-be-added task to the target task queue.
[0082] For example, the task execution time of each task in the task queue from the head to the tail is sequentially increased, that is, the head task is the oldest task.
[0083] When the task processor determines that the current head task in the target task queue is a dirty task, the task processor can delete the current head task in the target task queue and add the to-be-added task to the target task queue.
[0084] For example, for a non-retry task, the task processor can directly add the task to the tail of the target task queue.
[0085] For example, when the task processor adds the to-be-added task to the target task queue, the tasks in the target task queue can be sorted in ascending order of execution time.
[0086] It should be noted that if the to-be-added task is a retry task, but the head task is not a dirty task, it can be considered that the retry task has been successfully executed at present, and in this case, the retry task does not need to be added to the task queue.
[0087] In some embodiments, for any task queue, executing the first target task by the task execution thread allocated for the task queue can include:
[0088] For any task queue, when the task queue is not empty, each task in the task queue is traversed in order from the head to the tail; wherein the task execution time of each task in the task queue from the head to the tail is sequentially increased;
[0089] If the current head task is a dirty task, the current head task is deleted, the next task of the deleted task is determined as the latest head task, and the traversal is continued;
[0090] If the current head task is a non-dirty task and the task execution time of the current head task has arrived, the current head task is set as a dirty task, and the task execution thread allocated for the task queue is informed to execute the current head task in the task queue through the task execution thread, and the iteration is ended;
[0091] If the current head task is a non-dirty task and the task execution time of the current head task has not arrived, the iteration is ended.
[0092] If the task queue is empty, the iteration is ended.
[0093] For example, in the task reading process, for any task queue, the task processor can iterate through the tasks in the task queue in the order from the head to the tail, i.e., in the order from early to late of the execution time of the tasks, when the task queue is not empty.
[0094] For the current head task of the queue, the task processor can determine whether the task is a dirty task.
[0095] For example, the task processor can determine whether the task is a dirty task according to whether the task is provided with a dirty task flag. If the task is provided with the dirty task flag, it can be determined that the task is a dirty task; otherwise, it can be determined that the task is not a dirty task, i.e., a non-dirty task.
[0096] If the task (i.e., the current head task of the queue) is a dirty task, the task processor can delete the current head task, at this time, the next task of the deleted head task becomes the latest head task of the queue, and the task processor can continue to iterate through the latest head task to determine whether the head task is a dirty task, and if so, continue to delete the current head task.
[0097] For example, assuming that the tasks A-D are included in the queue in the order from the head to the tail, the task processor can first iterate through the current head task (task A), and if the task A is a dirty task, the task A can be deleted, at this time, the task B becomes the latest head task. At this time, the task processor can continue to iterate and determine whether the current head task (task B) is a dirty task.
[0098] For example, when the task processor determines that the current head task of the task queue is not a dirty task, the task processor can determine whether the task execution time of the current head task has arrived, i.e., whether the current system time is greater than or equal to the task execution time of the current head task.
[0099] In a case where the task execution time of the current head task has arrived, the current head task can be set as a dirty task, and a task execution thread allocated for the task queue is notified to execute the current head task in the task queue through the task execution thread, and the current round of iteration is ended.
[0100] In a case where the task execution time of the current head task has not arrived, i.e., in a case where the current system time is less than the task execution time of the current head task, the current round of iteration can be ended. In this case, the tasks in the task queue are not executed.
[0101] For example, in a case where the tasks in the task queue are iterated in the above manner, and none of the tasks is a dirty task, the task processor can delete each task in the task queue in turn until the task queue is empty, and the iteration is ended.
[0102] It can be seen that, through the above task reading process, each task in the task queue can be read and executed in turn according to the execution order, and the timing of task execution is ensured. In addition, by monitoring the execution time of the tasks in the task queue, the timeliness of task execution is ensured, and the efficiency of task execution is improved.
[0103] In an example, the above notification of the task execution thread allocated for the task queue can include:
[0104] In a case where the task queue is not allocated a task execution thread, a task execution thread is allocated for the task queue, and the task execution thread allocated for the task queue is notified.
[0105] For example, considering that the number of resources in the system can be relatively large, if a task execution thread is fixedly allocated for each resource, a large number of task execution threads will be required.
[0106] Therefore, in order to reduce the demand for task execution threads, the task execution threads can be allocated on demand. For any task queue, a task execution thread can be allocated for the task queue in a case where there is a task to be executed in the task queue, and the task to be executed in the queue is executed by the task execution thread.
[0107] For example, for any task queue, in a case where the current head task of the task queue is determined to be a non-dirty task through the above task reading process, and the task execution time of the current head task has arrived, it can be determined whether the task queue is allocated a task execution thread.
[0108] In a case where the task queue is not allocated a task execution thread, a task execution thread can be allocated for the task queue, and the task execution thread allocated for the task queue is notified to execute the current head task in the task queue through the task execution thread.
[0109] Exemplarily, in the case that the task queue has allocated a task execution thread, the task execution thread allocated for the task queue can be directly notified to execute the current head task in the task queue.
[0110] In one example, for any task queue allocated with a task execution thread, in the case that the task execution time of the current head task in the task queue has not arrived, the task execution thread allocated for the task queue is released.
[0111] Exemplarily, for any task queue, in the case that the task queue has allocated a task execution thread and the task execution time of the current head task in the task queue has not arrived, the task execution thread allocated for the task queue can be released, so as to avoid the occupation of the task execution thread by the task queue in the case that there is no task to be executed.
[0112] In order for those skilled in the art to better understand the technical solutions provided by the embodiments of the present application, the technical solutions provided by the embodiments of the present application will be described below in conjunction with specific examples.
[0113] In this embodiment, in order to solve the problems of resource processing speed and real-time data synchronization, timing of resource tasks, writing efficiency and thread execution efficiency, a scheme of a task processor for implementing resource service processing based on a task bucket (including the above-mentioned task queue and resource identifier, one task bucket containing one task queue and one thread identifier), thread pool and task scheduling is proposed.
[0114] Exemplarily, as shown in Figure 2 The task processor has the following attributes:
[0115] 1. The tasks added in the task bucket in the task container are added to the task bucket with the same resource identifier according to the resource identifier, and the tasks in the same task bucket are sorted in ascending order according to the execution time.
[0116] 2. Each task execution thread in the thread pool maintains an occupation bit for recording the resource identifier of the task executed by the task execution thread.
[0117] 3. The idle task execution thread in the thread pool is allocated to the task bucket in the task container that has a task reaching the execution condition and not executed.
[0118] Exemplarily, considering that the number of resources in the system is usually large, fixed allocation of a task execution thread for each resource will require a large number of task execution threads, causing great consumption, therefore, the task execution thread can be allocated to the task bucket in the case that there is a task ready in the task bucket (i.e. reaching the execution condition and the task not executed).
[0119] For example, assuming that the number of resources in the system is n (n≥1), if the execution time of the unexecuted task in the task bucket corresponding to resource m (1≤m≤n) is less than or equal to the current system time, a task execution thread is allocated to resource m. If the execution time of each task in the task bucket corresponding to resource m is greater than the current system time (i.e., the execution time of the head task is greater than the current system time), the task execution thread occupied by resource m is released.
[0120] For example, the state diagram of the task execution thread can be as shown in Figure 3
[0121] It should be noted that if there is no task in the task bucket corresponding to resource m, the task bucket corresponding to resource m is released.
[0122] Thus, in the case where the task of resource m needs to be added to the task bucket, if the task bucket corresponding to resource m exists, the task of resource m is added to the corresponding task bucket; if the task bucket corresponding to resource m does not exist, the task bucket corresponding to resource m is created, and the task of resource m is added to the corresponding task bucket.
[0123] For example, a task monitoring thread (which can be referred to as a task monitor) can also be provided in the task processor, which is used to perform task execution inspection, i.e., to detect whether there is an unexecuted task reaching the execution time in each task bucket every preset time (such as every second), and if so, to notify the task execution thread allocated to the task bucket to perform task consumption.
[0124] As can be seen, the task bucket is created according to the corresponding resource identifier, the tasks of different resources are stored in different task buckets, and the tasks of each resource are stored in the task bucket in parallel, and the tasks are consumed from the task bucket, thereby ensuring the efficiency and timing of the execution of the tasks of the same resource.
[0125] 4. Support for task filtering.
[0126] For example, by determining the filtering relationship of the tasks, the task filtering condition can be set as needed according to different requirements.
[0127] For example, in order to meet the timing and avoid invalid task accumulation, the filtering condition can be set as follows: if the generation time of the existing task in the task bucket is earlier than the generation time of the current pushed task (i.e., the above-mentioned to-be-added task), the existing task can be filtered according to the task type of the current pushed task.
[0128] In one example, the task filtering condition of the task bucket can be as shown in Table 1:
[0129] Table 1
[0130]
[0131] For example, in the case where a task exists for resource m needs to be pushed into the task bucket, it can be determined whether the task bucket exists for resource m in the task processor, and if the task bucket exists, it is determined whether a task exists in the task bucket whose generation time is less than the generation time of the currently pushed task. If a task exists whose generation time is less than the generation time of the currently pushed task, task filtering is performed according to the task type of the currently pushed task and the task type of the task whose generation time is less than the generation time of the currently pushed task.
[0132] For example, if the data source m issues a delete task, the just-issued add, configure, arm, subscribe, etc. tasks can be filtered; if the data source m continuously issues multiple configure tasks, it can be determined that the last configure task is the actual configure task, and the previous configure tasks can be filtered; if a data source channel delete task is issued, the previously issued data source channel add task can be filtered; if a data target modification task is issued, multiple modification tasks previously issued can be filtered; if a data target delete task is issued, the previously issued add and modification tasks can be filtered; if a third-party resource delete task is issued, the previously issued add task can be filtered, etc.
[0133] It can be seen that by setting the filtering conditions in the above manner, for the multi-resource multi-task batch issuance scenario, the kernel resource consumption can be effectively reduced, the effective task execution of the resource can be greatly promoted, and the use experience of the customer can be improved.
[0134] 5. Support for task retry.
[0135] For example, in order to improve product reliability and ease of use, task retry can be supported, including resource login retry, arm retry, configure task retry, etc.
[0136] For example, by setting a retry delay time for a task that fails to execute, deleting the task that is being executed in the task bucket, and pushing it into the task bucket again, task retry can be achieved.
[0137] It can be seen that by setting the task retry function, the reliability of task execution is improved, and since the execution time of the retry task is reset, the execution of new tasks is not affected, and the task timing is strictly guaranteed. In addition, the retry task can exist and execute until it is successfully executed or filtered and deleted, and the task effectiveness is strictly guaranteed.
[0138] The task processing flow implemented based on the above task processor is described below.
[0139] In this embodiment, the task processing flow can include:
[0140] 1. Configure the task container.
[0141] 2. Configure the task processor.
[0142] 3. Configure a task bucket.
[0143] Exemplarily, the attributes of the tasks in the task bucket can include but are not limited to: resource identifier, UUID, generation time, execution time, retry flag, dirty task flag.
[0144] Exemplarily, the task bucket can include three states: tasks are ready (there are tasks that reach the execution time and have not been executed), tasks are not ready (there are tasks that do not reach the execution time and have not been executed), and no tasks.
[0145] Exemplarily, the process of pushing tasks into the task bucket and consuming tasks from the task bucket can be respectively referred to Figure 4 and Figure 5 .
[0146] 4. The task bucket attribute implements that tasks of the same resource are stored in a task bucket (implemented by matching the resource identifier), and the tasks in the task bucket are sorted in ascending order according to the execution time.
[0147] 5. When a task execution fails, a retry delay time can be set for the task that fails to execute, a retry task is generated, and the task is pushed into the task bucket again.
[0148] 6. When there is a new task to be pushed into the task bucket, the task bucket can be filtered first, and then the task is pushed into the task bucket.
[0149] 7. Task consumption processing.
[0150] Exemplarily, the task processing can be classified according to the actual resource business, such as: adding, deleting, and configuring a collection device; adding, deleting, and configuring a storage device; device state statistics; data reporting statistics; arming and disarming, etc.
[0151] Exemplarily, when a task execution exception or failure occurs, task retry can be performed until the task is successfully executed and deleted or the task is filtered out.
[0152] The method provided in the present application is described above. The device provided in the present application is described below:
[0153] Please refer to Figure 6 , which is a structural schematic diagram of a task processing device provided in an embodiment of the present application, as shown in Figure 6 , the task processing device can include:
[0154] The receiving unit 610 is configured to receive a task adding instruction; the task adding instruction includes a task to be added and a resource identifier, the resource identifier being used to uniquely identify a resource to which the task to be added is to be issued;
[0155] The adding unit 620 is configured to add the task to be added to a target task queue according to a resource identifier of the task to be added, the target task queue having the same resource identifier as the task to be added;
[0156] The processing unit 630 is configured to execute a first target task in a task queue that reaches a task execution time by a task execution thread allocated to the task queue.
[0157] In some embodiments, the adding unit 620 is specifically configured to determine whether the target task queue exists; if not, create the target task queue and add the task to be added to the target task queue; if yes, add the task to be added to the target task queue.
[0158] The adding unit 620 is further configured to release a task queue if there is no task in the task queue.
[0159] In some embodiments, the adding unit 620 is further configured to filter a second target task in the target task queue if the second target task has a generation time earlier than a generation time of the task to be added, according to a task type of the second target task and a task type of the task to be added.
[0160] In some embodiments, the adding unit 620 is further configured to determine whether the task to be added is a retry task; if the task to be added is the retry task, determine whether a current head task in the target task queue is a dirty task; if yes, delete the current head task in the target task queue and add the task to be added to the target task queue; wherein the retry task is a task in the target task queue that fails to be executed and supports task retry; the task execution time of each task in the task queue from the head to the tail is sequentially increased, and the execution time of the retry task is determined according to a latest addition time and a preset delay time; if the task to be added is not the retry task, add the task to be added to the target task queue.
[0161] In some embodiments, the processing unit 630 is specifically configured to traverse each task in a task queue in order from the head to the tail if the task queue is not empty; wherein the task execution time of each task in the task queue from the head to the tail is sequentially increased.
[0162] If the current head task is the dirty task, delete the current head task, determine a next task of the deleted task as a latest head task, and continue the traversal.
[0163] If the current head task is a non-dirty task and the task execution time of the current head task has arrived, the current head task is set as a dirty task, and a task execution thread allocated for the task queue is informed to execute the current head task in the task queue through the task execution thread, and the iteration is ended;
[0164] If the current head task is a non-dirty task and the task execution time of the current head task has not arrived, the iteration is ended.
[0165] If the task queue is empty, the iteration is ended.
[0166] In some embodiments, the processing unit 630 is specifically configured to allocate a task execution thread for the task queue in the case that the task queue is not allocated with a task execution thread, and inform the task execution thread allocated for the task queue.
[0167] The processing unit 630 is further configured to, for any task queue allocated with a task execution thread, release the task execution thread allocated for the task queue in the case that the task execution time of the current head task in the task queue has not arrived.
[0168] Please refer to Figure 7 A hardware structure schematic diagram of an electronic device is provided for the embodiments of the present application. The electronic device can include a processor 701, a communication interface 702, a memory 703 and a communication bus 707. The processor 701, the communication interface 702 and the memory 703 complete the communication among each other through the communication bus 707. Among them, the memory 703 stores a computer program; the processor 701 can execute the task processing method described above by executing the program stored in the memory 703.
[0169] The memory 703 mentioned in the present application can be any electronic, magnetic, optical or other physical storage device, and can contain or store information such as executable instructions, data, etc. For example, the memory 703 can be a RAM (Random Access Memory, random access memory), a volatile memory, a non-volatile memory, a flash memory, a storage drive (such as a hard disk drive), a solid state disk, any type of storage disk (such as an optical disk, a DVD, etc.), or similar storage media, or a combination thereof.
[0170] The embodiments of the present application also provide a computer readable storage medium storing a computer program, for example Figure 7 The computer program can be executed by the processor 701 in the electronic device shown in Figure 7 to realize the task processing method described above.
[0171] The embodiments of the present application also provide a computer program stored in a computer readable storage medium, for example, the memory 703 in the computer 700, and when the processor executes the computer program, the processor 701 is caused to perform the task processing method described above. Figure 7
[0172] It should be noted that the relative terms such as first and second etc. are used herein only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply that there is any such actual relationship or order between these entities or operations. Moreover, the terms "comprise", "contain" or any other variant thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements not only includes those elements, but also includes other elements not explicitly listed or inherent to such a process, method, article or device. Without more limitations, the element defined by the statement "comprising a" does not exclude the presence of other identical elements in the process, method, article or device including the element.
[0173] The above only describes the preferred embodiments of the present application and is not intended to limit the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.
Claims
1. A task processing method characterized by, The method comprises the following steps: receiving a task adding instruction; the task adding instruction comprises a task to be added and a resource identifier, the resource identifier being used for uniquely identifying a resource for issuing the task to be added; adding the task to be added to a target task queue according to the resource identifier of the task to be added, the resource identifier of the target task queue being consistent with the resource identifier of the task to be added; tasks with the same resource identifier are gathered in the same task queue; for any task queue, executing a first target task in the task queue that reaches a task execution time by a task execution thread allocated for the task queue; the step of executing the first target task by the task execution thread allocated for the task queue comprises the following steps: for any task queue, if the task queue is not empty, traversing tasks in the task queue in order from a head to a tail; the task execution times of the tasks in the task queue from the head to the tail are sequentially increased; if the current head task is a dirty task, deleting the current head task, determining a next task of the deleted task as a latest head task, and continuing the traversal; if the current head task is a non-dirty task and the task execution time of the current head task has arrived, setting the current head task as a dirty task, and notifying the task execution thread allocated for the task queue to execute the current head task in the task queue by the task execution thread, and ending the traversal; if the current head task is a non-dirty task and the task execution time of the current head task has not arrived, ending the traversal; if the task queue is empty, ending the traversal.
2. The method of claim 1, wherein, the step of adding the task to be added to the target task queue comprises the following steps: determining whether the target task queue exists; if the target task queue does not exist, creating the target task queue and adding the task to be added to the target task queue; if the target task queue exists, adding the task to be added to the target task queue; the method further comprises the following steps: for any task queue, if there is no task in the task queue, releasing the task queue.
3. The method of claim 1, wherein, before the step of adding the task to be added to the target task queue, the method further comprises the following steps: if there is a second target task in the target task queue, the generation time of which is earlier than the generation time of the task to be added, filtering the second target task according to the task type of the second target task and the task type of the task to be added.
4. The method of claim 1, wherein, the step of adding the task to be added to the target task queue comprises the following steps: determining whether the task to be added is a retry task; if the task to be added is a retry task, determining whether the current head task in the target task queue is a dirty task; if yes, deleting the current head task in the target task queue and adding the task to be added to the target task queue; wherein the retry task is a task in the target task queue that fails to be executed and supports task retry; the task execution times of tasks in a task queue from a head to a tail are sequentially increased, and the execution time of the retry task is determined according to a latest addition time and a preset delay time. If the to-be-added task is not a retry task, the to-be-added task is added to a target task queue.
5. The method of claim 1, wherein, The notification is a task execution thread allocated to the task queue, and includes: In a case where the task queue is not allocated a task execution thread, a task execution thread is allocated to the task queue, and the task execution thread allocated to the task queue is notified; The method further includes: For any task queue allocated a task execution thread, in a case where the task execution time of a current head task of the task queue has not arrived, the task execution thread allocated to the task queue is released.
6. A task processing apparatus characterized by comprising: Include: A receiving unit configured to receive a task adding instruction; the task adding instruction includes a to-be-added task and a resource identifier, and the resource identifier is used to uniquely identify a resource of the to-be-added task; An adding unit configured to add the to-be-added task to a target task queue according to the resource identifier of the to-be-added task, and the resource identifier of the target task queue is consistent with the resource identifier of the to-be-added task; tasks with the same resource identifier are collected in the same task queue; A processing unit configured to execute, for any task queue, a first target task in the task queue whose task execution time has arrived through a task execution thread allocated to the task queue; The processing unit is specifically configured to, for any task queue, traverse tasks in the task queue in an order from a head to a tail in a case where the task queue is not empty; and the task execution times of the tasks in the task queue from the head to the tail are sequentially increased; If the current head task is a dirty task, the current head task is deleted, a next task of the deleted task is determined as a latest head task, and the traversal is continued; If the current head task is a non-dirty task and the task execution time of the current head task has arrived, the current head task is set as a dirty task, a task execution thread allocated to the task queue is notified, the current head task in the task queue is executed through the task execution thread, and the traversal is ended; If the current head task is a non-dirty task and the task execution time of the current head task has not arrived, the traversal is ended; If the task queue is empty, the traversal is ended.
7. The apparatus according to claim 6, wherein The adding unit is specifically configured to determine whether the target task queue exists; if not, the target task queue is created, and the to-be-added task is added to the target task queue; and if yes, the to-be-added task is added to the target task queue; The adding unit is further configured to, for any task queue, release the task queue in a case where no task exists in the task queue; And / or The adding unit is further configured to, in a case where a second target task with a generation time earlier than a generation time of the to-be-added task already exists in the target task queue, filter the second target task according to a task type of the second target task and a task type of the to-be-added task; And / or The adding unit is further configured to determine whether the task to be added is a retry task; if the task to be added is a retry task, determine whether a current head task in the target task queue is a dirty task; if yes, delete the current head task in the target task queue and add the task to be added to the target task queue; wherein the retry task is a task in the target task queue that fails to be executed and supports task retry; task execution time of each task in the task queue from the head to the tail is sequentially increased, and the execution time of the retry task is determined according to the latest adding time and a preset delay time; if the task to be added is not a retry task, the task to be added is added to the target task queue; And / or, The processing unit is specifically configured to, in a case where the task queue is not assigned with a task execution thread, assign the task queue with a task execution thread and notify the task execution thread assigned to the task queue; The processing unit is further configured to, for any task queue assigned with a task execution thread, release the task execution thread assigned to the task queue in a case where task execution time of a current head task of the task queue does not arrive.
8. An electronic device, comprising: The device comprises a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory complete mutual communication through the communication bus; The memory is used for storing a computer program; The processor is used for executing the program stored on the memory to implement the method in any one of claims 1-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 implement the method in any one of claims 1-5.
Citation Information
Patent Citations
Task sending processing method and device, task processing method, device and system, and equipment
CN113434307A