Processing method and device for computing tasks

By caching and processing computational tasks in an ordered manner in a circular queue, thread safety issues are resolved, ensuring the correct execution of computational tasks.

CN115981891BActive Publication Date: 2025-10-24ZHEJIANG DAHUA TECH CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202211732592.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-12-30
Publication Date
2025-10-24
Estimated Expiration
2042-12-30

AI Technical Summary

Technical Problem

Existing technologies cannot guarantee thread safety for computational tasks, which can lead to errors in the results of thread processing of computational tasks.

Method used

By caching computation tasks in a circular queue, the first thread adds the computation task to the end of the circular queue's cache and sends it to the consumer thread for processing at the start of the consumption process. Each consumer thread in the consumer thread queue processes the computation tasks sequentially, preventing the same computation task from being acquired by multiple consumer threads.

Benefits of technology

It enables ordered processing of computational tasks, improves thread safety, and ensures the correctness and reliability of computational tasks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115981891B_ABST
    Figure CN115981891B_ABST
Patent Text Reader

Abstract

The embodiment of the present application provides a kind of processing method and device of computing task, wherein, the method comprises: by first thread, first computing task is added to first ring queue in first time;In the case where the target request of first consumer thread in consumer thread queue is received and the consumption starting point of the first ring queue is the cache starting point of the first computing task in the first ring queue, the first computing task is sent to the first consumer thread, to process the first computing task by the first consumer thread;The consumption starting point of the first ring queue is updated to the cache end point of the first computing task in the first ring queue.By the present application, the problem that thread safety cannot be guaranteed in processing computing task in the related art is solved, and the effect of improving thread safety in processing computing task is achieved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] Embodiments of the present application relate to the field of computer, in particular, to a method and apparatus for processing a computing task. BACKGROUND

[0002] To improve data processing efficiency, a distributed storage system generally adopts a multi-thread mode, divides a data processing task to be processed into a plurality of computing tasks, allocates the plurality of computing tasks to threads participating in computation, and implements parallel processing by simultaneously processing the allocated computing tasks by the plurality of threads, and finally integrates the results of processing each computing task to obtain a result of data processing. However, when allocating computing tasks to multiple threads, there are a plurality of possibilities, and some of the possibilities can cause thread insecurity, which is that the result of processing a computing task by a thread is incorrect, for example, when allocating computing tasks, the same computing task is allocated to two threads, and processing the same computing task by the two threads can cause the result output by the computing task to be incorrect, or the order of scheduling computing tasks is different, which causes the result of data processing to be incorrect. Therefore, there is a problem in the prior art that thread safety of processing a computing task cannot be guaranteed.

[0003] In view of the problem in the related art that thread safety of processing a computing task cannot be guaranteed, no effective solution has been proposed so far. SUMMARY

[0004] Embodiments of the present application provide a method and apparatus for processing a computing task to at least solve the problem in the related art that thread safety of processing a computing task cannot be guaranteed.

[0005] According to an embodiment of the present application, a method for processing a computing task is provided, comprising: adding, by a first thread, a first computing task to a first circular queue at a first time, wherein the first circular queue is used to buffer a computing task to be processed, and a buffering start point of the first computing task in the first circular queue is a buffering end point of the first circular queue, and before the first computing task is added to the first circular queue, the buffering end point of the first circular queue is a buffering end point of a second computing task in the first circular queue, and the second computing task is a computing task preceding the first computing task and added to the first circular queue;

[0006] send the first computing task to the first consumer thread for processing the first computing task by the first consumer thread, in a case that a target request of a first consumer thread in a consumer thread queue is received and a consumption start point of the first ring queue is a cache start point of the first computing task in the first ring queue, wherein each of the consumer threads in the consumer thread queue is configured to process a computing task to be processed in the first ring queue after receiving the computing task to be processed in the first ring queue;

[0007] update the consumption start point of the first ring queue to a cache end point of the first computing task in the first ring queue.

[0008] According to still another embodiment of the present application, there is also provided a processing apparatus of computing tasks, comprising: an adding module configured to add a first computing task to a first ring queue by a first thread at a first time, wherein the first ring queue is configured to cache computing tasks to be processed, a cache start point of the first computing task in the first ring queue is a cache end point of the first ring queue, and the cache end point of the first ring queue is a cache end point of a second computing task in the first ring queue before the first computing task is added to the first ring queue, the second computing task being a previous computing task added to the first ring queue than the first computing task;

[0009] a sending module configured to send the first computing task to the first consumer thread for processing the first computing task by the first consumer thread, in a case that a target request of a first consumer thread in a consumer thread queue is received and a consumption start point of the first ring queue is a cache start point of the first computing task in the first ring queue, wherein each of the consumer threads in the consumer thread queue is configured to process a computing task to be processed in the first ring queue after receiving the computing task to be processed in the first ring queue;

[0010] an updating module configured to update the consumption start point of the first ring queue to a cache end point of the first computing task in the first ring queue.

[0011] According to still another embodiment of the present application, there is also provided a computer readable storage medium, wherein a computer program is stored in the computer readable storage medium, and the computer program is configured to perform the steps in any of the method embodiments when executed.

[0012] According to still another embodiment of the present application, there is also provided an electronic device, comprising a memory and a processor, wherein a computer program is stored in the memory, and the processor is configured to execute the computer program to perform the steps in any of the method embodiments.

[0013] By the present application, by adding the to-be-processed computing task to the first ring queue at the cache end point of the ring queue, caching the to-be-processed computing task in the first ring queue, sending the computing task corresponding to the consumption start point to the consumption thread in the consumption thread queue, processing by the consumption thread, the to-be-processed computing task is sequentially added to the first ring queue and cached in the first queue, and the consumption thread of the consumption thread queue can also sequentially obtain the computing task from the first ring queue for processing, so that the consumption thread can sequentially process the computing task, and the same computing task cannot be obtained by multiple consumption threads. Therefore, the problem that the thread safety of processing the computing task cannot be guaranteed in the related art is solved, and the effect of improving the thread safety of processing the computing task is achieved. BRIEF DESCRIPTION OF DRAWINGS

[0014] Figure 1 is a mobile terminal hardware structure block diagram of the computing task processing method according to the embodiment of the present application.

[0015] Figure 2 is a flowchart of the computing task processing method according to the embodiment of the present application.

[0016] Figure 3 is a schematic diagram of the ring queue in the embodiment of the present application.

[0017] Figure 4 is a schematic diagram of the ring queue in the embodiment of the present application.

[0018] Figure 5 is a schematic diagram of the ring queue shrinkage and expansion according to the specific embodiment of the present application.

[0019] Figure 6 is a structure block diagram of the computing task processing device according to the embodiment of the present application. DETAILED DESCRIPTION

[0020] Hereinafter, the embodiments of the present application will be described in detail with reference to the accompanying drawings and in conjunction with the embodiments.

[0021] It should be noted that the terms "first", "second" and the like in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and do not necessarily have to describe a specific order or sequence.

[0022] The method embodiments provided in the embodiments of the present application can be executed in a mobile terminal, a computer terminal or a similar computing device. Taking the case of running on a mobile terminal, Figure 1 is a mobile terminal hardware structure block diagram of the computing task processing method according to the embodiment of the present application. As shown in Figure 1 , the mobile terminal can include one or more (Figure 1 The mobile terminal can further include a transmission device 106 for communication function and an input / output device 108. Those skilled in the art can understand that, Figure 1 The structure shown is only schematic, and does not limit the structure of the mobile terminal. For example, the mobile terminal can include more or less components than those shown, or have a different configuration of components. Figure 1 The mobile terminal shown in the figure can include more or less components than those shown, or have a different configuration of components. Figure 1 The mobile terminal shown in the figure can include more or less components than those shown, or have a different configuration of components.

[0023] The memory 104 can be used to store computer programs, such as software programs of application software and modules, such as the computer program corresponding to the processing method of the computing task in the embodiments of the present application. The processor 102 executes various functional applications and data processing by running the computer programs stored in the memory 104, that is, implements the above-mentioned method. The memory 104 can include a high-speed random access memory, and can also include a non-volatile memory, such as one or more magnetic storage devices, flash memories, or other non-volatile solid-state memories. In some examples, the memory 104 can further include a memory remotely arranged with respect to the processor 102, which can be connected to the mobile terminal through a network. Examples of the above-mentioned network include but are not limited to the Internet, an intranet, a local area network, a mobile communication network, and a combination thereof.

[0024] The transmission device 106 is used to receive or send data via a network. Specific examples of the above-mentioned network can include a wireless network provided by a communication provider of the mobile terminal. In one example, the transmission device 106 includes a network adapter (Network Interface Controller, NIC), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the transmission device 106 can be a radio frequency (Radio Frequency, RF) module, which is used to communicate with the Internet in a wireless manner.

[0025] In the embodiments, a processing method of a computing task is provided, Figure 2 is a flowchart of the processing method of the computing task according to the embodiments of the present application, as shown in the figure, the flow includes the following steps: Figure 2

[0026] ​Step S202, adding a first computing task to a first ring queue at a first time point by a first thread, wherein the first ring queue is used to buffer computing tasks to be processed, a buffer start point of the first computing task in the first ring queue is a buffer end point of the first ring queue, and the buffer end point of the first ring queue is a buffer end point of a second computing task in the first ring queue before the first computing task is added to the first ring queue, the second computing task being a computing task preceding the first computing task and added to the first ring queue;

[0027] Step S204, sending the first computing task to a first consumer thread in the first consumer thread queue to process the first computing task by the first consumer thread, in a case that a target request of the first consumer thread in the consumer thread queue is received and a consumption start point of the first ring queue is a buffer start point of the first computing task in the first ring queue, wherein each consumer thread in the consumer thread queue is used to process a computing task to be processed in the first ring queue after the computing task to be processed is received.

[0028] Step S206, updating the consumption start point of the first ring queue to a buffer end point of the first computing task in the first ring queue.

[0029] In the embodiment, a computing task refers to a task that needs to be processed by a computer, and it is to be noted that the computing task is not only a numerical calculation, but also a general term of all tasks processed by a computer.

[0030] A ring queue is constructed, the ring queue is used to buffer computing tasks to be processed in sequence, a request of receiving and processing a computing task is acquired by a first thread, a corresponding computing task is buffered by the first thread in the computing tasks to be processed, and when the computing tasks to be processed are buffered in the ring queue, a thread in a consumer thread queue acquires a computing task to be processed from the ring queue, thereby processing the computing task.

[0031] In processing the first computing task, the first computing task is first added to a first ring queue for buffering computing tasks to be processed by the first thread, and a consumer thread in a consumer thread queue processes the first computing task.

[0032] The consumption start point of the ring queue and the cache end point of the ring queue are marked in the ring queue, and the cache start point of each computing task cached in the ring queue and the cache end point of each computing task are marked, wherein the cache start point of the computing task indicates that the corresponding computing task is cached from the position; the cache end point of the computing task indicates that the corresponding computing task is cached at the position; the consumption start point of the ring queue is the position from which the computing task to be processed is obtained when a consumption thread requests to obtain the computing task to be processed; and the cache end point of the ring queue indicates the cache end point of the last computing task added to the ring queue, Figure 3 is a schematic diagram of the ring queue according to an embodiment of the present application, as Figure 3 shown, Figure 3 (a) in (a) is the ring queue in an initial state, at this time, the computing task to be processed is not cached in the ring queue, and each number in (a) indicates a different position in the ring queue; in the initial state, the consumption start point and the cache end point of the ring queue are the same, corresponding to position 0 in (b), Figure 3 from which the computing task is stored, Figure 3 (b) in (b) is the ring queue in which two computing tasks are cached, including a computing task (1) and a computing task (2); the cache start point of the computing task (1) is position 0, the cache end point of the computing task (1) is position 1, the cache start point of the computing task (2) is position 1, and the cache end point of the computing task (2) is position 3.

[0033] It should be noted that the cache start point of each computing task and the cache end point of each computing task are constant and are determined when the computing task is cached; the consumption start point of the ring queue and the cache end point of the ring queue are changed and updated; the consumption start point of the ring queue is updated when the computing task is obtained by the consumption thread, and the cache end point of the ring queue is updated when a new computing task is added, for example, Figure 4 is a change schematic diagram of the ring queue according to an embodiment of the present application, in Figure 3 (b) in (b), the computing task (1) is sent to the consumption thread, the computing task (1) is processed by the consumption thread, and the computing task (3) and the computing task (4) are added to the ring queue, to obtain the ring queue as Figure 4 shown, at this time, the consumption start point of the ring queue is updated to position 1, and the cache end point of the ring queue is updated to position 6.

[0034] When the first computing task is added into the first circular queue, the cache start point of the first computing task is the cache end point of the current first circular queue, that is, the first computing task is cached from the cache end point of the first circular queue, before the first computing task is added into the first circular queue, the cache end point of the first circular queue is the cache end point of a previous computing task (i.e., a second computing task) added into the first circular queue, the first computing task is cached from the cache end point of the second computing task, and after the first computing task is added into the first circular queue, the cache end point of the first circular queue is updated to the cache end point of the first computing task.

[0035] The consumer thread queue includes one or more consumer threads, and each consumer thread processes a computing task to be processed to obtain a corresponding processing result after obtaining the computing task to be processed from the circular queue.

[0036] When the consumer thread obtains a computing task to be processed in the first circular queue, the consumer thread obtains a computing task from the consumer start point, deletes the corresponding computing task in the circular queue after successfully obtaining the computing task, and then the memory space occupied by the computing task in the first circular queue is released and can be used to cache a new computing task again. In the initial stage, the consumer threads in the consumer thread queue are all in an idle state, the consumer threads in the consumer thread queue obtain computing tasks from the first circular queue in turn according to a pre-set priority order, and then process the computing tasks. After processing the corresponding computing task, the consumer thread sends a request to obtain a new computing task to be processed in the first circular queue.

[0037] After receiving the target request sent by the first consumer thread, the computing task corresponding to the consumer start point in the first circular queue is sent to the first consumer thread, and the first consumer thread processes the computing task. In the case that the consumer start point is the same as the cache end point of the first computing task, the first computing task is sent to the first consumer thread, and the first consumer thread processes the first computing task.

[0038] After sending the first computing task to the first consumer thread, the consumer start point of the first circular queue needs to be updated. The consumer start point of the first circular queue is updated to the cache end point of the first computing task, or the consumer start point of the first circular queue is updated to the cache start point of a third computing task, which is a computing task added into the first circular queue after the first computing task.

[0039] Through the above steps, by adding the to-be-processed computing task to the first ring queue at the cache end point of the ring queue, caching the to-be-processed computing task in the first ring queue, sending the computing task corresponding to the consumption start point to the consumer thread in the consumer thread queue for processing by the consumer thread, the to-be-processed computing task is sequentially added to the first ring queue and cached in the first queue, and the consumer thread in the consumer thread queue can also sequentially obtain the computing tasks from the first ring queue for processing, the consumer thread can sequentially process the computing tasks, and the same computing task cannot be obtained by multiple consumer threads. Therefore, the problem that thread safety in processing computing tasks cannot be guaranteed in the related art is solved, and the effect of improving thread safety in processing computing tasks is achieved.

[0040] In an optional embodiment, the method further includes: in a case where the first computing task is processed by the first consumer thread, adding the first computing task to a target computing task set, wherein the target computing task set records the computing tasks that have been processed; and recording a waiting duration of the first computing task and a processing duration of the first computing task, wherein the waiting duration of the first computing task is equal to a time interval between the first time and a second time, the second time is a time when the first computing task is sent to the first consumer thread, the waiting duration of the first computing task is a duration for which the first computing task waits to be processed, and the processing duration of the first computing task is a duration for which the first consumer thread processes the first computing task.

[0041] In this embodiment, the first computing task that has been processed by the first consumer thread is added to the target computing task set, the target computing task set records all computing tasks that have been processed by all consumer threads in the consumer thread queue, and the number of computing tasks in the target computing task set represents the number of computing tasks that have been processed.

[0042] The first computing task that has been processed is added to the target computing task set, and the waiting time and the processing time of the first computing task are recorded at the same time. The waiting time is the time for which the first computing task is cached in the first ring queue, and is determined by the first time and the second time, that is, determined by the time when the first computing task is added to the first ring queue and the time when the computing task leaves the first ring queue. The first time represents the time when the first computing task is added to the first ring queue, and the second time represents the time when the computing task leaves the first ring queue, that is, the time when the first computing task is sent to the first consumer thread. The processing duration of the first computing task is the duration for which the first consumer thread processes the first computing task, that is, the interval duration from when the first consumer thread starts processing the first computing task to when the first computing task is processed.

[0043] In an optional embodiment, the method further comprises: in a case where the number of the computing tasks in the target computing task set reaches a preset number threshold, determining an average waiting time and an average processing time according to the waiting time and the processing time corresponding to each of the computing tasks in the target computing task set; determining a target number of consumer threads according to the average waiting time and the average processing time; and in a case where the target number of consumer threads is less than or equal to a preset thread number threshold, adjusting the number of the consumer threads in the consumer thread queue to the target number of consumer threads.

[0044] In the embodiment, the number of the computing tasks in the target computing task set represents the number of the computing tasks that have been processed, and in a case where the number of the computing tasks that have been processed reaches a preset number threshold, the optimal number of threads (corresponding to the target number of consumer threads) is determined according to the average waiting time and the average processing time of the computing tasks, and represents the optimal value of the number of the consumer threads in the consumer thread queue.

[0045] The average waiting time and the average processing time of the computing tasks are determined according to the waiting time and the processing time of each of the computing tasks in the target computing task set, for example, the average waiting time of the computing tasks is obtained by dividing the sum of the waiting times of all the computing tasks in the target computing task set by the number of the computing tasks in the target computing task set, and the average processing time of the computing tasks is obtained by dividing the sum of the processing times of all the computing tasks in the target computing task set by the number of the computing tasks in the target computing task set.

[0046] After the target number of consumer threads is calculated, the number of the consumer threads in the consumer queue is adjusted according to the target number of consumer threads, and the adjusted number of the consumer threads cannot exceed the preset thread number threshold. Therefore, before the number of the consumer threads in the consumer queue is adjusted, the target number of consumer threads is compared with the preset thread number threshold, and in a case where the target number of consumer threads is less than or equal to the preset thread number threshold, the number of the consumer threads in the consumer thread queue is adjusted to the target number of consumer threads.

[0047] In a case where the target number of consumer threads is greater than the preset thread number threshold, the number of the consumer threads in the consumer thread queue is adjusted to the preset thread number threshold.

[0048] It should be noted that the preset thread number threshold can be determined according to the number of cores of the processor, for example, the preset thread number threshold is determined as 2*U+1, where U represents the number of cores of the processor.

[0049] In an optional embodiment, the determining the target number of consumer threads according to the average waiting time length and the average processing time length comprises: determining a target coefficient as a ratio of a target time length to the average processing time length, wherein the target time length is a sum of the average waiting time length and the average processing time length; and determining the target number of consumer threads as a product of the target coefficient and a number of cores of the processor.

[0050] In the embodiment, the target number of consumer threads is calculated according to the following formula:

[0051]

[0052] wherein U is the number of cores of the processor.

[0053] In an optional embodiment, the method further comprises: in a case where the target number of consumer threads is greater than the preset threshold of thread number, adjusting the number of consumer threads in the consumer thread queue to the preset threshold of thread number; and creating a second ring queue, wherein a memory value occupied by full added computing tasks in the second ring queue is a second memory value, a memory value occupied by full added computing tasks in the first ring queue is a first memory value, and the second memory value is greater than the first memory value.

[0054] In the embodiment, in a case where the target number of consumer threads is greater than the preset threshold of thread number, the number of consumer threads in the consumer thread queue is adjusted to the preset threshold of thread number, but since the number of consumer threads is not adjusted to an optimal value, the processing speed of computing tasks is less than the caching speed of computing tasks in the first ring queue, and thus the number of cached computing tasks in the first ring queue is increasing. In order to provide sufficient caching space for computing tasks, the first ring queue needs to be expanded.

[0055] The memory value of a ring queue represents the capacity of the ring queue, i.e., how many computing tasks can be cached in the ring queue. In a case where full added computing tasks are in the ring queue, the memory value occupied by all computing tasks is the memory value of the ring queue. The memory value of the first ring queue is the first memory value, the memory value of the second ring queue is the second memory value, and the memory value of the third ring queue is the third memory value.

[0056] The expansion operation is completed by establishing a new ring queue. When expansion of the first ring queue is needed, a second ring queue is established, and a second memory value of the second ring queue is greater than a first memory value of the first ring queue. After the second ring queue is created, new to-be-processed computing tasks are added to the second ring queue instead of the first ring queue. The consuming threads in the consuming thread queue still process the to-be-processed computing tasks in the first ring queue. After the computing tasks cached in the first ring queue are processed, the first ring queue is deleted, and the to-be-processed computing tasks in the second ring queue are processed.

[0057] In an optional embodiment, after the first computing task is added to the first ring queue by the first thread at the first time, the method further includes: updating the cache end point of the first ring queue to the cache end point of the first computing task in the first ring queue.

[0058] In an optional embodiment, before the first computing task is added to the first ring queue by the first thread at the first time, the method further includes: determining a residual queue memory value of the first ring queue at a third time, wherein the third time is a time when the second computing task is added to the first ring queue, and the residual queue memory value refers to a memory value required by a computing task allowed to be added to the first ring queue in a case where the second computing task is added to the first ring queue and the first computing task is not added to the first ring queue; and in a case where the residual queue memory value is greater than or equal to a memory value required by the first computing task, the first computing task is added to the first ring queue by the first thread at the first time.

[0059] In this embodiment, before the first computing task is added to the first ring queue, it is needed to determine whether the residual queue memory value of the first ring queue is sufficient for adding the first computing task. In a case where the residual queue memory value of the first ring queue is greater than or equal to a memory value required by the first computing task, the residual queue memory value of the first ring queue is sufficient for adding the first computing task, and the first computing task is added to the first ring queue by the first thread.

[0060] It should be noted that the residual queue memory value refers to a memory value of a computing task that is still allowed to be added to the first ring queue.

[0061] In an optional embodiment, the determining the residual queue memory value of the first ring queue at the third time point comprises: determining the residual queue memory value of the first ring queue at the third time point according to the consumption start point of the first ring queue at the third time point and the cache end point of the first ring queue.

[0062] In the embodiment, the residual queue memory value is determined according to the consumption start point of the first ring queue and the cache end point of the first ring queue.

[0063] The consumption start point and the cache end point are updated. Since the first ring queue is ring-shaped, the calculation task is cached from the preset position in the initial state. At this time, the consumption start point of the ring queue and the cache end point of the ring queue correspond to the preset position, and the ring number corresponding to the consumption start point of the ring queue and the cache end point of the ring queue is 1. Subsequently, when the consumption start point of the ring queue or the cache end point of the ring queue is updated to the preset position, the ring number corresponding to the consumption start point of the ring queue or the cache end point of the ring queue is increased by 1.

[0064] The memory difference value between the memory value corresponding to the consumption start point of the first ring queue and the memory value corresponding to the cache end point of the first ring queue is calculated, which is recorded as the first difference value. The memory value corresponding to the consumption start point of the first ring queue represents the memory value occupied by the calculation task that can be cached from the preset position to the consumption start point. The memory value corresponding to the cache end point of the first ring queue represents the memory value occupied by the calculation task that can be cached from the preset position to the cache end point. The memory value corresponding to each position represents the memory value occupied by the calculation task that can be cached from position 0 to the position.

[0065] When determining the residual queue memory value of the first ring queue, the consumption start point of the first ring queue, the first ring number corresponding to the consumption start point, the cache end point of the first ring queue, and the second ring number corresponding to the cache end point are determined.

[0066] Specifically, in the case where the first ring number and the second ring number are equal and the consumption start point and the cache end point are different, the difference between the first difference value and the first memory value is determined as the residual queue memory value. In the case where the first ring number and the second ring number are not equal and the consumption start point and the cache end point are different, the first difference value is determined as the residual queue memory value. In the case where the first ring number and the second ring number are not equal and the consumption start point and the cache end point are the same, the residual queue memory value is determined to be zero. In the case where the first ring number and the second ring number are equal and the consumption start point and the cache end point are the same, the residual queue memory value is determined to be the first memory value.

[0067] In an optional embodiment, the method further comprises: in the case that the remaining queue memory value is less than the memory value required by the first computing task, creating a third ring queue, wherein the memory value required by the full added computing task in the third ring queue is a third memory value, the memory value required by the full added computing task in the first ring queue is a first memory value, and the third memory value is greater than the first memory value; and adding the first computing task into the third ring queue by the first thread at the first time point.

[0068] In the embodiment, in the case that the remaining queue memory value of the first ring queue is less than the memory value required by the first computing task, the remaining queue memory value of the first ring queue does not buffer the added first computing task, thus, the first ring queue needs to be expanded, a third ring queue is established, the third memory value of the third ring queue is greater than the first memory value of the first ring queue, after the third ring queue is created, the first computing task is added into the third ring queue by the first thread, the consuming thread in the thread queue still processes the computing task to be processed in the first ring queue, after all the computing tasks buffered in the first ring queue are processed, the first ring queue is deleted, the first computing task in the second ring queue is sent to the first consuming thread, and the first computing task is processed by the first consuming thread, and in the subsequent addition of the computing task to be processed, the first computing task is added into the third ring queue from the buffered end point of the first computing task, and the computing task is no longer added into the first ring queue.

[0069] It should be noted that the memory value of the expanded ring queue cannot exceed the preset memory threshold, wherein the preset memory threshold can be set according to actual application, for example, no more than 20% of the size of the computer memory, if the memory value of the expanded ring queue cannot exceed the preset memory threshold, the ring queue will no longer accept new computing tasks.

[0070] In an optional embodiment, after the first thread adds the first computing task to the first ring queue at the first time, the method further comprises: determining a used memory value of the first ring queue, wherein the used memory value represents a memory value occupied by all the pending computing tasks cached in the first ring queue in the case that the first computing task is added to the first ring queue; creating a fourth ring queue when the used memory value is less than a preset initial threshold, wherein a fourth memory value required for adding full pending computing tasks to the fourth ring queue is less than a first memory value required for adding full pending computing tasks to the first ring queue.

[0071] In the embodiment, after the first computing task is added to the first ring queue, a used memory value of the first ring queue is determined, i.e., a memory value occupied by all the pending computing tasks cached in the first ring queue, wherein the used memory value can be determined according to a difference between a current remaining queue memory value and the first memory value.

[0072] When the used memory value is less than the preset initial threshold, it indicates that the first memory value of the current first ring queue is too large, and the memory value occupied by the pending computing tasks of the first ring queue does not need to be as large as the first memory value, thus, it is necessary to shrink the capacity of the first ring queue.

[0073] A fourth ring queue is established, and a fourth memory value of the fourth ring queue is less than the first memory value of the first ring queue. After the fourth ring queue is created, new pending computing tasks are added to the fourth ring queue by the first thread, and the consumer threads in the consumer thread queue still process the pending computing tasks in the first ring queue. After all the computing tasks cached in the first ring queue are processed, the first ring queue is deleted, and the pending computing tasks cached in the fourth ring queue are processed.

[0074] It should be noted that when the queue is shrunk, the memory value of the shrunk ring queue can be half of the memory value of the current ring queue, but the memory value of the shrunk ring queue cannot be less than the preset initial threshold, wherein the preset initial threshold can be set according to actual application.

[0075] Obviously, the above-described embodiments are only part of the embodiments of the present application, not all the embodiments.

[0076] The present application will be described in detail below with reference to the embodiments:

[0077] Figure 5is a schematic diagram of the ring queue shrinkage and expansion according to an embodiment of the present application, as shown, comprising the following steps: Figure 5

[0078] Step S501, creating a ring queue, the column length of the ring queue being V;

[0079] Step S502, caching a large number of computing tasks in the ring queue;

[0080] Step S503, judging whether the number of processed computing tasks is greater than a preset number threshold, in the case of yes, executing step S504, and in the case of no, executing step S510;

[0081] Step S504, calculating the target number of consumer threads;

[0082] Step S505, judging whether the target number of consumer threads is greater than a preset thread number threshold, in the case of yes, executing step S506, and in the case of no, executing step S507;

[0083] Step S506, adjusting the number of consumer threads in the consumer thread queue to the preset thread number threshold, expanding the queue, and then executing step S508; wherein the ring queue can be expanded to twice the capacity of the original queue when expanded.

[0084] Step S507, adjusting the number of consumer threads in the consumer thread queue to the target number of consumer threads, and then executing step S508;

[0085] Step S508, judging whether the consumption starting point of the queue is the same as the cache end point and whether the corresponding ring numbers are different, in the case of yes, executing step S509, and in the case of no, executing step S510;

[0086] Step S509, expanding the queue, and then executing step S510;

[0087] Step S510, judging whether the consumption starting point of the queue is the same as the cache end point and whether the corresponding ring numbers are the same, in the case of yes, executing step S511, and in the case of no, executing step S512;

[0088] Step S511, shrinking the queue, and then executing step S512; wherein shrinking the queue can be reducing the capacity of the queue to half of the current queue capacity, but the final capacity is not less than the initial capacity V0, the capacity of the queue is the memory value of the queue, and the initial capacity is the preset initial threshold.

[0089] ​Step S512, judging whether there is a new to-be-processed computing task, executing step S502 when the judging result is yes, and executing step S513 when the judging result is no;

[0090] Step S513, ending.

[0091] The application is specifically described in combination with another embodiment:

[0092] Step 1. Constructing the initial capacity V0 of the ring queue;

[0093] Step 2. Storing each request of all computing tasks into the ring queue;

[0094] Step 3. Starting T0 threads equal to the number of CPU cores U (corresponding to the number of cores of the processor) to consume the queue, recording the waiting time tw of each computing task and the running time tr of each computing task;

[0095] Step 4. Marking the consumption start point CaCb and the cache end point PaPb (a current ring number, b current queue position) of the ring queue in the ring queue, the cache start point Ws of each computing task and the cache end point We (available when storing multiple tasks into the queue), and each time a computing task is added to the ring queue, if Pb is equal to Cb and Pa is greater than Ca, the queue is full of computing tasks, the thread (corresponding to the first thread) saving data enters waiting and starts to count the waiting time, and the stored tasks are more, and the storage speed exceeds the consumption speed and needs to be expanded. Each consumption thread needs to change the atomicity flag in the ring queue when obtaining the computing task from the ring queue, so that the obtained computing task is not obtained by other consumption threads. When the cache tail Pb is equal to V0, the ring number corresponding to the cache end point is added by 1, that is, Pa+1, and the position in the queue returns to the start point; when Pa is greater than Ca and Pb is less than Cb, new computing tasks are cached at PaPb; when Pa is equal to Ca and Pb is equal to Cb, go to the 9th point (consumption end);

[0096] Step 5. When the number of processed computing tasks is greater than the preset number threshold, the average waiting time and the average processing time of the computing tasks are counted, and the formula is:

[0097]

[0098] The optimal thread number, that is, the target consumption thread number, is calculated, and the consumption thread queue size, that is, the number of consumption threads, is adjusted;

[0099] Step 6. If the target number of consumer threads does not exceed twice the number of CPU cores plus one (2*U+1), then adjust the number of consumer threads to the calculated target number of consumer threads, and if it exceeds 2*U+1, then adjust the number of consumer threads to equal 2*U+1, and expand the capacity of the ring queue to twice the original queue capacity (2*V0). The expanded capacity should not exceed 20% of the size of the computer memory, and if the capacity equals 20% of the size of the computer memory, then no new calculation task will be accepted;

[0100] 7. The expansion process opens up new space, and stores the newly entered calculation task from the Pa ring V0+1 position of the new queue, and when the consumer reaches V0, the number of consumer rings is incremented by one Ca+1, and when the consumer position Cb equals the V0+1 position, the original queue is deleted;

[0101] 8. The current capacity V1, when stored to the tail of the queue, i.e. Pb equals V1, and if (Pa-Ca)*V1+Pb-Cb is less than V0, then shrink the capacity by half, and the new queue capacity Vnew=V1 / 2, and Vnew>=V0. The shrinkage rule is explained as follows: when the task is stored to the tail of the queue, the number of consumer rings Ca and the number of storage rings Pa are equal, and the number of remaining tasks in the queue is less than the initial capacity V0, then the size of the queue is reduced to half of the current queue size, but the final capacity is not less than the initial capacity V0;

[0102] 9. The newly created capacity V0 queue Ca, Cb, Pa, Pb are set to zero, and the old queue is deleted.

[0103] Those skilled in the art can clearly understand the method according to the above embodiment can be realized by means of software and the necessary general hardware platform, of course, it can also be realized by hardware, but in many cases, the former is a better embodiment. Based on such understanding, the technical solutions of the present application or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes a plurality of instructions for making a terminal device (which can be a mobile phone, computer, server, or network device, etc.) execute the method described in each embodiment of the present application.

[0104] In the present embodiment, a processing device for calculation tasks is also provided, Figure 6 which is a structural block diagram of the processing device for calculation tasks according to the embodiment of the present application, as Figure 6 shown, the device comprises:

[0105] The adding module 602 is configured to add a first computing task into a first ring queue at a first time point by a first thread, wherein the first ring queue is used to buffer computing tasks to be processed, a buffering start point of the first computing task in the first ring queue is a buffering end point of the first ring queue, and the buffering end point of the first ring queue is a buffering end point of a second computing task in the first ring queue before the first computing task is added into the first ring queue, the second computing task being a computing task added into the first ring queue before the first computing task.

[0106] The sending module 604 is configured to send the first computing task to a first consuming thread in the first consuming thread queue to process the first computing task by the first consuming thread, when a target request of the first consuming thread in the first consuming thread queue is received and a consuming start point of the first ring queue is the buffering start point of the first computing task in the first ring queue, wherein each consuming thread in the first consuming thread queue is configured to process a computing task to be processed in the first ring queue when the computing task to be processed is received.

[0107] The updating module 606 is configured to update the consuming start point of the first ring queue to a buffering end point of the first computing task in the first ring queue.

[0108] In an optional embodiment, the apparatus further includes a first adding module configured to add the first computing task into a target computing task set when the first computing task is processed by the first consuming thread, wherein the target computing task set records computing tasks that have been processed.

[0109] The first recording module is configured to record a waiting time length of the first computing task and a processing time length of the first computing task, wherein the waiting time length of the first computing task is equal to a time interval between the first time point and a second time point, the second time point being a time point at which the first computing task is sent to the first consuming thread, the waiting time length of the first computing task being a time length during which the first computing task waits to be processed, and the processing time length of the first computing task being a time length during which the first computing task is processed by the first consuming thread.

[0110] In an optional embodiment, the apparatus further includes:

[0111] The first determining module is configured to determine an average waiting time length and an average processing time length according to the waiting time length and the processing time length of each computing task in the target computing task set when a number of computing tasks in the target computing task set reaches a preset number threshold.

[0112] a second determining module, configured to determine a target number of consumer threads according to the average waiting time length and the average processing time length;

[0113] a first adjusting module, configured to adjust a number of consumer threads in the consumer thread queue to the target number of consumer threads in a case where the target number of consumer threads is less than or equal to a preset threshold of thread number.

[0114] In an optional embodiment, the second determining module further includes:

[0115] a first determining subunit, configured to determine a target coefficient as a ratio of a target time length to the average processing time length, wherein the target time length is a sum of the average waiting time length and the average processing time length;

[0116] a second determining subunit, configured to determine the target number of consumer threads as a product of the target coefficient and a number of cores of a processor.

[0117] In an optional embodiment, the apparatus further includes:

[0118] a second adjusting module, configured to adjust the number of consumer threads in the consumer thread queue to the preset threshold of thread number in a case where the target number of consumer threads is greater than the preset threshold of thread number.

[0119] a first creating module, configured to create a second ring queue, wherein a memory value occupied by full added-to-be-processed computing tasks in the second ring queue is a second memory value, a memory value occupied by full added-to-be-processed computing tasks in the first ring queue is a first memory value, and the second memory value is greater than the first memory value.

[0120] In an optional embodiment, the apparatus further includes:

[0121] a first updating module, configured to update a cache end point of the first ring queue to a cache end point of the first computing task in the first ring queue after the first computing task is added to the first ring queue by the first thread at the first time.

[0122] In an optional embodiment, the apparatus further includes:

[0123] a third determining module, configured to determine a residual queue memory value of the first ring queue at a third time point before the first computing task is added to the first ring queue by the first thread at a first time point, wherein the third time point is a time point at which the second computing task is added to the first ring queue, and the residual queue memory value refers to a memory value required by computing tasks allowed to be added to the first ring queue in a case that the second computing task is added to the first ring queue and the first computing task is not added to the first ring queue;

[0124] a second adding module, configured to add the first computing task to the first ring queue by the first thread at the first time point in a case that the residual queue memory value is greater than or equal to the memory value required by the first computing task.

[0125] In an optional embodiment, the third determining module further includes a third determining subunit, configured to determine the residual queue memory value of the first ring queue at the third time point according to a consumption start point of the first ring queue at the third time point and a cache end point of the first ring queue.

[0126] In an optional embodiment, the apparatus further includes:

[0127] a second creating module, configured to create a third ring queue in a case that the residual queue memory value is less than the memory value required by the first computing task, wherein a memory value required by full computing tasks added to the third ring queue is a third memory value, a memory value required by full computing tasks added to the first ring queue is a first memory value, and the third memory value is greater than the first memory value;

[0128] a third adding module, configured to add the first computing task to the third ring queue by the first thread at the first time point.

[0129] In an optional embodiment, the apparatus further includes:

[0130] a fourth determining module, configured to determine a used memory value of the first ring queue after the first computing task is added to the first ring queue by the first thread at the first time point, wherein the used memory value refers to a memory value occupied by all computing tasks cached in the first ring queue in a case that the first computing task is added to the first ring queue.

[0131] A third creating module is configured to create a fourth ring queue when the memory value is less than a preset initial threshold value, wherein a memory value required for adding full to-be-processed computing tasks in the fourth ring queue is a fourth memory value, a memory value required for adding full to-be-processed computing tasks in the first ring queue is a first memory value, and the fourth memory value is less than the first memory value.

[0132] It should be noted that the above modules can be implemented by software or hardware, and for the latter, the following implementation manners can be used, but are not limited thereto: the above modules are located in the same processor; or the above modules are located in different processors in any combination.

[0133] Embodiments of the present application also provide a computer readable storage medium, which stores a computer program, and the computer program is configured to execute the steps in any of the above method embodiments when running.

[0134] In an example embodiment, the above computer readable storage medium can include, but is not limited to, a U disk, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk, and various media that can store computer programs.

[0135] Embodiments of the present application also provide an electronic device, which includes a memory storing a computer program and a processor configured to execute the computer program to perform the steps in any of the above method embodiments.

[0136] In an example embodiment, the above electronic device can further include a transmission device connected to the processor and an input / output device connected to the processor.

[0137] The specific examples in the present embodiment can refer to the examples described in the above embodiments and example embodiments, and the present embodiment will not be described here again.

[0138] It should be apparent to those skilled in the art that the modules or steps of the application described above can be implemented with general computing devices, which can be centralized on a single computing device or distributed on a network of multiple computing devices, which can be implemented with program codes executable by the computing devices, so that they can be stored in storage devices and executed by the computing devices, and in some cases, the steps shown or described can be executed in different orders than shown, or made into individual integrated circuit modules, or made into a single integrated circuit module. Thus, the application is not limited to any particular combination of hardware and software.

[0139] The preferred embodiments of the application described above are intended to be merely exemplary and those skilled in the art will recognize that many changes and modifications can be made to the application without departing from the spirit and scope of the application. Any modifications, equivalent substitutions, improvements, etc. made within the principles of the application are intended to be included in the protection scope of the application.

Claims

1. A processing method of a computing task, characterized by, The method comprises: adding, by a first thread, a first computing task into a first ring queue at a first time point, wherein the first ring queue is used to buffer computing tasks to be processed, a buffering start point of the first computing task in the first ring queue is a buffering end point of the first ring queue, and the buffering end point of the first ring queue is a buffering end point of a second computing task in the first ring queue before the first computing task is added into the first ring queue, the second computing task being a computing task preceding the first computing task and added into the first ring queue; sending the first computing task to a first consumer thread in the first ring queue for processing the first computing task by the first consumer thread, in a case that a target request of the first consumer thread in a consumer thread queue is received and a consumption start point of the first ring queue is the buffering start point of the first computing task in the first ring queue, wherein each consumer thread in the consumer thread queue is used to process a computing task to be processed in the first ring queue after the computing task to be processed is received; updating the consumption start point of the first ring queue to a buffering end point of the first computing task in the first ring queue. The method further comprises: in a case that a number of computing tasks in a target computing task set reaches a preset number threshold, determining an average waiting time and an average processing time according to a waiting time and a processing time corresponding to each computing task in the target computing task set, the target computing task set recording computing tasks that have been processed; determining a target number of consumer threads according to the average waiting time and the average processing time; in a case that the target number of consumer threads is less than or equal to a preset thread number threshold, adjusting a number of consumer threads in the consumer thread queue to the target number of consumer threads, the preset thread number threshold being determined by a number of cores of a processor, the target number of consumer threads being determined by a product of a target coefficient and the number of cores of the processor, the target coefficient being determined by a ratio of a target time to the average processing time, the target time being a sum of the average waiting time and the average processing time.

2. The method of claim 1, wherein, The method further comprises: in a case that the first computing task is processed by the first consumer thread, adding the first computing task into a target computing task set, wherein the target computing task set records computing tasks that have been processed; recording a waiting time of the first computing task and a processing time of the first computing task, wherein the waiting time of the first computing task is equal to a time interval between the first time point and a second time point, the second time point being a time point at which the first computing task is sent to the first consumer thread, the waiting time of the first computing task being a time length during which the first computing task waits to be processed, and the processing time of the first computing task being a time length during which the first computing task is processed by the first consumer thread.

3. The method of claim 1, wherein, The method further comprises: In a case where the target number of consumer threads is greater than the preset threshold number of threads, the number of consumer threads in the consumer thread queue is adjusted to the preset threshold number of threads. A second ring queue is created, wherein, when the second ring queue is full of to-be-processed computing tasks, a memory value required by the full to-be-processed computing tasks in the second ring queue is a second memory value, when the first ring queue is full of to-be-processed computing tasks, a memory value required by the full to-be-processed computing tasks in the first ring queue is a first memory value, and the second memory value is greater than the first memory value.

4. The method of claim 1, wherein, After the first computing task is added to the first ring queue by the first thread at the first time, the method further comprises: The cache end point of the first ring queue is updated to the cache end point of the first computing task in the first ring queue.

5. The method of claim 1, wherein, Before the first computing task is added to the first ring queue by the first thread at the first time, the method further comprises: A remaining queue memory value of the first ring queue at a third time is determined, wherein the third time is a time when the second computing task is added to the first ring queue, and the remaining queue memory value refers to a memory value required by computing tasks allowed to be added to the first ring queue in a case where the second computing task is added to the first ring queue and the first computing task is not added to the first ring queue. In a case where the remaining queue memory value is greater than or equal to the memory value required by the first computing task, the first computing task is added to the first ring queue by the first thread at the first time.

6. The method of claim 5, wherein, The determination of the remaining queue memory value of the first ring queue at the third time comprises: The remaining queue memory value of the first ring queue at the third time is determined according to a consumption start point of the first ring queue at the third time and a cache end point of the first ring queue.

7. The method of claim 5, wherein, The method further comprises: In a case where the remaining queue memory value is less than the memory value required by the first computing task, a third ring queue is created, wherein, when the third ring queue is full of to-be-processed computing tasks, a memory value required by the full to-be-processed computing tasks in the third ring queue is a third memory value, when the first ring queue is full of to-be-processed computing tasks, a memory value required by the full to-be-processed computing tasks in the first ring queue is a first memory value, and the third memory value is greater than the first memory value; The first computing task is added to the third ring queue by the first thread at the first time.

8. The method of claim 5, wherein, After the first computing task is added to the first ring queue by the first thread at the first time, the method further comprises: A used memory value of the first ring queue is determined, wherein the used memory value represents a memory value occupied by all to-be-processed computing tasks cached in the first ring queue in a case where the first computing task is added to the first ring queue. In the use, when the memory value is less than a preset initial threshold value, a fourth ring queue is created, wherein when a full to-be-processed computing task is added in the fourth ring queue, a memory value required by the full to-be-processed computing task added in the fourth ring queue is a fourth memory value, when a full to-be-processed computing task is added in the first ring queue, a memory value required by the full to-be-processed computing task added in the first ring queue is a first memory value, and the fourth memory value is less than the first memory value.

9. A processing device of a computing task, characterized by, The method comprises the following steps: adding a first computing task to a first ring queue by a first thread at a first time, wherein the first ring queue is used to buffer to-be-processed computing tasks, a buffering start point of the first computing task in the first ring queue is a buffering end point of the first ring queue, and before the first computing task is added to the first ring queue, the buffering end point of the first ring queue is a buffering end point of a second computing task in the first ring queue, the second computing task being a previous computing task added to the first ring queue of the first computing task; sending the first computing task to a first consumer thread in a first consumer thread queue to process the first computing task by the first consumer thread when a target request of the first consumer thread in the first consumer thread queue is received and a consumption start point of the first ring queue is the buffering start point of the first computing task in the first ring queue, wherein each consumer thread in the first consumer thread queue is used to process a to-be-processed computing task in the first ring queue when the to-be-processed computing task is received; updating the consumption start point of the first ring queue to the buffering end point of the first computing task in the first ring queue; The device is further configured to: when a number of computing tasks in a target computing task set reaches a preset number threshold value, determine an average waiting time and an average processing time according to a waiting time and a processing time corresponding to each computing task in the target computing task set, the target computing task set recording computing tasks that have been processed; determine a target number of consumer threads according to the average waiting time and the average processing time; when the target number of consumer threads is less than or equal to a preset thread number threshold value, adjust a number of consumer threads in the consumer thread queue to the target number of consumer threads, the preset thread number threshold value being determined by a number of cores of a processor, the target number of consumer threads being determined by a product of a target coefficient and the number of cores of the processor, and the target coefficient being determined by a ratio of a target time to the average processing time, the target time being a sum of the average waiting time and the average processing time.

10. A computer-readable storage medium, characterized in that, The computer program is stored in the computer-readable storage medium and is executed by the processor to implement the steps of the method in any one of claims 1 to 8. 11.An electronic device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, wherein, The processor, when executing the computer program, implements the steps of the method recited in any one of claims 1 to 8.

Citation Information

Patent Citations

  • Log synchronization method and device, storage medium and electronic equipment

    CN110019498A

  • Asynchronous queue processing method and device, electronic equipment and storage medium

    CN112463314A

  • Data flow statistical method and related device

    CN114168649A