A method and related device for preventing repeated consumption based on asynchronous tasks
By determining the target proportion based on the historical task allocation and keywords of the consumer instance in asynchronous tasks, creating batch numbers, and allowing multiple consumer instances to tag tasks, the system performance degradation caused by a large number of asynchronous tasks is solved, and the task update speed and system stability are improved.
Patent Information
- Application Number
- CN202210573796.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-05-25
- Publication Date
- 2025-12-30
- Estimated Expiration
- 2042-05-25
AI Technical Summary
When handling large batches of asynchronous tasks, existing technologies can easily lead to system performance degradation, database table locking and application blockage, and even application crashes, causing user dissatisfaction or complaints.
By determining the target ratio based on the historical task allocation of consumer instances and keywords, batch numbers are created to mark asynchronous tasks, and multiple consumer instances are allowed to mark tasks simultaneously, thus avoiding overloading of a single consumer instance.
It improves system performance, prevents asynchronous tasks from being consumed repeatedly, reduces database table locking and application blocking, and increases task update speed.
Smart Images

Figure CN114968575B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a method and related apparatus for preventing duplicate consumption based on asynchronous tasks. Background Technology
[0002] With the advancement of technology and economic development, data is growing explosively, potentially generating and processing massive amounts of data every day. This is especially true in industries like insurance and banking, which have large customer bases and business volumes, resulting in a large number of asynchronous tasks.
[0003] When handling asynchronous tasks, it's necessary to prevent duplicate submissions of the same task. Therefore, locking is required for asynchronous tasks. For example, when modifying data in a database, locking the data is used to prevent simultaneous modifications by other users. A sudden surge in the number of asynchronous tasks can significantly degrade system performance and easily lead to database table locking as more instances consume data. This problem can even cause application and database congestion, leading to application crashes and easily causing user frustration or complaints.
[0004] Therefore, how to solve the system performance problem when dealing with a large number of asynchronous tasks to prevent duplicate consumption is a hot topic of concern for us. Summary of the Invention
[0005] This application provides a method and related apparatus for preventing duplicate consumption based on asynchronous tasks, which can effectively improve system performance to a certain extent.
[0006] In a first aspect, embodiments of this application provide a method for preventing duplicate consumption based on asynchronous tasks, the method comprising:
[0007] The target ratio is determined based on the task allocation of multiple consumer instances when executing the first batch of tasks in history, and the number of tasks searched from the second batch of tasks based on the keywords of these multiple consumer instances.
[0008] Based on the target ratio, determine the target asynchronous task that each of the multiple consumer instances needs to execute, wherein the target asynchronous task belongs to the task in the second batch task;
[0009] Create batch numbers for the multiple consumer instances to mark the target asynchronous task of the multiple consumer instances. The batch numbers are different for different consumer instances. At least one asynchronous task in the second batch task is marked with two batch numbers.
[0010] An asynchronous task marked with the batch number of the first consumer instance is executed through the first consumer instance, where the first consumer instance is any one of the plurality of consumer instances.
[0011] In the aforementioned method, by referencing the historical execution of asynchronous tasks by each consumer instance and the availability of executable asynchronous tasks, and considering the limitation of directly processing a certain number of asynchronous tasks from a large batch when the exact asynchronous task to be retrieved cannot be precisely located, the number of consumer instances in this embodiment is more balanced. Furthermore, the same asynchronous task can be marked by multiple consumer instances. For example, consumer instance A does not need to mark all target asynchronous tasks before releasing them, allowing consumer instance B to mark them. This approach avoids blocking other consumer instances from marking tasks. Because multiple consumer instances are marking, the update speed for batch asynchronous tasks is also faster, significantly improving system performance.
[0012] Secondly, embodiments of this application provide an apparatus for preventing duplicate consumption based on asynchronous tasks, the apparatus comprising:
[0013] The first determining unit is used to determine the target ratio based on the task allocation of multiple consumer instances when executing the first batch of historical tasks, and the number of tasks searched from the second batch of tasks based on the keywords of multiple consumer instances.
[0014] The second determining unit is used to determine the target asynchronous task to be executed by each of the multiple consumer instances according to the target ratio, wherein the target asynchronous task belongs to the task in the second batch task;
[0015] The tagging unit is used to create batch numbers for the multiple consumer instances, thereby tagging the target asynchronous task of the multiple consumer instances. The batch numbers of different consumer instances are different, and at least one asynchronous task in the second batch task is tagged with two batch numbers.
[0016] An execution unit is used to execute an asynchronous task that is marked with the batch number of the first consumer instance, wherein the first consumer instance is any one of the plurality of consumer instances.
[0017] In the aforementioned method, by referencing the historical execution of asynchronous tasks by each consumer instance and the availability of executable asynchronous tasks, and considering the limitation of directly processing a certain number of asynchronous tasks from a large batch when the exact asynchronous task to be retrieved cannot be precisely located, the number of consumer instances in this embodiment is more balanced. Furthermore, the same asynchronous task can be marked by multiple consumer instances. For example, consumer instance A does not need to mark all target asynchronous tasks before releasing them, allowing consumer instance B to mark them. This approach avoids blocking other consumer instances from marking tasks. Because multiple consumer instances are marking, the update speed for batch asynchronous tasks is also faster, significantly improving system performance.
[0018] Thirdly, embodiments of this application provide an electronic device, which includes a processor and a memory. The memory stores a computer program, and the processor invokes the computer program to perform the following operations:
[0019] The target ratio is determined based on the task allocation of multiple consumer instances when executing the first batch of tasks in history, and the number of tasks searched from the second batch of tasks based on the keywords of multiple consumer instances.
[0020] Based on the target ratio, determine the target asynchronous task that each of the multiple consumer instances needs to execute, wherein the target asynchronous task belongs to the task in the second batch task;
[0021] Create batch numbers for the multiple consumer instances to mark the target asynchronous task of the multiple consumer instances. The batch numbers of different consumer instances are different. At least one asynchronous task in the second batch task is marked with two batch numbers.
[0022] Execute an asynchronous task that marks the batch number of the first consumer instance, where the first consumer instance is any one of the plurality of consumer instances.
[0023] In the aforementioned method, by referencing the historical execution of asynchronous tasks by each consumer instance and the availability of executable asynchronous tasks, and considering the limitation of directly processing a certain number of asynchronous tasks from a large batch when the exact asynchronous task to be retrieved cannot be precisely located, the number of consumer instances in this embodiment is more balanced. Furthermore, the same asynchronous task can be marked by multiple consumer instances. For example, consumer instance A does not need to mark all target asynchronous tasks before releasing them, allowing consumer instance B to mark them. This approach avoids blocking other consumer instances from marking tasks. Because multiple consumer instances are marking, the update speed for batch asynchronous tasks is also faster, significantly improving system performance.
[0024] Fourthly, embodiments of this application provide a computer-readable storage medium storing program instructions that, when executed on a processor, implement the method for preventing duplicate consumption based on asynchronous tasks provided in the first aspect. Attached Figure Description
[0025] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments of this application will be briefly described below.
[0026] Figure 1 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application;
[0027] Figure 2 This is a schematic flowchart of a method for preventing duplicate consumption based on asynchronous tasks provided in an embodiment of this application;
[0028] Figure 3 This is a schematic diagram illustrating the determination of a target ratio, as exemplified by an embodiment of this application.
[0029] Figure 4 This is a schematic diagram illustrating the marking of an asynchronous task as exemplified in an embodiment of this application;
[0030] Figure 5 This is a schematic diagram illustrating a message notifying the execution of an asynchronous task, as executed in an embodiment of this application.
[0031] Figure 6 This is a schematic diagram of a device for preventing duplicate consumption based on asynchronous tasks, provided in an embodiment of this application. Detailed Implementation
[0032] The technical solutions in the embodiments of this application will be described in more detail below with reference to the accompanying drawings.
[0033] See Figure 1 , Figure 1 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. The electronic device 100 includes a processor 101, a memory 102 and a communication interface 103, which are interconnected via a bus.
[0034] The memory 102 is a component used to store programs and various data information. The memory 102 includes, but is not limited to, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM), or compact disc read-only memory (CD-ROM). The memory 102 provides storage space for programs and data, and may include, but is not limited to, Android systems, iOS systems, Windows systems, etc. In this application, the memory 102 is used to store related instructions and data, such as asynchronous tasks that a consumer instance needs to execute, and notification messages sent by a consumer instance to other consumer instances.
[0035] Processor 101 is an execution unit for information processing and program execution. Processor 101 can be one or more central processing units (CPUs). When processor 101 is a CPU, the CPU can be a single-core CPU or a multi-core CPU. In the embodiments of this application, for example, processor 101 can call asynchronous tasks stored in memory 102 and assign them to multiple consumer instances; or, processor 101 can call asynchronous tasks stored in memory 102 and mark them.
[0036] The communication interface 103 is used to receive or send data. Optionally, the communication interface may include a wired communication interface or a wireless communication interface. In this embodiment, for example, it receives a notification message that an asynchronous task has been executed.
[0037] The electronic device 100 can be a single device or a server cluster consisting of multiple devices.
[0038] The methods provided in the embodiments of this application are described below.
[0039] Please see Figure 2 , Figure 2 This is a flowchart illustrating a method for preventing duplicate consumption based on asynchronous tasks, provided in an embodiment of this application. This method can be based on... Figure 1 The electronic device 100 shown can be implemented using other devices, and the method includes, but is not limited to, the following steps.
[0040] Step S201: The electronic device determines the target ratio based on the task allocation of multiple consumer instances when executing the first batch of historical tasks, and the number of tasks searched from the second batch of tasks based on the keywords of the multiple consumer instances.
[0041] In this context, "instance" refers to a thread. A thread is the smallest unit of computation that the operating system can schedule; it is contained within a process and is the actual unit of operation within that process. A thread refers to a single, sequential flow of control within a process. Multiple threads can run concurrently within a process, each executing different tasks in parallel.
[0042] In this embodiment, the task refers to an asynchronous task. Asynchronous means that after executing a function or method, there is no need to block and wait for a return value or message. Instead, an asynchronous process is delegated to the system. When the system receives the return value or message, it automatically triggers the delegated asynchronous process, thus completing a full flow.
[0043] Taking online insurance purchases as an example, one step in the process is sending an email. After purchasing insurance, the system needs to send an email to the user to notify them of the policy. If it's synchronous, after the user clicks the "buy" button, the system sends an email confirming the successful purchase, followed by a pop-up window displaying relevant insurance information. In this synchronous scenario, the system sends the email first, then the pop-up appears; this is a synchronous task. If it's asynchronous, the system first records the email request in a database table, then directly displays the pop-up, and only then sends the email. This is an asynchronous task. In simpler terms, an asynchronous task is one that doesn't execute immediately.
[0044] The first batch task refers to any one of the batch asynchronous tasks executed multiple times in the history by multiple consumer instances.
[0045] The second batch of asynchronous tasks refers to a batch of asynchronous tasks that need to be executed by multiple consumer instances.
[0046] To distinguish asynchronous tasks executed in the history of multiple consumer instances from the asynchronous tasks that need to be executed now, asynchronous tasks in the two cases are distinguished by different names.
[0047] Keywords for multiple consumer instances refer to key information about the asynchronous tasks that each consumer instance needs to execute. For example, multiple consumer instances may need to process a batch of asynchronous tasks related to insurance, such as pension insurance, travel insurance, and accident insurance. Suppose consumer instance A needs to process the asynchronous tasks related to pension insurance, and the keywords would be words related to pension insurance.
[0048] Optionally, asynchronous tasks can be searched from the second batch of tasks by using a computer to input keywords into a Structured Query Language (SQL) statement. SQL is a database language with various functions such as data manipulation and data definition, allowing access to and processing of the database, including data insertion, querying, updating, and deletion. However, there are no restrictions on the specific form of the search.
[0049] The target ratio is used to determine the target asynchronous task that each of the multiple consumer instances mentioned above needs to execute, wherein the target asynchronous task belongs to the tasks in the second batch of tasks mentioned above.
[0050] The target ratio can be determined at least in the following ways:
[0051] Method 1: Determine a first proportion based on the number of first tasks executed by the aforementioned multiple consumer instances in the historical first batch of tasks. This first proportion represents the percentage of asynchronous tasks executed by the aforementioned multiple consumer instances historically, and this first proportion serves as the target proportion. For example, if consumer instances A, B, and C executed 1000 asynchronous tasks in the historical first batch, with consumer instance A executing 300, consumer instance B executing 400, and consumer instance C executing 300, then the number of asynchronous tasks executed by each consumer instance A, B, and C is the aforementioned number of first tasks. If the proportion of asynchronous tasks executed by consumer instance A is 30%, the proportion executed by consumer instance B is 40%, and the proportion executed by consumer instance C is 30%, then the target proportion is 30% for each of these three consumer instances.
[0052] It is understandable that the first proportion of the number of first tasks in the first batch of asynchronous tasks executed by multiple consumer instances reflects the ability of multiple consumer instances to execute asynchronous tasks. Therefore, using this first proportion as a factor to determine the target proportion is meaningful.
[0053] Method Two: First, determine a first proportion based on the number of first tasks executed by the aforementioned multiple consumer instances in the first batch of historical tasks. This first proportion represents the percentage of asynchronous tasks executed by the aforementioned multiple consumer instances historically, and the number of first tasks is the number of asynchronous tasks executed by each of the multiple consumer instances. Taking the execution of 1000 tasks in the first batch of historical tasks by consumer instances A, B, and C as an example, if the percentage of asynchronous tasks executed by consumer instance A is 30%, the percentage executed by consumer instance B is 40%, and the percentage executed by consumer instance C is 30%, then the first proportion is calculated as follows: 30% for consumer instance A, 40% for consumer instance B, and 30% for consumer instance C.
[0054] Secondly, based on the keywords of each consumer instance among the multiple consumer instances, the corresponding tasks for each consumer instance are searched from the second batch of tasks to determine the number of second tasks corresponding to each consumer instance. The number of second tasks is the number of asynchronous tasks found by each consumer instance among the multiple consumer instances. For example, if consumer instances A, B, and C need to process 1000 second batch tasks, and consumer instance A finds 400 asynchronous tasks, consumer instance B finds 500, and consumer instance C finds 400, then the 400 found by consumer instance A, 500 by consumer instance B, and 400 by consumer instance C constitute the aforementioned number of second tasks.
[0055] Then, a second ratio is determined based on the number of second tasks corresponding to each consumer instance. This second ratio represents the estimated proportion of asynchronous tasks executed by the multiple consumer instances during the execution of the second batch of tasks. Taking the number of second tasks searched by consumer instances A, B, and C as an example, the estimated proportions of asynchronous tasks executed by consumer instances A, B, and C are 40%, 50%, and 40%, respectively. The estimated proportions of consumer instance A (40%), consumer instance B (50%), and consumer instance C (40%) constitute the aforementioned second ratio.
[0056] Finally, the target ratio is determined based on the first and second ratios mentioned above. For example... Figure 3 As shown, Figure 3 This example illustrates how to determine a target proportion. Assume that the first proportion for consumption instance A is 30%, the first proportion for consumption instance B is 40%, and the first proportion for consumption instance C is 30%; the second proportion for consumption instance A is 40%, the second proportion for consumption instance B is 50%, and the second proportion for consumption instance C is 40%. The target proportion can be determined by comprehensively considering the proportion parameters of each consumption instance. For example, the target proportion for consumption instance A is (30% + 40%) / 2 = 35%, the target proportion for consumption instance B is (40% + 50%) / 2 = 45%, and the target proportion for consumption instance C is (30% + 40%) / 2 = 35%.
[0057] Understandably, to avoid an imbalance in the overall distribution of asynchronous tasks due to too many or too few asynchronous tasks executed by a particular consumer instance, this application specifically uses the historical execution of asynchronous tasks by each consumer instance and the available asynchronous tasks queried by each consumer instance as references. The historical execution of asynchronous tasks can reflect the business execution capabilities of each consumer instance to a certain extent, while the available asynchronous tasks queried can reflect the execution requirements of asynchronous tasks to a certain extent. Therefore, after comprehensively considering these two aspects, the number of asynchronous tasks determined for each consumer instance is more balanced.
[0058] Step S202: The electronic device determines the target asynchronous task to be executed for each of the multiple consumer instances based on the target ratio.
[0059] This target asynchronous task belongs to the second batch of tasks mentioned above. The target asynchronous task is the asynchronous task that each of the multiple consumer instances ultimately needs to process.
[0060] Specifically, the electronic device adjusts the asynchronous tasks searched for in each of the multiple consumer instances to obtain the target asynchronous tasks corresponding to each consumer instance, such that the ratio of the number of target asynchronous tasks corresponding to each consumer instance is the aforementioned target ratio.
[0061] For example, consumer instances A, B, and C need to process 1000 second batch tasks. Consumer instance A finds 400 tasks in the second batch, consumer instance B finds 600 tasks, and consumer instance C finds 300 tasks. The target ratio for consumer instance A is 35%, the target ratio for consumer instance B is 45%, and the target ratio for consumer instance C is 35%. Based on the target ratio of 35% for consumer instance A, the electronic device adjusts the number of asynchronous tasks found in the search to 350. Consumer instances B and C adjust the number of asynchronous tasks found in the search in the same way. The adjusted asynchronous tasks are the target asynchronous tasks mentioned above.
[0062] Understandably, adjusting the number of executable asynchronous tasks obtained from querying each consumer instance across multiple consumer instances according to the target ratio results in a more balanced number of asynchronous tasks.
[0063] Step S203: The electronic device creates a batch number for the multiple consumer instances to mark the target asynchronous task of the multiple consumer instances.
[0064] Different consumer instances have different batch numbers. Batch numbers are created to mark asynchronous tasks, allowing each consumer instance to obtain an asynchronous task based on its corresponding batch number.
[0065] In the second batch of tasks mentioned above, at least one asynchronous task is marked with both batch numbers. For example, in conventional technology, if an asynchronous task is marked by consumer instance A, consumer instance B will not mark that asynchronous task again. In this embodiment, consumer instance A marks an asynchronous task, and at the same time, consumer instance B can also mark that asynchronous task. Figure 4 As shown, Figure 4 The example illustrates an asynchronous task labeling process, where consumer instance A labels asynchronous tasks numbered 1-6 as a1 to a6, while consumer instance B labels asynchronous tasks numbered 5-10 as b1 to b6.
[0066] When consumer instance A marks an asynchronous task, it does not prevent consumer instance B from marking it at the same time. Suppose that an asynchronous task in consumer instance A is marked as 'a' and is also marked as 'b' by consumer instance B.
[0067] Understandably, consumer instance A doesn't need to mark all targets asynchronously before releasing them so that consumer instance B can start marking them. This approach avoids blocking other consumer instances from marking. Furthermore, since multiple consumer instances are marking, the update speed for batch asynchronous tasks is also faster.
[0068] Furthermore, in this embodiment of the application, a batch number is created and the target asynchronous task is obtained by adjusting the target asynchronous task obtained from the batch of asynchronous tasks searched; instead of creating a batch number and obtaining a certain number of asynchronous tasks directly from a large batch of asynchronous tasks because the asynchronous task to be obtained cannot be accurately located.
[0069] For example, if there are one million asynchronous tasks in a database table, and assuming that consumer instance A ultimately executes 1,000 data items, from a traditional technology perspective, consumer instance A may not be able to determine which asynchronous tasks it needs to process, and may mark 10,000 of them first. In this embodiment of the application, the target asynchronous tasks determined according to the target ratio may be 1,500. In this case, only these 1,500 asynchronous tasks need to be marked, which greatly reduces the amount of data to be marked.
[0070] Understandably, if you need to retrieve 1,000 data points from a million asynchronous tasks, but can't precisely pinpoint those 1,000 tasks, you might initially mark all 10,000 tasks. Only after marking them would you determine that the 1,000 tasks actually require the data, but you've already marked those 10,000. However, by identifying the target asynchronous tasks based on a target proportion, you could mark only 1,500 of them. This would reduce the need to mark large amounts of data, preventing a significant drop in computer system performance.
[0071] Optionally, the electronic device obtains the index of the target asynchronous task, and uses this index to create a batch number for each of the multiple consumer instances, thereby marking the target asynchronous task across the multiple consumer instances. There are no restrictions on the specific form in which the batch number is created.
[0072] In relational databases, an index is a separate, physical storage structure that sorts the values of one or more columns in a database table. It's a collection of values from one or more columns and a list of logical pointers to the data pages in the table that physically identify those values. An index is analogous to a book's table of contents, allowing you to quickly find the desired content based on page numbers. In simpler terms, an index refers to the identity document (ID) corresponding to an asynchronous task, where each asynchronous task has a unique ID. Optionally, the index for an asynchronous task can be obtained by writing SQL statements using a computer.
[0073] Step S204: The electronic device executes an asynchronous task marked with the batch number of the first consumer instance through the first consumer instance, wherein the first consumer instance is any one of the plurality of consumer instances.
[0074] Specifically, the electronic device executes an asynchronous task marked with the batch number of the first consumer instance through the first consumer instance, including at least two cases:
[0075] In scenario one, the electronic device determines whether the first asynchronous task, which marks the batch number of the first consumer instance, has been executed by a consumer instance other than the first consumer instance through the first consumer instance; if the first asynchronous task has not been executed by a consumer instance other than the first consumer instance, then the first asynchronous task is executed.
[0076] The first asynchronous task is any asynchronous task that marks the batch number of the first consumption instance.
[0077] Some of the asynchronous tasks in the first consumer instance may have been executed, so each asynchronous task needs to be checked before execution.
[0078] Taking sending emails when a user purchases insurance as an example, after consumer instance A receives the emails it has marked, assuming there are 150 emails, it first checks if the first email has been executed by any other consumer instance. If it has, it ignores the first email. Next, it checks if the second email has been executed by any other consumer instance. If it hasn't, it sends the second email. This process continues for the remaining 148 emails. Similarly, other consumer instances operate in the same way.
[0079] It's understandable that when users are purchasing insurance, receiving multiple emails confirming successful policy purchases when a large volume of emails needs to be sent can create a negative user experience and waste computer resources.
[0080] In one alternative approach, if the first asynchronous task has been executed by a consumer instance other than the first consumer instance, a first notification message is sent from the first consumer instance to the consumer instance other than the first consumer instance, wherein the first notification message is used to notify that the first asynchronous task has been executed.
[0081] For example, consumer instances A, B, and C need to execute a batch of asynchronous tasks. Consumer instance A has already executed asynchronous tasks numbered 1 to 150. Consumer instance B is currently processing asynchronous tasks numbered 148 to 400 one by one. After determining that asynchronous task numbered 148 has been executed, consumer instance B sends a notification message to the other consumer instances, informing them that asynchronous task numbered 148 has been executed. Then, it performs the same operation on asynchronous tasks numbered 149 and 150, and so on. Figure 5 As shown, Figure 5 This example illustrates a message notifying the execution of asynchronous tasks. Therefore, consumer instance C can directly ignore asynchronous tasks numbered 148, 149, and 150.
[0082] Understandably, the above consumption instance C does not need to judge the asynchronous tasks numbered 148, 149, and 150. It can directly ignore the asynchronous tasks numbered 148, 149, and 150. Therefore, in the case of a large number of asynchronous tasks, the overhead of judging whether the asynchronous tasks have been executed can be reduced.
[0083] In scenario two, the electronic device performs an overall assessment of the asynchronous tasks marked with the batch number of the first consumer instance through the first consumer instance, and determines whether the asynchronous tasks with the batch number of the first consumer instance have been executed by a second asynchronous task by a consumer instance other than the first consumer instance; if the second asynchronous task has not been executed by a consumer instance other than the first consumer instance, then the second asynchronous task is executed.
[0084] The second asynchronous task is any asynchronous task that is marked with the batch number of the first consumption instance.
[0085] Taking sending emails by a consumer instance as an example, after receiving the emails that it has marked, consumer instance A, assuming there are 150 emails, first checks all 150 emails, and then sends out the emails that have not been processed.
[0086] In one alternative approach, if the second asynchronous task has been executed by a consumer instance other than the first consumer instance, then the first consumer instance sends a second notification message to the consumer instance other than the consumer instance that executed the second asynchronous task, wherein the second notification message is used to notify that the second asynchronous task has been executed.
[0087] For example, consumer instances A, B, and C need to execute a batch of asynchronous tasks. Consumer instance A has already executed asynchronous tasks numbered 1 to 150. Consumer instance B is currently processing asynchronous tasks numbered 148 to 400. Consumer instance B first performs a comprehensive check on all the asynchronous tasks it receives and finds that asynchronous tasks numbered 148, 149, and 150 have already been executed. Consumer instance B then sends a notification to the other consumer instances, informing them that asynchronous tasks numbered 148, 149, and 150 have been executed. Therefore, consumer instance C can directly ignore asynchronous tasks numbered 148, 149, and 150.
[0088] The methods of the embodiments of this application have been described in detail above. The apparatus of the embodiments of this application is provided below.
[0089] Please see Figure 6 , Figure 6 This is a schematic diagram of the structure of a device 600 for preventing duplicate consumption based on asynchronous tasks provided in this application embodiment. The device 600 for preventing duplicate consumption can be the above-mentioned electronic device or a device or functional module in the electronic device. The device 600 may include a first determining unit 601, a second determining unit 602, a marking unit 603 and an execution unit 604, wherein each unit is described in detail below.
[0090] The first determining unit 601 is used to determine the target ratio based on the task allocation of multiple consumer instances when executing the historical first batch of tasks, and the number of tasks searched from the second batch of tasks based on the keywords of multiple consumer instances.
[0091] The second determining unit 602 is used to determine the target asynchronous task to be executed by each of the multiple consumer instances according to the target ratio, wherein the target asynchronous task belongs to the task in the second batch task;
[0092] The marking unit 603 is used to create batch numbers for the multiple consumer instances, thereby marking the target asynchronous task of the multiple consumer instances. The batch numbers of different consumer instances are different, and at least one asynchronous task in the second batch task is marked with two batch numbers.
[0093] Execution unit 604 is used to execute an asynchronous task that marks the batch number of the first consumer instance, wherein the first consumer instance is any one of the plurality of consumer instances.
[0094] In the aforementioned method, by referencing the historical execution of asynchronous tasks by each consumer instance and the availability of executable asynchronous tasks, and considering the limitation of directly processing a certain number of asynchronous tasks from a large batch when the exact asynchronous task to be retrieved cannot be precisely located, the number of consumer instances in this embodiment is more balanced. Furthermore, the same asynchronous task can be marked by multiple consumer instances. For example, consumer instance A does not need to mark all target asynchronous tasks before releasing them, allowing consumer instance B to mark them. This approach avoids blocking other consumer instances from marking tasks. Because multiple consumer instances are marking, the update speed for batch asynchronous tasks is also faster, significantly improving system performance.
[0095] In one alternative approach, the first determining unit 601 is specifically used to determine the target ratio based on the task allocation of multiple consumer instances during the execution of the first batch of historical tasks, and the number of tasks searched from the second batch of tasks based on the keywords of the multiple consumer instances.
[0096] A first proportion is determined based on the number of first tasks executed by multiple consumer instances in the first batch of historical tasks. This first proportion represents the percentage of asynchronous tasks executed by these multiple consumer instances in history.
[0097] Based on the keywords of each consumption instance in the multiple consumption instances, search for the corresponding task of each consumption instance from the second batch of tasks to determine the number of second tasks corresponding to each consumption instance;
[0098] A second ratio is determined based on the number of second tasks corresponding to each consumer instance, wherein the second ratio represents the estimated proportion of asynchronous tasks executed by the multiple consumer instances during the execution of the second batch of tasks;
[0099] The target ratio is determined based on the first ratio and the second ratio.
[0100] Understandably, to avoid an imbalance in the overall distribution of asynchronous tasks due to too many or too few asynchronous tasks executed by a particular consumer instance, this application specifically uses the historical execution of asynchronous tasks by each consumer instance and the available asynchronous tasks queried by each consumer instance as references. The historical execution of asynchronous tasks can reflect the business execution capabilities of each consumer instance to a certain extent, while the available asynchronous tasks queried can reflect the execution requirements of asynchronous tasks to a certain extent. Therefore, after comprehensively considering these two aspects, the number of asynchronous tasks determined for each consumer instance is more balanced.
[0101] In one alternative approach, the second determining unit 602 is specifically used to: determine the target asynchronous task to be performed by each of the multiple consumer instances based on the target proportion;
[0102] The asynchronous tasks obtained for each consumer instance are adjusted to obtain the target asynchronous tasks corresponding to each consumer instance, such that the ratio of the number of target asynchronous tasks corresponding to each consumer instance is the target ratio.
[0103] In an alternative approach, the execution unit 604 is specifically used for: executing the asynchronous task that marks the batch number of the first consumption instance;
[0104] Determine whether the first asynchronous task marked with the batch number of the first consumer instance has been executed by a consumer instance other than the first consumer instance. The first asynchronous task can be any asynchronous task marked with the batch number of the first consumer instance.
[0105] If the first asynchronous task is not executed by a consumer instance other than the first consumer instance, then the first asynchronous task is executed.
[0106] For example, when a user is purchasing insurance, if they receive multiple emails confirming the purchase success when a large number of emails need to be sent, it will create a poor user experience and waste computer resources. Understandably, before executing an asynchronous task through the first consumer instance, the electronic device first checks whether the marked asynchronous task is to be executed by another consumer instance besides the first one. This effectively avoids the same asynchronous task being executed multiple times and, to a certain extent, avoids the waste of system resources from executing multiple asynchronous tasks.
[0107] In an alternative embodiment, the aforementioned anti-double consumption device 600 further includes:
[0108] The notification unit is configured to send a first notification message to a consumer instance other than the first consumer instance when the first asynchronous task has been executed by the first consumer instance, wherein the first notification message is used to notify that the first asynchronous task has been executed.
[0109] It is understandable that by sending a notification message to a consumer instance other than the first consumer instance, the consumer instance other than the first consumer instance can ignore the asynchronous task that has been executed based on the received notification message, without having to check the asynchronous task again. In the case of a large number of asynchronous tasks, this can reduce the overhead of checking whether the asynchronous task has been executed.
[0110] In an alternative approach, the execution unit 604 is specifically used for: executing the asynchronous task that marks the batch number of the first consumption instance;
[0111] Perform an overall judgment on the asynchronous tasks marked with the batch number of the first consumer instance, and determine whether there is a second asynchronous task in the asynchronous tasks of the batch number of the first consumer instance that has been executed by a consumer instance other than the first consumer instance. The second asynchronous task is any asynchronous task marked with the batch number of the first consumer instance.
[0112] If the second asynchronous task is not executed by a consumer instance other than the first consumer instance, then the second asynchronous task is executed.
[0113] In an alternative embodiment, the notification unit is further used for:
[0114] If the second asynchronous task has been executed by a consumer instance other than the first consumer instance, a second notification message is sent from the first consumer instance to the consumer instance other than the consumer instance that executed the second asynchronous task. The second notification message is used to notify that the second asynchronous task has been executed.
[0115] This application also provides a computer-readable storage medium that, when storing computer instructions, executes them on a processor to achieve the following: Figure 2 The method flow is shown.
[0116] It should be noted that, for the sake of simplicity, the foregoing method embodiments are all described as a series of actions. However, those skilled in the art should understand that this application is not limited to the described order of actions, as some steps may be performed in other orders or simultaneously according to this application. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are all optional embodiments, and the actions and modules involved are not necessarily essential to this application.
[0117] In the above embodiments, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions in other embodiments.
[0118] In the several embodiments provided in this application, it should be understood that the disclosed apparatus can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the displayed or discussed mutual couplings, direct couplings, or communication connections may be through some interfaces; indirect couplings or communication connections between devices or units may be electrical or other forms.
[0119] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0120] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software program module.
[0121] If the integrated unit is implemented as a software program module and sold or used as an independent product, it can be stored in a computer-readable storage device (CMD). Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a memory and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned memory includes various media capable of storing program code, such as USB flash drives, read-only memory (ROM), random access memory (RAM), portable hard drives, magnetic disks, or optical disks.
[0122] Those skilled in the art will understand that all or part of the steps in the various methods of the above embodiments can be implemented by a program instructing related hardware. The program can be stored in a computer-readable storage medium, which may include: flash drive, read-only memory (ROM), random access memory (RAM), disk or optical disk, etc.
[0123] The embodiments of this application have been described in detail above. Specific examples have been used to illustrate the principles and implementation methods of this application. The description of the above embodiments is only for the purpose of helping to understand the method and core ideas of this application. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of this application. Therefore, the content of this specification should not be construed as a limitation of this application.
Claims
1. A method for duplicate consumption prevention based on asynchronous tasks, characterized in that, The method is applied to an electronic device, and the method comprises: determining a first proportion according to a number of first tasks in a first batch of tasks executed by a plurality of consumer instances in a history, the first proportion representing a proportion of asynchronous tasks executed by the plurality of consumer instances in the history; searching for tasks corresponding to each of the plurality of consumer instances from a second batch of tasks according to a keyword of the each of the plurality of consumer instances, to determine a second number of tasks corresponding to the each of the plurality of consumer instances; determining a second proportion according to the second number of tasks corresponding to the each of the plurality of consumer instances, wherein the second proportion represents a proportion of asynchronous tasks estimated to be executed by the plurality of consumer instances in a process of executing the second batch of tasks; determining a target proportion according to the first proportion and the second proportion; adjusting the asynchronous tasks searched for the each of the plurality of consumer instances to obtain target asynchronous tasks corresponding to the each of the plurality of consumer instances, so that a number proportion of the target asynchronous tasks corresponding to the each of the plurality of consumer instances is the target proportion, and the target asynchronous tasks belong to tasks in the second batch of tasks; creating batch numbers for the plurality of consumer instances, to mark the target asynchronous tasks of the plurality of consumer instances, wherein batch numbers of different consumer instances are different, and at least one asynchronous task in the second batch of tasks is marked with two batch numbers; executing, by a first consumer instance, an asynchronous task marked with a batch number of the first consumer instance, wherein the first consumer instance is any one of the plurality of consumer instances.
2. The method of claim 1, wherein, The executing, by the first consumer instance, the asynchronous task marked with the batch number of the first consumer instance comprises: determining, by the first consumer instance, whether a first asynchronous task marked with the batch number of the first consumer instance has been executed by a consumer instance other than the first consumer instance, the first asynchronous task being any one of the asynchronous tasks marked with the batch number of the first consumer instance; if the first asynchronous task has not been executed by the consumer instance other than the first consumer instance, executing the first asynchronous task.
3. The method of claim 2, wherein, The method further comprises: if the first asynchronous task has been executed by the consumer instance other than the first consumer instance, sending, by the first consumer instance, a notification message to the consumer instance other than the first consumer instance, wherein the notification message is used to notify that the first asynchronous task has been executed.
4. The method of claim 1, wherein, The executing, by the first consumer instance, the asynchronous task marked with the batch number of the first consumer instance comprises: determining, by the first consumer instance, whether a second asynchronous task marked with the batch number of the first consumer instance exists in the asynchronous tasks marked with the batch number of the first consumer instance, the second asynchronous task being any one of the asynchronous tasks marked with the batch number of the first consumer instance; if the second asynchronous task has not been executed by the consumer instance other than the first consumer instance, executing the second asynchronous task.
5. The method of claim 4, wherein, The method further comprises: If the second asynchronous task has been executed by a consumer instance other than the first consumer instance, a second notification message is sent by the first consumer instance to a consumer instance other than the consumer instance that executed the second asynchronous task, wherein the second notification message is used to notify that the second asynchronous task has been executed.
6. An apparatus for duplicate consumption prevention based on asynchronous tasks, the apparatus comprising: The apparatus comprises: The first determining unit is configured to determine a first proportion according to a first number of tasks executed by a plurality of consumer instances in a first batch of tasks in an execution history, the first proportion representing a proportion of asynchronous tasks executed by the plurality of consumer instances in the history; search for a second number of tasks corresponding to each consumer instance in the plurality of consumer instances from a second batch of tasks according to a keyword of the each consumer instance; determine a second proportion according to the second number of tasks corresponding to the each consumer instance, wherein the second proportion represents a proportion of asynchronous tasks estimated to be executed by the plurality of consumer instances in executing the second batch of tasks; and determine a target proportion according to the first proportion and the second proportion; The second determining unit is configured to adjust the asynchronous tasks searched for the each consumer instance to obtain target asynchronous tasks corresponding to the each consumer instance, so that a number proportion of the target asynchronous tasks corresponding to the each consumer instance is the target proportion, wherein the target asynchronous tasks belong to tasks in the second batch of tasks; The marking unit is configured to create batch numbers for the plurality of consumer instances, and mark the target asynchronous tasks of the plurality of consumer instances according to the batch numbers, wherein the batch numbers of different consumer instances are different, and at least one asynchronous task in the second batch of tasks is marked with two batch numbers; The execution unit is configured to execute an asynchronous task marked with a batch number of a first consumer instance by the first consumer instance, wherein the first consumer instance is any one of the plurality of consumer instances.
7. An electronic device, comprising: The device comprises a processor, a memory, and a computer program stored in the memory and executable on the processor, and the processor executes the computer program to implement the method for anti-duplicate consumption based on asynchronous tasks according to any one of claims 1-5.
8. A computer-readable storage medium, characterized in that, The computer readable storage medium stores a computer program, and when the computer program is executed on the processor, the method for anti-duplicate consumption based on asynchronous tasks according to any one of claims 1-5 is implemented.
Citation Information
Patent Citations
Task scheduling method, device, platform, server and storage medium
CN111625331A
Resource scheduling method and device
CN112035228A