A task processing method, device, electronic device and storage medium
By attaching expired task duration and supervision duration to the task execution thread, the problem that the task execution thread cannot be automatically released in the event of failure is solved, and efficient resource utilization and automatic fault handling are achieved.
Patent Information
- Application Number
- CN202111646980.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-30
- Publication Date
- 2025-06-20
- Estimated Expiration
- 2041-12-30
AI Technical Summary
In the prior art, task execution threads cannot be automatically released when an application fails, resulting in other task threads being unable to continue to execute processing tasks, or multiple supervised threads are needed to prevent the expiration lock from being extended, resulting in wasting server resources.
By establishing a task execution thread for pending tasks and attaching an expired task duration, determining the supervision time to avoid unlimited extension of the task execution time, and deciding whether to release the task execution thread based on the execution result.
It realizes that task execution threads are automatically released in the event of failure, avoiding wasting server resources and does not require the establishment of multiple supervised threads.
Smart Images

Figure CN114296901B_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present invention relate to the field of computer technologies, and in particular, to a task processing method, apparatus, electronic device, and storage medium. Background Art
[0002] In a distributed system, a server may include multiple application programs to execute corresponding processing tasks.
[0003] Generally, based on the task threads in each application software, each processing task can be executed. During the execution of the processing task by the task thread, it is not allowed to be interfered by other task threads. Therefore, an exclusive lock can be set to avoid this situation. However, when the application program fails in the prior art, the task thread cannot be automatically released, resulting in other task threads being unable to continue to execute the processing task, or a supervision thread needs to be established for each task thread to prevent the expiration lock from not having a thread to extend its expiration time in case of an unexpected failure of the application program, resulting in the failure of the execution of the processing task. However, such a method will cause a large amount of server resources to be wasted.
[0004] To solve the above problems, it is necessary to improve the task processing method to better execute the processing task. Summary of the Invention
[0005] The present invention provides a task processing method, apparatus, electronic device, and storage medium to achieve the effect of automatically releasing task threads and avoiding waste of server resources.
[0006] In a first aspect, an embodiment of the present invention provides a task processing method, including:
[0007] When receiving a task processing request, creating a task execution thread for the task to be processed, and attaching an expiration task duration corresponding to the task execution thread;
[0008] Determining a supervision duration corresponding to the task execution thread;
[0009] Based on the supervision duration and the expiration task duration, determining an execution result of the task to be processed;
[0010] Based on the execution result, determining whether to release the task execution thread.
[0011] In a second aspect, an embodiment of the present invention further provides a task processing apparatus, including:
[0012] A thread creation module, configured to create a task execution thread for the task to be processed when receiving a task processing request, and attach an expiration task duration corresponding to the task execution thread;
[0013] A supervision duration determination module, configured to determine a supervision duration corresponding to the task execution thread;
[0014] An execution result determination module, configured to determine an execution result of the to-be-processed task based on the supervision duration and the expired task duration;
[0015] A thread release module, configured to determine whether to release the task execution thread based on the execution result.
[0016] In a third aspect, an embodiment of the present invention further provides an electronic device, where the electronic device includes:
[0017] One or more processors;
[0018] A storage device, configured to store one or more programs,
[0019] When the one or more programs are executed by the one or more processors, the one or more processors implement the task processing method according to any one of the embodiments of the present invention.
[0020] In a fourth aspect, an embodiment of the present invention further provides a storage medium containing computer-executable instructions, where the computer-executable instructions are used to execute the task processing method according to any one of the embodiments of the present invention when executed by a computer processor.
[0021] In the technical solution of this embodiment, when a task processing request is received, a task execution thread is established for the task to be processed, and an expiration task duration corresponding to the task execution thread is attached. According to the task types of each task to be processed, the corresponding expiration task duration is determined. When the task to be processed cannot be completed within the expiration task duration, the task execution duration can be extended for the task execution thread through an expiration exclusive lock to ensure the smooth completion of the task to be processed. The supervision duration corresponding to the task execution thread is determined to prevent the expiration exclusive lock from infinitely extending the task execution duration, resulting in the task to be processed being in an execution state all the time and unable to proceed with the next operation on the task to be processed. Based on the supervision duration and the expiration task duration, the execution result of the task to be processed is determined. By querying whether the task to be processed and the task execution thread exist, the execution result of the task to be processed can be determined. Based on the execution result, it is determined whether to release the task execution thread. If the task to be processed is successfully completed within the supervision duration, the task execution thread will be automatically released after executing the task to be processed. If the task to be processed is still not completed within the supervision duration, it can be determined that there is a problem during the execution of the task to be processed. The task to be processed can be closed, and the task execution thread can be removed at the same time. This solves the problems in the prior art that the task execution thread cannot be released when a failure occurs during task execution, or multiple task supervision threads are established, resulting in waste of server resources, and realizes the effect that when a task execution fails, the task execution thread is automatically released, and there is no need to set multiple task supervision threads to avoid waste of server resources. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] To more clearly illustrate the technical solutions of the exemplary embodiments of the present invention, the following briefly introduces the drawings required for describing the embodiments. Obviously, the introduced drawings are only the drawings of a part of the embodiments to be described by the present invention, rather than all the drawings. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.
[0023] Figure 1 It is a flowchart of a task processing method provided in Embodiment 1 of the present invention;
[0024] Figure 2 It is a flowchart of a task processing method provided in Embodiment 2 of the present invention;
[0025] Figure 3 It is a flowchart of a task processing method provided in Embodiment 3 of the present invention;
[0026] Figure 4 It is a flowchart of a task processing method provided in Embodiment 3 of the present invention;
[0027] Figure 5Schematic structural diagram of a task processing device provided in the fourth embodiment of the present invention;
[0028] Figure 6 Schematic structural diagram of an electronic device provided in the fifth embodiment of the present invention. Detailed implementation manners
[0029] The present invention will be further described in detail below with reference to the accompanying drawings and embodiments. It can be understood that the specific embodiments described herein are only used to explain the present invention, rather than limiting the present invention. Additionally, it should be noted that for the sake of description, only parts related to the present invention are shown in the accompanying drawings, rather than all structures.
[0030] Before elaborating on the technical solution in detail, the application scenario of the technical solution will be introduced to facilitate a clearer understanding of the technical solution. In a distributed system, a server can be regarded as an overall system. When receiving a task processing request, it can distribute the received tasks to each application program in the system in a round-robin manner for processing. When an application program executes the task, it will automatically create a task thread for the task to execute the task. At the same time, to prevent interference from other threads during the process of the task thread processing the task, the application program will also create an exclusive lock for the task thread that is executing the task to ensure that no other threads can perform any operation processing on this task during the period when the task thread executes the task. However, the existing method of creating an exclusive lock for the task thread to ensure the smooth progress of the task has certain defects. For example, when the application program executing the task shuts down unexpectedly, etc., the task thread cannot be released, and thus the task cannot be initiated and executed again; or, if a dedicated thread for supervising the exclusive lock is created for each thread, it will consume a large amount of server resources. In order to be able to execute tasks better in a distributed system, the technical solution provided by the present technical solution can be adopted.
[0031] Embodiment 1
[0032] Figure 1 Schematic flowchart of a task processing method provided in the first embodiment of the present invention. This embodiment is applicable to a situation in a distributed system where when a task node fails, the task thread is automatically released, and there is no need to create a corresponding thread for extending the expiration time of the task for the exclusive lock of each task thread. This method can be executed by a task processing device, and the device can be implemented in the form of software and / or hardware. The hardware can be an electronic device, such as a mobile terminal or a PC, etc.
[0033] As Figure 1 shown, the method includes:
[0034] S110. When receiving a task processing request, create a task execution thread for the task to be processed, and append an expiration task duration corresponding to the task execution thread.
[0035] Among them, when a user needs to execute a certain task, the user can input the task to be executed through the display interface of the server, and then the server can generate a corresponding task processing request based on the user's operation. The task processing request can be understood as a request generated based on the user's operation. The task processing request may contain information related to the task being processed, such as the task type, task name, and other information related to the task. The task to be processed can be understood as the task carried in the task processing request and needs to be processed by the application program in the server. The task execution thread can be understood as a thread used to execute the task to be processed. The task thread can be created by the application program. Each task to be processed can correspond to a task thread. Generally speaking, when the task thread executes the task to be processed, other threads cannot perform any processing on this task anymore. During the execution of the task to be processed by the task thread, an expiration exclusive lock can be added to the task thread to ensure that the task thread is not interfered by other threads during the execution of the task to be processed. At the same time, the task execution duration of the task execution thread for the task to be processed can also be extended. The expiration task duration can be understood as the duration corresponding to ensuring that the task thread is not interfered by other threads by the expiration exclusive lock.
[0036] Specifically, the user can initiate a task processing request through the display interface of the server. After receiving the task processing request, the server can distribute the task to be processed to the corresponding application program for processing. Among them, the method for the server to distribute tasks to each application program can be round-robin distribution. That is to say, when there are multiple application programs, the received tasks can be distributed to each application program in turn; or, it can also be to pre-query the task execution status of each application program, and according to the obtained task execution status result, preferentially distribute the task to be processed to the application program with a short waiting duration for processing.
[0037] When the application program receives the task processing request, first create a task execution thread for the task to be processed to execute the task to be processed. At the same time, the application program can also add an expiration exclusive lock to the task execution thread to ensure that during the execution of the task to be processed by the task execution thread, other threads will no longer perform any processing on the task to be processed. It should be noted that the expiration task duration can be set in advance for the expiration exclusive lock. For example, the expiration duration can be set to 5 minutes. That is to say, since the expiration exclusive lock is added to the task execution thread, the expiration exclusive lock can ensure that within 5 minutes, other threads will not perform any operation processing on the task to be processed.
[0038] Optionally, establishing a task execution thread for the task to be processed and attaching an expiration task duration corresponding to the task execution thread includes: determining the expiration task duration according to the task type of the task to be processed; and establishing the task execution thread for the task to be processed.
[0039] Specifically, different tasks to be processed require different execution times. For example, some tasks to be processed may require an execution duration of 1 hour, some may require 3 hours, or some may require 1 day or even longer. Considering that in actual applications, due to different network speeds or server performances, the actual execution duration corresponding to each task to be processed on different servers may differ from the preset execution duration, and it is necessary to determine the execution duration required for the task to be processed according to its execution status. To ensure the smooth execution of the task to be processed, while adding an expiration exclusive lock to the task thread, the expiration task duration of the expiration exclusive lock can also be set. That is to say, the expiration lock can add an execution duration to the task execution thread of the task to be processed to ensure the smooth completion of the task to be processed.
[0040] S120. Determine the supervision duration corresponding to the task execution thread.
[0041] In actual applications, although the expiration exclusive lock can add an execution duration to the task execution thread during the execution of the task to be processed, to avoid the situation where the task execution thread remains in the task execution state when the task to be processed encounters an execution error or other conditions, the expiration exclusive lock cannot add an execution duration to the task execution thread without limit. Instead, an upper limit needs to be set for the expiration exclusive lock to extend the expiration task duration of the task execution thread. That is to say, to prevent the expiration exclusive lock from extending the execution duration of the task execution thread without limit, a supervision duration can be added to the expiration exclusive lock. For example, the supervision duration can be set to 3 hours. Within these 3 hours, the expiration exclusive lock can extend the execution duration of the task execution thread. When it exceeds 3 hours, the expiration exclusive lock will no longer be able to extend the execution duration of the task execution thread.
[0042] Specifically, considering that the durations required to execute each task to be processed are different, and during the execution, due to factors such as differences in network or server performance, there may be situations where the task to be processed cannot be completed within the preset duration. To ensure the smooth completion of the task to be processed, the expiration exclusive lock can be used to extend the expiration task duration of the task execution thread for executing the task to be processed, but the expiration task duration of the task execution thread cannot be extended without limit. It is necessary to preset a supervision duration to limit the extension of the expiration task duration of the task execution thread by the expiration exclusive lock.
[0043] Exemplarily, when an execution error occurs during the execution of a task to be processed, resulting in the task to be processed being unable to be completed all the time, the task to be processed should be closed at this time, or the execution status of the task to be processed should be fed back to the staff, and the staff determines whether to continue executing the task to be processed. However, since the expired exclusive lock can extend the expiration time of the expired task for the task execution thread, if the corresponding supervision duration is not set for the expired exclusive lock, the expired exclusive lock will always operate to extend the expiration time of the expired task for the task execution thread, resulting in the task to be processed being in the execution state all the time and unable to proceed with the next operation on the task to be processed. Therefore, when adding an expired exclusive lock to the task thread, it is also necessary to set the corresponding supervision duration.
[0044] Optionally, determining the supervision duration corresponding to the task execution thread includes: determining the supervision duration corresponding to the task to be processed according to the task type of the task to be processed and a pre-established mapping relationship table; or, retrieving the pre-stored supervision duration as the supervision duration of the task to be processed.
[0045] It can be understood that the supervision duration of the task to be processed can be pre-set or set based on each task to be processed. The mapping relationship table can be understood as a corresponding relationship table established between each task to be processed and the corresponding supervision duration when the supervision duration is set according to each task to be processed.
[0046] Specifically, for each application program in a server, when each application program receives a task to be processed, it will automatically create a task execution thread for the task to be processed, add a corresponding expired exclusive lock to the task execution thread, and can also set the corresponding supervision duration. Among them, the method for determining the supervision duration can be that each application program uses a unified supervision duration. For example, it can be set to 5 hours. That is to say, regardless of the length of the execution duration required for the task to be processed, the time for which the expired exclusive lock can extend the expiration time of the expired task for the task execution thread corresponding to the task to be processed is 5 hours. After 5 hours, the expired exclusive lock will not continue to operate to extend the expiration time of the expired task for the task execution thread.
[0047] Alternatively, the supervision duration corresponding to each task to be processed can also be determined according to the task type of the task to be processed and a pre-established mapping relationship table. Exemplarily, when the task type of the task to be processed is relatively conventional or simple, correspondingly, the corresponding task execution duration is shorter, then the supervision duration corresponding to the task to be processed can be set shorter, for example, it can be set to 1 hour; when the task type of the task to be processed is relatively complex, correspondingly, the corresponding task execution duration is longer, then the supervision duration corresponding to the task to be processed can be set longer, for example, it can be set to 5 hours or even a longer duration. The specific supervision duration can be set according to actual needs and is not specifically limited here.
[0048] S130. Determine the execution result of the to-be-processed task based on the supervision duration and the expired task duration.
[0049] Among them, the execution result can be understood as the result of whether the to-be-processed task is successfully executed within the specified duration.
[0050] Specifically, when the server distributes each to-be-processed task to each application program for execution, each application program will establish a corresponding execution task thread for each to-be-processed task. At the same time, it will also apply to the server for an expired exclusive lock corresponding to each execution task thread to ensure that the task execution thread is not interfered by other task threads during the processing of the to-be-processed task, and extend the expired task duration for the task execution thread. To avoid the expired exclusive lock extending the expired task duration for the task execution thread without limit, it is also necessary to set a supervision duration for the expired exclusive lock. That is to say, only within the supervision duration can the expired exclusive lock extend the expired task duration for the task execution thread. According to the expired task duration corresponding to the to-be-processed task and the supervision duration corresponding to the expired exclusive lock, it can be determined whether the to-be-processed task is successfully executed.
[0051] It should be noted that before the expired exclusive lock extends the expired task duration for the task execution thread, a daemon thread needs to be established in advance in the application program to check whether the to-be-processed task and the corresponding task execution thread exist, and whether it is necessary to extend the expired task duration for the task execution thread. When the to-be-processed task and the task execution thread exist at the same time, it indicates that the to-be-processed task is being executed, and then the expired exclusive lock is used to extend the expired task duration for the task execution thread; if it is detected that the to-be-processed task or the task execution thread disappears, it means that a failure occurs during the execution of the to-be-processed task, and at this time, the expired task duration is no longer extended for the task execution thread.
[0052] Exemplarily, if the normal execution time of the task to be processed is 1 hour, when the application processes the task to be processed, it can set the expiration task duration of the expiration exclusive lock added to the task execution thread corresponding to the task to be processed to 1 hour. Considering possible network delays or server performance differences in actual situations, the expiration exclusive lock can appropriately extend the expiration task duration of the task execution thread. Suppose the expiration exclusive lock can extend the expiration task duration of the task execution thread by 5 minutes each time. When the daemon thread detects the simultaneous existence of the task to be processed and the task execution thread, based on the current moment, it will extend the expiration task duration of the task execution thread by 5 minutes. At the same time, it is also necessary to set the supervision duration of the daemon thread, which can be set to 2 hours, that is to say, within 2 hours, the daemon thread allows the expiration exclusive lock to extend the expiration task duration of the task execution thread. Then, according to whether the task to be processed is successfully completed within the supervision duration and the expiration task duration, it can be determined that the execution result of the task to be processed is successful execution or failed execution.
[0053] S140. Based on the execution result, determine whether to release the task execution thread.
[0054] Specifically, generally speaking, after the task execution thread normally completes the task to be processed, it will automatically release the thread. That is to say, when the execution result of the task to be processed is successful execution, the task execution thread can actively release the corresponding task execution thread, and the server does not need to perform the operation of releasing the task thread. However, when problems occur during the processing of the task to be processed and it fails to be successfully completed within the supervision duration and the expiration task duration, that is, when the execution result is failed execution, the server needs to actively release the task execution thread corresponding to the task to be processed so that other task execution threads can process the task to be processed.
[0055] In the technical solution of this embodiment, when a task processing request is received, a task execution thread is established for the task to be processed, and an expiration task duration corresponding to the task execution thread is attached. According to the task types of the tasks to be processed, the corresponding expiration task durations are determined. When a task to be processed cannot be completed within the expiration task duration, an expiration exclusive lock can be used to extend the task execution duration of the task execution thread to ensure the smooth completion of the task to be processed. The supervision duration corresponding to the task execution thread is determined to prevent the expiration exclusive lock from infinitely extending the task execution duration, resulting in the task to be processed always being in execution and unable to continue with the next operation on the task to be processed. Based on the supervision duration and the expiration task duration, the execution result of the task to be processed is determined. By querying whether the task to be processed and the task execution thread exist, the execution result of the task to be processed can be determined. Based on the execution result, it is determined whether to release the task execution thread. If the task to be processed is successfully completed within the supervision duration, the task execution thread will be automatically released after executing the task to be processed. If the task to be processed is still not completed within the supervision duration, it can be determined that there is a problem during the execution of the task to be processed. The task to be processed can be closed, and at the same time, the task execution thread is removed. This solves the problems in the prior art that the task execution thread cannot be released when a failure occurs during task execution, or multiple task supervision threads are established, resulting in waste of server resources. It realizes the effect that when a task execution fails, the task execution thread is automatically released, and there is no need to set multiple task supervision threads to avoid waste of server resources.
[0056] Embodiment 2
[0057] As an optional embodiment of the above embodiment, Figure 2 FIG. is a schematic flowchart of a task processing method provided by Embodiment 2 of the present invention. Optionally, the determination of the execution result of the task to be processed based on the supervision duration and the expiration task duration is refined.
[0058] As Figure 2 shown, the method includes:
[0059] S210. When a task processing request is received, a task execution thread is established for the task to be processed, and an expiration task duration corresponding to the task execution thread is attached.
[0060] S220. Determine the supervision duration corresponding to the task execution thread.
[0061] S230. Store the task identifier, expiration task duration, thread identifier of the task execution thread, and supervision duration of the task to be processed in a record list, and traverse the record information in the record list.
[0062] Among them, each application program in the server can process multiple tasks to be processed simultaneously. Therefore, it is necessary to establish a task identifier for each task to be processed. The task identifier can be understood as the identifying information corresponding to the task to be processed, used to mark the task to be processed. Correspondingly, each task to be processed can correspond to a task execution thread. The thread identifier can be understood as the identifying information corresponding to the task execution thread, used to identify the task execution thread corresponding to the task to be processed. The record list can be understood as a list file that can be preset in each application program, used to record information such as the task type of each task to be processed, the corresponding task execution thread, and the preset supervision duration, etc.
[0063] Specifically, there is a preset record list in each application program in the server. When the server distributes the tasks to be processed to each application program and starts the application program, information related to the tasks to be processed, such as task type, task identifier, the corresponding task execution thread, thread identifier, and supervision duration, etc., can be recorded in the record list. It should be noted that each application program also includes a daemon thread, used to query the information in the record list. By traversing the recorded information in the record list by the daemon thread, it can be determined whether the task to be processed and the task execution thread exist, and whether the task to be processed exceeds the expired task duration and the supervision duration, etc.
[0064] S240. Determine whether the current supervision duration exceeds the supervision duration based on the record list. If so, execute S250; if not, and the task identifier is included in the record list, execute S260; if not, and the task identifier is not included in the record list, execute S270.
[0065] Specifically, according to the recorded information in the record list, the current execution situation of the task to be processed and whether the current supervision duration exceeds the supervision duration can be determined, and then the next operation information can be determined based on the obtained information.
[0066] S250. If so, and the task identifier is included in the record list, remove a set of data associated with the task identifier from the record list and send a warning message.
[0067] Among them, the warning message can be understood as a prompt message sent to the staff after the task to be processed fails, used to prompt the staff to process the task to be processed.
[0068] Specifically, if the daemon thread finds that the execution duration of the task to be processed has exceeded the maximum supervision duration when traversing the record information in the record list, but the task identifier corresponding to the task to be processed can still be queried in the record list, that is, the task to be processed is still in the execution process even after exceeding the maximum supervision duration. At this time, it can be considered that there is a problem in the execution process of the task to be processed, and it is necessary to terminate the execution of the task to be processed, remove the task identifier corresponding to the task to be processed and the data associated with the task to be processed from the record list, and send a prompt message to the staff to remind the staff to operate on the task to be processed.
[0069] It should be noted that a set of data associated with the task identifier may include the task type, the corresponding task execution thread, and the thread identifier information. When the task to be processed is terminated, correspondingly, the task execution thread executing the task to be processed also needs to be released, and at the same time, the thread identifier corresponding to the task execution thread is removed from the record list.
[0070] S260. If not, and the task identifier is included in the record table, then extend the expiration task duration.
[0071] Specifically, if the daemon thread finds that the execution duration of the task to be processed has not exceeded the maximum supervision duration when traversing the record information in the record list, and there is a task identifier corresponding to the task to be processed in the record table, and the task to be processed also exists in the record table, it indicates that the task to be processed is still in the execution process. At this time, the expiration task duration of the task execution thread executing the task to be processed can be extended through the expiration exclusive lock to ensure that the task to be processed can be completed normally within the specified time.
[0072] Optionally, extending the expiration task duration includes: determining the extended expiration task duration according to the current supervision duration and the supervision duration.
[0073] Among them, the current supervision duration can be understood as the already supervised duration of the daemon thread for the expiration exclusive lock.
[0074] Specifically, a preset interval duration for the daemon thread to supervise the expiration exclusive lock is set, and at the same time, the already supervised duration of the daemon thread for the expiration exclusive lock is recorded to determine whether the maximum supervision duration is exceeded, and based on this, whether to extend the expiration task duration of the expiration exclusive lock is determined.
[0075] Exemplarily, when the task execution thread starts to execute the task to be processed, the record table in the application can record information such as the type of the task to be processed, the task identifier, the corresponding task execution thread, the thread identifier, and the supervision duration. If the preset supervision duration is 5 hours, when the daemon thread traverses the record table, it can determine the current supervision duration corresponding to the task to be processed according to the current time, that is, the current supervised duration. For example, if the elapsed time since the moment when the information related to the task to be processed was recorded is 2 hours, the current supervision duration is 2 hours, and the remaining supervision duration is 3 hours. According to the current supervision duration and the preset supervision duration, it can be determined whether to continue to extend the expiration task duration for the expired exclusive lock corresponding to the task execution thread.
[0076] S270. If not, and the record table does not include the task identifier, remove a set of data associated with the task identifier from the record table and report the task execution completion information.
[0077] Specifically, if the daemon thread finds that the execution duration of the task to be processed does not exceed the maximum supervision duration and does not find the task identifier corresponding to the task to be processed in the record table when traversing the record information in the record list, it indicates that the task to be processed has been successfully completed and the execution result is successful. At this time, it is necessary to remove a set of data associated with the task identifier of the task to be processed, such as task type, task identifier, the corresponding task execution thread, thread identifier, and supervision duration, etc. from the record table, and at the same time send the information that the task to be processed has been successfully completed to the server.
[0078] S280. Based on the execution result, determine whether to release the task execution thread.
[0079] For the technical solution of this embodiment, store the task identifier of the to-be-processed task, the expiration task duration, the thread identifier of the task execution thread, and the supervision duration in a record list, and traverse the record information in the record list to determine whether each to-be-processed task and the corresponding task execution thread exist. If they exist, determine whether the supervision duration corresponding to the to-be-processed task is exceeded, and determine whether to continue adding task execution duration to the task execution thread based on whether the supervision duration is exceeded. Based on the record list, determine whether the current supervision duration exceeds the supervision duration. If so, and the record list includes the task identifier, remove a set of data associated with the task identifier from the record list and send a warning message. If the supervision duration is exceeded, it indicates that the to-be-processed task has not been completed within the specified duration, and problems may occur. The to-be-processed task needs to be closed, the task execution thread needs to be released, and a prompt message needs to be sent to relevant personnel. If not, and the record list includes the task identifier, extend the expiration task duration; if not, and the record list does not include the task identifier, remove a set of data associated with the task identifier from the record list, report the task execution completion information, and after the to-be-processed task is executed, the task execution thread can be automatically released. This solves the problem of determining whether to release the task execution thread according to the execution status of the to-be-processed task when the to-be-processed task exceeds the supervision duration, realizes the effect of determining the execution result according to the expiration task duration and the supervision duration, and releasing the task execution thread based on the execution result.
[0080] Embodiment III
[0081] In a specific example, such as Figure 3As shown, in the scenario of a distributed system, according to the triggering operation of the user in the display interface of the server, a corresponding task execution request (i.e., task processing request) can be generated. Then, the server can poll and distribute this request to the corresponding task nodes (i.e., application programs) for execution processing. Among them, the task processing request contains information related to the task to be processed, such as task type and task name, etc. It can be understood that each task node can form a cluster with other task nodes. Taking task node 1 as an example, when the execution node receives the task to be processed, it will automatically create a task execution thread for the task to be processed to perform execution processing based on the task execution thread. At the same time, when the execution node starts, it can call the pre-created local daemon lock pool (i.e., record list) to store the information related to the task to be processed in the local daemon lock pool, including the task type, task identifier, corresponding execution thread (i.e., task execution thread), and thread identifier of the task to be processed. Task node 1 can apply for an expired exclusive lock from the remote expired lock pool to protect the task execution thread corresponding to the task to be processed. Among them, the remote expired exclusive lock can be understood as a database storing expired exclusive locks. Multiple expired exclusive locks can be stored in the remote expired lock pool, and information such as the expiration time of each expired exclusive lock can also be recorded. That is to say, the local daemon lock pool can also record the information of the expired exclusive lock corresponding to the task to be processed, such as, and the establishment time and maximum daemon duration (i.e., supervision duration) of the expired exclusive lock. Among them, the expired exclusive lock can be K11, K12... Kn. That is to say, during this period, the expired exclusive lock can ensure that no other task threads can process the task to be processed. It should be noted that each task node can process multiple tasks to be processed simultaneously, but during the execution of each task to be processed, only one task execution thread is allowed to process this task. To ensure that the task execution thread is not interfered by other task threads during the execution of the task to be processed.
[0082] In addition, when the execution node starts, it can also start the pre-created daemon thread 1 to supervise the task execution thread corresponding to the task to be processed. Based on the daemon thread 1, it can poll and read the expired exclusive locks in the local daemon lock pool. If the task execution thread corresponding to the expired exclusive lock is in a normal working state, an operation to delay (i.e., extend the expired task duration) can be performed for this thread.
[0083] Such as Figure 4 shown Figure 4It is a schematic flowchart of a task processing method provided in Embodiment 3 of the present invention. When the server receives a task execution request, it distributes the request to the corresponding execution node. When the execution node receives the request and processes the to-be-processed task carried in the request, it automatically creates a task execution thread for the to-be-processed task and applies to the remote expiration lock pool to construct a unique Key (i.e., an expiration exclusive lock) corresponding to the task thread. Among them, when constructing an expiration exclusive lock for the to-be-processed application, it can be created based on information such as the task name and task type of the to-be-processed task, that is, created based on the task identifier corresponding to the to-be-processed task. When the execution node starts, it can also retrieve the pre-created local daemon lock pool to record data information associated with the to-be-processed task, and traverse and query the information recorded in the local daemon lock pool based on the pre-created daemon thread. When the to-be-executed task is normally processed and completed, when the to-be-processed task is completed, the corresponding task execution thread can be automatically released. When the daemon thread traverses the local daemon lock pool, if it cannot query information corresponding to the task identifier of the to-be-processed task, it removes the data associated with the task identifier corresponding to the to-be-processed task from the local lock pool, removes the remote expiration lock corresponding to the task execution thread, and then sends the information that the to-be-processed task has been executed to the relevant staff.
[0084] At the same time, in order to prevent the expiration exclusive lock from infinitely extending the expiration task duration for the task execution thread, a maximum daemon time (i.e., supervision duration) can be set in advance. Within the supervision duration, the daemon thread can traverse and query the information recorded in the local daemon lock pool to determine whether the task identifier of the to-be-processed task and the thread identifier corresponding to the task execution thread exist, and at the same time, it can also obtain whether the daemon time (i.e., the current supervision duration) exceeds the maximum daemon time. If it exceeds the maximum daemon time, it means that there is an interruption in the task execution thread corresponding to the to-be-processed task. It removes the identification information corresponding to the expiration exclusive lock from the local daemon lock pool and sends an alarm notification (i.e., a warning message) to the staff to remind the staff to process the to-be-processed task, and then continues to query whether there is a next local daemon lock (i.e., an expiration exclusive lock). If not, this round of daemon ends; if so, it repeats the above execution process. If it does not exceed the maximum daemon time, it is necessary to determine whether the daemon thread can query the task identifier corresponding to the to-be-processed task in the local daemon lock pool. If the task identifier exists, it extends the expiration task duration of the corresponding task execution thread; if it does not exceed the maximum daemon time and the task identifier does not exist, it means that the to-be-processed task has been processed. It removes a set of data associated with the task identifier from the local daemon lock pool and at the same time sends a message indicating that the task has been successfully completed to the staff.
[0085] It should be noted that the daemon thread can traverse the local daemon lock pool at regular intervals, for example, it can be set to traverse the local daemon lock pool every 10 seconds. When it is detected that there are task identifiers and thread identifiers in the local daemon lock pool and the maximum daemon time has not been exceeded, the expiration task duration of all task execution threads in the local daemon lock pool is extended, for example, it can be extended by 300 seconds each time. If the task execution thread does not actively release it, the corresponding exclusive lock will exist indefinitely. When a problem occurs in the execution node, the task execution thread will also stop running. At this time, the exclusive lock will expire automatically, and the corresponding record information in the local daemon lock pool will be removed. When the daemon thread traverses again and cannot find the corresponding task execution thread, the operation of extending the expiration task duration will not be performed.
[0086] The advantage of this setting is that there is no need to create a supervision thread for each task execution thread corresponding to a task to be processed. Only based on the local daemon lock pool and the daemon thread created for each execution node, all task execution threads in the node can be managed, avoiding waste of server resources.
[0087] The technical solution of this embodiment, when receiving a task processing request, establishes a task execution thread for the task to be processed, attaches an expiration task duration corresponding to the task execution thread, determines the corresponding expiration task duration according to the task type of each task to be processed. When the task to be processed cannot be completed within the expiration task duration, the task execution duration can be extended for the task execution thread through the exclusive lock to ensure the smooth completion of the task to be processed. Determine the supervision duration corresponding to the task execution thread to prevent the exclusive lock from extending the task execution duration indefinitely, resulting in the task to be processed being in an execution state all the time and unable to continue with the next operation on the task to be processed. Based on the supervision duration and the expiration task duration, determine the execution result of the task to be processed. By querying whether the task to be processed and the task execution thread exist, the execution result of the task to be processed can be determined. Based on the execution result, determine whether to release the task execution thread. If the task to be processed is successfully completed within the supervision duration, the task execution thread will be automatically released after executing the task to be processed. If the task to be processed is still not completed within the supervision duration, it can be determined that there is a problem in the execution process of the task to be processed. The task to be processed can be closed, and at the same time, the task execution thread can be removed. It solves the problems in the prior art that the task execution thread cannot be released when a failure occurs during task execution, or multiple task supervision threads are established, resulting in waste of server resources, and realizes the effect of automatically releasing the task execution thread when a task execution failure occurs and not setting multiple task supervision threads to avoid waste of server resources.
[0088] Embodiment 4
[0089] Figure 5A task processing device provided in the fourth embodiment of the present invention, the device includes: a thread creation module 310, a supervision duration determination module 320, an execution result determination module 330, and a thread release module 340.
[0090] Among them, the thread creation module 310 is configured to, when receiving a task processing request, create a task execution thread for the task to be processed, so as to append an expiration task duration corresponding to the task execution thread;
[0091] The supervision duration determination module 320 is configured to determine the supervision duration corresponding to the task execution thread;
[0092] The execution result determination module 330 is configured to determine the execution result of the task to be processed based on the supervision duration and the expiration task duration;
[0093] The thread release module 340 is configured to determine whether to release the task execution thread based on the execution result.
[0094] The technical solution of this embodiment, when receiving a task processing request, creates a task execution thread for the task to be processed, so as to append an expiration task duration corresponding to the task execution thread, determines the corresponding expiration task duration according to the task type of each task to be processed. When the task to be processed cannot be completed within the expiration task duration, the task execution duration can also be extended for the task execution thread through an expiration exclusive lock to ensure the smooth completion of the task to be processed. Determine the supervision duration corresponding to the task execution thread to avoid the expiration exclusive lock infinitely extending the task execution duration, resulting in the task to be processed being in execution all the time and unable to continue with the next operation on the task to be processed. Based on the supervision duration and the expiration task duration, determine the execution result of the task to be processed. By querying whether the task to be processed and the task execution thread exist, the execution result of the task to be processed can be determined. Based on the execution result, determine whether to release the task execution thread. If within the supervision duration, the task to be processed is successfully completed, the task execution thread will be automatically released after executing the task to be processed; if within the supervision duration, the task to be processed is still not completed, it can be determined that there is a problem in the execution process of the task to be processed, and the task to be processed can be closed, and at the same time, the task execution thread can be removed. It solves the problems in the prior art that the task execution thread cannot be released when a failure occurs during task execution, or multiple task supervision threads are established, resulting in waste of server resources, and realizes the effect of automatically releasing the task execution thread when a task execution failure occurs, and does not require setting multiple task supervision threads to avoid waste of server resources.
[0095] Based on any optional technical solution in the embodiment of the present invention, optionally, the thread creation module includes:
[0096] An expired task duration determination sub-module, configured to determine the expired task duration according to the task type of the to-be-processed task;
[0097] A thread creation sub-module, configured to create the task execution thread for the to-be-processed task.
[0098] Based on any optional technical solution in the embodiments of the present invention, optionally, the supervision duration determination module includes:
[0099] A supervision duration determination sub-module, configured to determine the supervision duration corresponding to the to-be-processed task according to the task type of the to-be-processed task and a pre-established mapping relationship table; or,
[0100] A supervision duration retrieval sub-module, configured to retrieve the pre-stored supervision duration as the supervision duration of the to-be-processed task.
[0101] Based on any optional technical solution in the embodiments of the present invention, optionally, the execution result determination module includes:
[0102] A storage sub-module, configured to store the task identifier of the to-be-processed task, the expired task duration, the thread identifier of the task execution thread, and the supervision duration into a record list, so as to traverse the record information in the record list;
[0103] A supervision duration judgment sub-module, configured to determine whether the current supervision duration exceeds the supervision duration based on the record list;
[0104] An early warning information sending sub-module, configured to, if so, and if the record table includes the task identifier, remove a group of data associated with the task identifier from the record table, and send early warning information.
[0105] Based on any optional technical solution in the embodiments of the present invention, optionally, the task processing device further includes:
[0106] An expired task duration extension module, configured to, if not, and if the record table includes the task identifier, extend the expired task duration;
[0107] An information reporting module, configured to, if not, and if the record table does not include the task identifier, remove a group of data associated with the task identifier from the record table, and report task execution completion information.
[0108] Based on any optional technical solution in the embodiments of the present invention, optionally, the expired task duration extension module is configured to:
[0109] Determine the extended expired task duration according to the current supervision duration and the supervision duration.
[0110] The task processing device provided by the embodiments of the present invention can execute the task processing method provided by any embodiment of the present invention, and has the corresponding functional modules and beneficial effects for executing the method.
[0111] It should be noted that the various units and modules included in the above device are only divided according to functional logic, but are not limited to the above division, as long as the corresponding functions can be realized; in addition, the specific names of the functional units are only for the convenience of mutual distinction and are not used to limit the protection scope of the embodiments of the present invention.
[0112] Embodiment Five
[0113] Figure 6 It is a schematic structural diagram of an electronic device provided for Embodiment Five of the present invention. Figure 6 It shows a block diagram of an exemplary electronic device 40 suitable for implementing the embodiments of the present invention. Figure 6 The shown electronic device 40 is only an example and should not bring any limitation to the functions and usage scope of the embodiments of the present invention.
[0114] As Figure 6 shown, the electronic device 40 is presented in the form of a general-purpose computing device. The components of the electronic device 40 may include but are not limited to: one or more processors or processing units 401, a system memory 402, and a bus 403 connecting different system components (including the system memory 402 and the processing unit 401).
[0115] The bus 403 represents one or more of several types of bus structures, including a memory bus or a memory controller, a peripheral bus, a graphics acceleration port, a processor, or a local bus using any of the various bus structures. For example, these architectures include but are not limited to Industry Standard Architecture (ISA) bus, Micro Channel Architecture (MAC) bus, Enhanced ISA bus, Video Electronics Standards Association (VESA) local bus, and Peripheral Component Interconnect (PCI) bus.
[0116] The electronic device 40 typically includes a variety of computer system-readable media. These media can be any available media accessible by the electronic device 40, including volatile and non-volatile media, removable and non-removable media.
[0117] The system memory 402 may include computer system-readable media in the form of volatile memory, such as random access memory (RAM) 404 and / or cache memory 405. The electronic device 40 may further include other removable / non-removable, volatile / non-volatile computer system storage media. By way of example only, the storage system 406 may be used to read and write non-removable, non-volatile magnetic media ( Figure 6not shown, commonly referred to as a "hard disk drive"). Although Figure 6 not shown in Figure 6 , a disk drive for reading and writing to a removable non-volatile disk (such as a "floppy disk") and an optical disk drive for reading and writing to a removable non-volatile optical disk (such as a CD-ROM, DVD-ROM, or other optical medium) may be provided. In these cases, each drive may be connected to the bus 403 through one or more data medium interfaces. The memory 402 may include at least one program product having a set (such as at least one) of program modules configured to perform the functions of the embodiments of the present invention.
[0118] A program / utilities 408 having a set (at least one) of program modules 407 may be stored, for example, in the memory 402. Such program modules 407 include, but are not limited to, an operating system, one or more application programs, other program modules, and program data. Each or some combination of these examples may include an implementation of a network environment. The program modules 407 generally perform the functions and / or methods in the embodiments described in the present invention.
[0119] The electronic device 40 may also communicate with one or more external devices 409 (such as a keyboard, a pointing device, a display 410, etc.), and may also communicate with one or more devices that enable a user to interact with the electronic device 40, and / or communicate with any device that enables the electronic device 40 to communicate with one or more other computing devices (such as a network card, a modem, etc.). Such communication may be carried out through an input / output (I / O) interface 411. Also, the electronic device 40 may communicate with one or more networks (such as a local area network (LAN), a wide area network (WAN), and / or a public network, such as the Internet) through a network adapter 412. As shown in the figure, the network adapter 412 communicates with other modules of the electronic device 40 through the bus 403. It should be understood that although Figure 6 not shown in Figure 6 , other hardware and / or software modules may be used in conjunction with the electronic device 40, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems, etc.
[0120] The processing unit 401 executes various functional applications and data processing by running programs stored in the system memory 402, such as implementing the task processing method provided by the embodiments of the present invention.
[0121] Embodiment Six
[0122] Embodiment Six of the present invention also provides a storage medium containing computer-executable instructions, and the computer-executable instructions are used to execute a task processing method when executed by a computer processor. The method includes:
[0123] When receiving a task processing request, establish a task execution thread for the task to be processed, and attach an expiration task duration corresponding to the task execution thread;
[0124] Determine a supervision duration corresponding to the task execution thread;
[0125] Based on the supervision duration and the expiration task duration, determine an execution result of the task to be processed;
[0126] Based on the execution result, determine whether to release the task execution thread.
[0127] The computer storage medium of the embodiments of the present invention may adopt any combination of one or more computer-readable media. The computer-readable media may be a computer-readable signal medium or a computer-readable storage medium. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the above. More specific examples (non-exhaustive list) of the computer-readable storage medium include: an electrical connection having 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 this document, the computer-readable storage medium may be any tangible medium that contains or stores a program, and the program can be used by or in combination with an instruction execution system, apparatus, or device.
[0128] The computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries computer-readable program code. Such a propagated data signal may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. The computer-readable signal medium may also be any computer-readable medium other than the computer-readable storage medium, and the computer-readable medium can send, propagate, or transmit a program for use by or in combination with an instruction execution system, apparatus, or device.
[0129] The program code included on the computer-readable medium can be transmitted by any suitable medium, including - but not limited to - wireless, wire, optical cable, RF, etc., or any suitable combination of the above.
[0130] Computer program code for performing the operations of the embodiments of the present invention may be written in one or more programming languages or combinations thereof. The programming languages include object-oriented programming languages such as Java, Smalltalk, C++, and also include conventional procedural programming languages such as the "C" language or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, executed as a stand-alone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., through the Internet using an Internet service provider).
[0131] Note that the above is only the preferred embodiment of the present invention and the technical principles applied. Those skilled in the art will understand that the present invention is not limited to the specific embodiments described herein. Various obvious changes, re-adjustments, and substitutions can be made by those skilled in the art without departing from the protection scope of the present invention. Therefore, although the present invention has been described in more detail through the above embodiments, the present invention is not limited to the above embodiments. Without departing from the concept of the present invention, more other equivalent embodiments may be included, and the scope of the present invention is determined by the scope of the appended claims.
Claims
1. A task processing method, characterized in that, Including: When receiving a task processing request, establish a task execution thread for the task to be processed, and attach an expiration task duration corresponding to the task execution thread; wherein, the expiration task duration is the duration corresponding to ensuring that the task thread is not interfered by other threads through an expiration exclusive lock; Determine the supervision duration corresponding to the task execution thread; Based on the supervision duration and the expiration task duration, determine the execution result of the task to be processed; Based on the execution result, determine whether to release the task execution thread; The determining the execution result of the task to be processed based on the supervision duration and the expiration task duration includes: Store the task identifier, expiration task duration, thread identifier of the task execution thread, and supervision duration of the task to be processed into a record list, and traverse the record information in the record list; Based on the record list, determine whether the current supervision duration exceeds the supervision duration; If so, and the record table includes the task identifier, remove a set of data associated with the task identifier from the record table and send a warning message.
2. The method according to claim 1, characterized in that, The establishing a task execution thread for the task to be processed and attaching an expiration task duration corresponding to the task execution thread includes: Determine the expiration task duration according to the task type of the task to be processed; Establish the task execution thread for the task to be processed.
3. The method according to claim 1, characterized in that, The determining the supervision duration corresponding to the task execution thread includes: Determine the supervision duration corresponding to the task to be processed according to the task type of the task to be processed and a pre-established mapping relation table; or, Retrieve the pre-stored supervision duration as the supervision duration of the task to be processed.
4. The method according to claim 1, characterized in that, Also including: If not, and the record table includes the task identifier, extend the expiration task duration; If not, and the record table does not include the task identifier, remove a set of data associated with the task identifier from the record table and report task execution completion information.
5. The method according to claim 4, characterized in that, The extending the expiration task duration includes: Determine the extended expiration task duration according to the current supervision duration and the supervision duration.
6. A task processing device, characterized in that, Including: A thread establishment module, configured to establish a task execution thread for the task to be processed when receiving a task processing request, and attach an expiration task duration corresponding to the task execution thread; wherein, the expiration task duration is the duration corresponding to ensuring that the task thread is not interfered by other threads through an expiration exclusive lock; A supervision duration determination module, configured to determine the supervision duration corresponding to the task execution thread; An execution result determination module, configured to determine the execution result of the task to be processed based on the supervision duration and the expiration task duration; A thread release module, configured to determine whether to release the task execution thread based on the execution result; The execution result determination module includes: A storage sub-module, configured to store the task identifier, expiration task duration, thread identifier of the task execution thread, and supervision duration of the task to be processed into a record list, and traverse the record information in the record list; A supervision duration judgment sub-module, configured to determine whether the current supervision duration exceeds the supervision duration based on the record list; An early warning information sending sub-module, configured to, if so, and if the record table includes the task identifier, remove a set of data associated with the task identifier from the record table and send an early warning information.
7. The device according to claim 6, characterized in that, The supervision duration determination module includes: A supervision duration determination sub-module, configured to determine the supervision duration corresponding to the to-be-processed task according to the task type of the to-be-processed task and a pre-established mapping relation table; or A supervision duration retrieval sub-module, configured to retrieve a pre-stored supervision duration as the supervision duration of the to-be-processed task.
8. An electronic device, characterized in that, The electronic device includes: One or more processors; A storage device, configured to store 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 task processing method according to any one of claims 1-5.
9. A storage medium containing computer-executable instructions, characterized in that, The computer-executable instructions are used to execute the task processing method according to any one of claims 1-5 when executed by a computer processor.
Citation Information
Patent Citations
Thread maintenance method and device and electronic equipment
CN109697128A