Concurrent scheduling method and device based on Golang, medium and server
By judging the actual concurrent task volume and the target concurrent task volume in Golang's concurrent scheduling, and dynamically adjusting the creation of concurrent tasks, the problem of inability to flexibly control the number of concurrent in the existing technology is solved, and more efficient resource utilization and business requirements are achieved.
Patent Information
- Application Number
- CN202311235924.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-22
- Publication Date
- 2025-03-25
AI Technical Summary
The existing concurrent scheduling scheme based on Golang cannot flexibly perform concurrency control, resulting in the inability to dynamically adjust the number of concurrents according to business needs or server performance.
By judging the current actual concurrent task volume and the target concurrent task volume, dynamically adjust the creation of concurrent tasks, the target concurrent task volume is adjustable.
Flexible concurrency control is realized, which can better meet business needs, improve hardware resource utilization, and avoid system load abnormalities caused by excessive or low concurrency.
Smart Images

Figure CN119690598A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of concurrent scheduling, and in particular to a concurrent scheduling method and device, medium, and server based on Golang. Background Art
[0002] Using concurrent programming techniques to improve the utilization of multi-core central processing units (CPUs) is an important feature of modern programming languages.
[0003] Golang (also known as Go) is a statically strongly typed, compiled, concurrent programming language. Golang provides native and powerful concurrency support. By using the communication mechanism between goroutine and channel, developers can easily handle concurrency issues.
[0004] However, existing Golang-based concurrent scheduling solutions all use fixed-value scheduling for concurrency, that is, a fixed number of tasks are always performed concurrently during the scheduling process, and concurrency control cannot be performed flexibly. Summary of the invention
[0005] The problem to be solved by the present invention is: how to flexibly perform concurrency control.
[0006] To solve the above problems, an embodiment of the present invention provides a concurrent scheduling method based on Golang, the method comprising:
[0007] When receiving a concurrent task request, determine whether the current actual concurrent task volume is less than the current target concurrent task volume;
[0008] When the current actual concurrent task amount is less than the current target concurrent task amount, a concurrent task creation indication signal is output to create a concurrent task;
[0009] Wherein, the target concurrent task volume is adjustable.
[0010] Optionally, the method further comprises:
[0011] Receive target concurrent task volume adjustment information;
[0012] Based on the target concurrent task amount adjustment information, the target concurrent task amount is adjusted, and the adjusted target concurrent amount is used as the current target concurrent amount.
[0013] Optionally, the method further comprises:
[0014] When the adjusted target concurrent task amount is greater than the target concurrent task amount before adjustment, a message is written to the signal pipe.
[0015] Optionally, before writing a message to the signal pipe, also include:
[0016] Determine whether there is a blocked waiting task currently, and write a message to the signal pipe if there is a blocked waiting task currently.
[0017] Optionally, the method further comprises:
[0018] When the current actual concurrent task volume is greater than or equal to the current target concurrent task volume, block reading messages in the signal pipeline;
[0019] Based on the message read from the signal pipe, determine whether to output the concurrent task creation indication signal.
[0020] Optionally, determining whether to output a concurrent task creation indication signal based on a message read from a signal pipe includes:
[0021] When a message is read from the signal pipeline, it is determined whether the current actual concurrent task volume is less than the current target concurrent task volume;
[0022] When the current actual concurrent task amount is less than the current target concurrent task amount, a concurrent task creation indication signal is output.
[0023] Optionally, the message read from the signal pipe is a message released after the concurrent task is completed.
[0024] Optionally, the message read from the signal pipeline is a message written after the target concurrent task volume is added.
[0025] Optionally, the method further comprises:
[0026] Stores the current actual number of concurrent tasks and the number of tasks currently blocked and waiting.
[0027] Optionally, the method further comprises:
[0028] When a concurrent scheduling shutdown signal or a signal pipeline shutdown signal is received, the concurrent scheduling process ends.
[0029] The embodiment of the present invention also provides a concurrent scheduling device based on Golang, the device comprising:
[0030] A judgment unit, adapted to judge whether the current actual concurrent task amount is less than the current target concurrent task amount when receiving a concurrent task request;
[0031] A first task creation indication unit, adapted to output a concurrent task creation indication signal to create a concurrent task when the current actual concurrent task amount is less than the current target concurrent task amount;
[0032] Wherein, the target concurrent task volume is adjustable.
[0033] Optionally, the device further comprises:
[0034] The target concurrent task amount adjustment unit is adapted to receive target concurrent task amount adjustment information, adjust the target concurrent task amount based on the target concurrent task amount adjustment information, and use the adjusted target concurrent amount as the current target concurrent amount.
[0035] Optionally, the device further comprises:
[0036] The message writing unit is adapted to write a message to the signal pipeline when the adjusted target concurrent task volume is greater than the target concurrent task volume before adjustment.
[0037] Optionally, before writing the message to the signal pipe, the message writing unit is further adapted to determine whether there is currently a blocked waiting task, and write the message to the signal pipe when there is currently a blocked waiting task.
[0038] Optionally, the device further comprises:
[0039] The second task creation indication unit is adapted to block reading messages in the signal pipeline when the current actual concurrent task amount is greater than or equal to the current target concurrent task amount, and determine whether to output a concurrent task creation indication signal based on the message read from the signal pipeline.
[0040] Optionally, the second task creation indication unit is suitable for judging whether the current actual concurrent task volume is less than the current target concurrent task volume when a message is read from the signal pipe, and outputting a concurrent task creation indication signal when the current actual concurrent task volume is less than the current target concurrent task volume.
[0041] Optionally, the message read by the second task creation indication unit from the signal pipeline is a message released after the concurrent task is completed.
[0042] Optionally, the message read by the second task creation instruction unit from the signal pipeline is a message written after adding a target concurrent task quantity.
[0043] Optionally, the device further comprises:
[0044] The storage unit is suitable for the current actual concurrent task volume and the number of currently blocked waiting tasks.
[0045] Optionally, the device further comprises:
[0046] The scheduling closing unit is adapted to close the Golang-based concurrent scheduling device when a concurrent scheduling closing signal or a signal pipeline closing signal is received.
[0047] An embodiment of the present invention further provides a computer-readable storage medium on which a computer program is stored. The computer program is executed by a processor to implement the steps of any of the above methods.
[0048] An embodiment of the present invention further provides a server, comprising a memory and a processor, wherein the memory stores a computer program that can be run on the processor, and wherein the processor executes the steps of any one of the above-mentioned methods when running the computer program.
[0049] Compared with the prior art, the technical solution of the embodiment of the present invention has the following advantages:
[0050] By applying the solution of the present invention, when a concurrent task request is received, it is first determined whether the current actual concurrent task amount is less than the current target concurrent task amount. When the current actual concurrent task amount is less than the current target concurrent task amount, a concurrent task creation indication signal is output to create a concurrent task. Since the target concurrency amount is adjustable, after the target concurrency amount is adjusted, it is possible to determine whether to create a concurrent task by determining the size of the current actual concurrent task amount and the current target concurrent task amount, thereby flexibly performing concurrency control to better meet business needs and maximize hardware resource utilization. BRIEF DESCRIPTION OF THE DRAWINGS
[0051] Figure 1 It is a flowchart of a concurrent scheduling method based on Golang in an embodiment of the present invention;
[0052] Figure 2 It is a schematic diagram of the principle of a concurrent scheduling device in an embodiment of the present invention;
[0053] Figure 3 It is a flowchart of another concurrent scheduling method based on Golang in an embodiment of the present invention;
[0054] Figure 4 It is a flowchart of another concurrent scheduling method based on Golang in an embodiment of the present invention;
[0055] Figure 5 It is a structural diagram of a concurrent scheduling device in an embodiment of the present invention;
[0056] Figure 6 It is an application schematic diagram of a concurrent scheduling device in an embodiment of the present invention. DETAILED DESCRIPTION
[0057] Although Golang has powerful concurrency support, it is necessary to control the number of concurrent coroutines based on factors such as hardware resources and server performance.
[0058] Currently, the commonly used concurrent scheduling schemes include the following two:
[0059] 1) Based on the implementation of "buffered pipes". Specifically, a pipe with a fixed buffer length is initialized (i.e., make(chan struct{},N)). Before executing concurrent tasks, the coroutine first blocks the reading of data from a pipe. After completing the task, it rewrites a concurrent signal to the pipe.
[0060] 2) Based on the implementation of "Go official extension package - semaphore (sync / semaphore)". Specifically, you can first create a semaphore (ie w: = semaphore.NewWeighted(N)), the coroutine blocks to request a semaphore before executing the concurrent task (ie w.Acquire(ctx,1)), and releases a semaphore after completing the task (ie w.Release(1)).
[0061] The above two solutions both implement concurrent scheduling with a fixed concurrent number of N. Compared with solution 1), solution 2) can avoid scheduling anomalies caused by incorrectly writing or closing the pipeline.
[0062] However, both solution 1) and solution 2) use concurrent scheduling with a fixed concurrency of N, that is, during the scheduling process, N tasks are always performed concurrently, and the current concurrency cannot be flexibly increased or decreased according to business needs or server performance.
[0063] In actual business, if the concurrency is too low, the system resources cannot be fully utilized and performance is wasted. If the concurrency is too high, the system load may exceed and service anomalies may occur.
[0064] To address this problem, an embodiment of the present invention provides a concurrent scheduling method based on Golang. By applying the method, since the target concurrency is adjustable, concurrency control can be flexibly performed by adjusting the target concurrency to better meet business needs and maximize hardware resource utilization.
[0065] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, specific embodiments of the present invention are described in detail below with reference to the accompanying drawings.
[0066] Reference Figure 1 , an embodiment of the present invention provides a concurrent scheduling method based on Golang. Specifically, the method may include the following steps:
[0067] Step 11: Receive concurrent task requests.
[0068] In the specific implementation, refer to Figure 2The concurrent scheduling device 21 executing the concurrent scheduling method in the embodiment of the present invention can be connected to the task execution module 22 of the task master program. The task execution module 22 can send a concurrent task request to the concurrent scheduling device 21 when a concurrent task needs to be executed.
[0069] Step 12, determining whether the current actual concurrent task volume is less than the current target concurrent task volume, wherein the target concurrent task volume is adjustable.
[0070] In a specific implementation, the current actual concurrent task volume is used to indicate the number of concurrent tasks currently being executed.
[0071] In practical applications, the actual concurrent task volume can be stored and updated in real time so that the stored actual concurrent task volume can represent the actual concurrent task volume at the current moment. When step 12 needs to be executed, the stored actual concurrent task volume data can be directly read as the current actual concurrent task volume and compared with the current target concurrent task volume.
[0072] In a specific implementation, the target concurrent task amount is used to represent the maximum number of concurrent tasks currently allowed. The target concurrent task amount is adjustable.
[0073] In practical applications, the target concurrent task volume can be stored. When the target concurrent task volume is adjusted, the stored target concurrent task volume is updated in real time so that the stored target concurrent task volume can represent the target concurrent task volume at the current moment. Thus, when step 12 needs to be executed, the stored target concurrent task volume can be directly read as the current target concurrent task volume and compared with the current actual concurrent task volume.
[0074] In specific implementations, the target concurrent task volume can be adjusted based on a variety of factors, and the specific factors are not limited. For example, the target concurrent task volume can be adjusted based on current business needs to better meet business needs. The target concurrent task volume can also be adjusted based on system performance to more fully utilize server performance and maximize hardware resource utilization.
[0075] When the current actual concurrent task amount is less than the current target concurrent task amount, it indicates that the current actual number of concurrent tasks is less than the current maximum allowable number of concurrent tasks. At this time, step 13 may be executed.
[0076] When the current actual concurrent task amount is greater than or equal to the current target concurrent task amount, it indicates that the current actual number of concurrent tasks substantially exceeds the current maximum allowable number of concurrent tasks. At this time, step 14 may be executed.
[0077] Step 13: output a concurrent task creation indication signal to create a concurrent task.
[0078] For example, if the current target concurrent task quantity is 50, when a concurrent task request is received, if the current actual number of concurrent tasks is 30, then the output of a concurrent task creation indication signal can be supported.
[0079] In a specific implementation, the concurrent task creation indication signal is used to indicate the creation of a concurrent task. Figure 2 ) can receive the concurrent task creation indication signal, thereby starting the coroutine to asynchronously execute the corresponding concurrent tasks.
[0080] There is no limitation on how the concurrent task creation indication signal specifically indicates the creation of concurrent tasks. For example, the concurrent task creation indication signal can be set as a pulse signal of a specific frequency, so that once the task execution module 22 receives the pulse signal, it can start the coroutine to asynchronously execute the corresponding concurrent task.
[0081] Step 14, block the reading of messages in the signal pipe.
[0082] For example, if the current target concurrent task quantity is 50, when a concurrent task request is received, if the current actual number of concurrent tasks is 55, it is necessary to block the reading of messages in the signal pipeline.
[0083] In the embodiments of the present invention, referring to Figure 2 , signal pipe 23 is used for message passing between coroutines. Signal pipe 23 can be an unbuffered pipe, that is, the sender and receiver of signal pipe 23 must be ready at the same time when exchanging messages. Unbuffered pipes do not need to set the length. When a receiver attempts to read data from a signal pipe, if no sender writes a message to the signal pipe, the read operation will be blocked. When a sender attempts to write a message to a signal pipe, if no receiver is waiting to read on the signal pipe, the write operation will be blocked.
[0084] When the current actual concurrent task amount is greater than or equal to the current target concurrent task amount, the message in the signal pipe is blocked, that is, as a receiver of the message in the signal pipe 23, it waits for the writing of the message in the signal pipe 23. Once there is a message written in the signal pipe 23, the message written in the signal pipe 23 can be read.
[0085] In one embodiment of the present invention, the task execution module 22 will write a message to the signal pipe 23 after the task is completed to indicate that the current task execution is finished. At this time, the message read from the signal pipe 23 is a message released after the concurrent task is completed.
[0086] In another embodiment of the present invention, after the target task volume increases, the concurrent scheduling device also writes a message to the signal pipe 23 to indicate the increase in the target concurrency volume. At this time, the message read from the signal pipe 23 is the message written to the signal pipe 23 by the concurrent scheduling device after the target concurrent task volume is added.
[0087] Step 15: Determine whether to output a concurrent task creation indication signal based on the message read from the signal pipeline.
[0088] In a specific implementation, when a message is read from the signal pipeline, it indicates that the current scheduling state has changed. At this time, a concurrent task creation indication signal can be directly output.
[0089] In one embodiment of the present invention, since the task status may change at any time during the concurrent scheduling process, and the target concurrent task volume may also be adjusted at any time, when a message is read from the signal pipeline, the size relationship between the current actual concurrent task volume and the current target concurrent task volume can be judged again, so that when the current actual concurrent task volume is less than the current target concurrent task volume, a concurrent task creation indication signal is output, thereby further improving the reliability of concurrent scheduling.
[0090] In some embodiments, the method may further include:
[0091] When a concurrent scheduling shutdown signal or a signal pipeline shutdown signal is received, the concurrent scheduling process ends.
[0092] In a specific implementation, the concurrent scheduling shutdown signal is used to shut down the concurrent scheduling process. When the concurrent scheduling process is shut down, the signal pipeline is also shut down. The closed signal pipeline cannot be written or read. Through the concurrent scheduling shutdown signal, the concurrent scheduling process can be terminated at any time during the concurrent scheduling process, making concurrent scheduling more flexible.
[0093] In a specific implementation, the signal pipeline closing signal is used to close the signal pipeline in the process of blocking the reading of messages in the signal pipeline. Once the channel pipeline is closed, the entire concurrent scheduling process ends.
[0094] In a specific implementation, when a concurrent scheduling shutdown signal or a signal pipeline shutdown signal is received, the concurrent scheduling device may first detect whether the concurrent scheduling has been closed. If the concurrent scheduling has not been closed, the concurrent scheduling shutdown operation is performed to exit the concurrent scheduling process. Specifically, the concurrent scheduling device may first detect the field identifying the scheduling state in the data structure. When the value of the field identifying the scheduling state is true, it indicates that the concurrent scheduling is closed, otherwise it indicates that the concurrent scheduling is not closed. If the concurrent scheduling is not closed, the concurrent scheduling can be closed by modifying the value of the field identifying the scheduling state to true.
[0095] Figure 3 Flow chart of a method for adjusting target concurrent task volume in an embodiment of the present invention. Figure 3 , the method may include:
[0096] Step 31, receiving target concurrent task amount adjustment information.
[0097] In a specific implementation, it is possible to detect in real time whether the target concurrent task amount adjustment information is received. There is no restriction on the time of adjusting the target concurrent task amount, and the target concurrent task amount can be adjusted before executing the concurrent scheduling method or during the execution of the concurrent scheduling method.
[0098] In a specific implementation, after the target concurrent task volume is initialized, the target concurrent task volume can be adjusted once, twice, or more than twice. When the target concurrent task volume is adjusted more than twice, the last adjustment before receiving the current task request is used as the current target concurrent task volume to compare with the current actual concurrent task volume.
[0099] Step 32: adjust the target concurrent task volume based on the target concurrent task volume adjustment information, and use the adjusted target concurrent volume as the current target concurrent volume.
[0100] In a specific implementation, the target concurrent task amount adjustment information is used to indicate the adjusted target concurrent task amount. The specific indication method is not limited. For example, the adjustment amount of the target concurrent task amount can be indicated, and the concurrent scheduling device can obtain the current target concurrent task amount based on the adjustment amount of the target concurrent task amount and the target concurrent task amount before the adjustment. The adjusted target concurrent task amount can also be directly indicated, and the concurrent scheduling device can use the value indicated in the target concurrent task amount adjustment information as the previous target concurrent task amount.
[0101] In some embodiments, the method may further include:
[0102] Step 33, determining whether the adjusted target concurrent task volume is greater than the target concurrent task volume before adjustment.
[0103] In a specific implementation, it is determined whether the adjusted target concurrent task volume is greater than the target concurrent task volume before the adjustment, that is, it is determined whether the target concurrent task volume increases or decreases.
[0104] When the adjusted target concurrent task volume is greater than the target concurrent task volume before adjustment, step 34 can be executed. When the adjusted target concurrent task volume is less than or equal to the target concurrent task volume before adjustment, no message is written to the signal pipeline, and the target concurrent task volume adjustment process ends.
[0105] Step 34, write a message to the signal pipe.
[0106] That is, when the target concurrent task amount increases, the concurrent scheduling device can write a message to the signal pipeline, read a message from the signal pipeline, and determine whether to output a concurrent task creation indication signal based on the message read from the signal pipeline.
[0107] In some embodiments, before writing a message to the signal pipe, that is, before executing step 34, step 35 may also be executed.
[0108] Step 35, determine whether there is currently a blocked waiting task.
[0109] In the specific implementation, refer to Figure 2 After the task execution module 22 sends a task request to the concurrent scheduling device, if the concurrent scheduling device 21 feeds back a concurrent task creation indication signal to the task execution module 22, the task requested by the task execution module 22 does not need to be a blocked waiting task. If the concurrent scheduling device 21 does not feed back a concurrent task creation indication signal to the task execution module 22, the task requested by the task execution module 22 will be a blocked waiting task. In this way, on the output side of the signal pipeline, there may be no blocked waiting task, or there may be one or more blocked waiting tasks.
[0110] In a specific implementation, the concurrent scheduling device can store the number of blocked waiting tasks, and based on the task execution status, update the number of blocked waiting tasks in real time, so that the stored number of blocked waiting tasks can represent the current number of blocked waiting tasks. When it is subsequently determined whether there are currently any blocked waiting tasks, the stored number of blocked waiting tasks can be directly read. When the stored number of blocked waiting tasks is greater than 0, it indicates that there are currently any blocked waiting tasks. When the stored number of blocked waiting tasks is less than or equal to 0, it indicates that there are currently no blocked waiting tasks.
[0111] When there is currently a blocked waiting task, step 34 is executed, otherwise the target concurrent task amount adjustment process is terminated.
[0112] Before writing a message to the signal pipe, by judging whether there is a blocked waiting task, the message can be written to the signal pipe only when there is a blocked waiting task, otherwise the message will not be written to the signal pipe. In this way, when there is a blocked waiting task, the waiting task can be awakened, the task waiting time can be reduced, and the task execution efficiency can be improved.
[0113] In some embodiments, after executing step 31 and before executing step 32, that is, after receiving the target concurrent task amount adjustment information and before adjusting the target concurrent task amount, step 36 may also be executed.
[0114] Step 36, determine whether concurrent scheduling is closed.
[0115] The concurrent scheduling can be turned off by a concurrent scheduling turn-off signal. When the concurrent scheduling is turned off, the target concurrent task amount adjustment process ends, otherwise, step 32 is continued to adjust the target concurrent task amount.
[0116] Figure 4 It is a flowchart of another Golang-based concurrent scheduling method provided by an embodiment of the present invention.
[0117] In a specific implementation, the concurrent scheduling device can store the actual concurrent task quantity and the number of blocked waiting tasks, which is used to record the current actual number of concurrent tasks and the number of concurrent tasks currently waiting in line. Figure 4 , the process of recording the actual number of concurrent tasks and the number of blocked waiting tasks is described in detail.
[0118] Specifically, the method may include:
[0119] Step 401, receiving a concurrent task request.
[0120] Step 402, determine whether concurrent scheduling is closed.
[0121] When the concurrent scheduling is not closed, step 403 is executed, otherwise the concurrent scheduling process ends.
[0122] Step 403, determining whether the current actual number of concurrent tasks is less than the current target number of concurrent tasks.
[0123] When the current actual number of concurrent tasks is less than the current target number of concurrent tasks, step 404 is executed; otherwise, step 405 is executed.
[0124] Step 404: output a concurrent task creation indication signal, and increase the actual number of concurrent tasks by 1.
[0125] That is, when the current actual number of concurrent tasks is less than the current target number of concurrent tasks, a concurrent task creation indication signal is directly output to create a new concurrent task. In addition, the stored actual number of concurrent tasks is increased by 1 as the current actual number of concurrent tasks.
[0126] Step 405, block and wait for the number of tasks +1.
[0127] That is to say, the stored number of blocked waiting tasks is increased by 1 as the current number of blocked waiting tasks.
[0128] Step 406, blocking the reading of messages in the signal pipe.
[0129] In a specific implementation, the message read from the signal pipeline may be a message released after a concurrent task is completed, or a message written after a target concurrent task amount is added.
[0130] In some embodiments, after executing step 406, step 407 may be executed first.
[0131] Step 407, determine whether the signal pipeline is closed.
[0132] When the signal pipeline is closed, the concurrent scheduling process ends, otherwise, step 408 is continued.
[0133] Step 408, read the message from the signal pipe.
[0134] Step 409, determining whether the current actual concurrent task volume is less than the current target concurrent task volume.
[0135] When the current actual concurrent task amount is less than the current target concurrent task amount, step 410 is executed, otherwise step 406 is continued to be executed, that is, the message in the reading signal pipeline is blocked again until the current actual concurrent task amount is less than the current target concurrent task amount.
[0136] By determining again whether the current actual concurrent task amount is less than the current target concurrent task amount, step 410 is performed only when the current actual concurrent task amount is less than the current target concurrent task amount, thereby improving the reliability of concurrent scheduling.
[0137] Step 410, output a concurrent task creation indication signal, and increase the actual concurrent task quantity by 1, and decrease the number of blocked waiting tasks by 1.
[0138] That is to say, when the current actual concurrent task volume is less than the current target concurrent task volume, a concurrent task creation indication signal is output to create a new concurrent task, and the stored actual concurrent task volume is increased by 1 as the current actual concurrent task volume, and the number of blocked waiting tasks is increased by 1 as the current number of blocked waiting tasks.
[0139] In the specific implementation, Golang's native mutex can be used to control the execution order of the three operations: writing messages in the signal pipeline, adjusting the target concurrency, and closing the concurrent scheduling, to ensure that only one of the three operations can be executed at the same time, avoiding concurrent operations causing program errors.
[0140] In the specific implementation, the Golang native data structure chan struct{} can be used to perform operations such as reading messages from the signal pipe, writing messages to the signal pipe, and closing concurrent channels.
[0141] As can be seen from the above, in the concurrent scheduling method based on Golang in the embodiment of the present invention, the target concurrent task amount is adjustable. Regardless of the value of the target concurrent task amount, after receiving the concurrent task request, it is first determined whether the current actual concurrent task amount is less than the current target concurrent task amount. When the current actual concurrent task amount is less than the current target concurrent task amount, a concurrent task creation indication signal is output to create a concurrent task, thereby enabling more flexible concurrent control. In addition, the user can adjust the target concurrent task amount at any time to better meet business needs, and can make more full use of server performance to maximize hardware resource utilization.
[0142] Furthermore, by storing the current actual number of concurrent tasks and the current number of blocked waiting tasks, it is not only convenient for concurrent scheduling, but also the real-time status of concurrency can be recorded by storing data. The stored data can be returned later through public methods, so that users can obtain the stored data at any time to understand the current concurrency status.
[0143] Furthermore, concurrent scheduling can be stopped at any time through a concurrent scheduling shutdown signal or a signal pipeline shutdown signal, further improving the flexibility of concurrent control.
[0144] In order to enable those skilled in the art to better understand and implement the present invention, the device, computer-readable storage medium and server corresponding to the above method are described in detail below.
[0145] Reference Figure 5 The embodiment of the present invention further provides a concurrent scheduling device 50 based on Golang, and the concurrent scheduling device 50 may include: a judgment unit 51 and a first task creation indication unit 52.
[0146] in:
[0147] The judging unit 51 is adapted to judge whether the current actual concurrent task amount is less than the current target concurrent task amount when receiving a concurrent task request;
[0148] The first task creation indication unit 52 is adapted to output a concurrent task creation indication signal to create a concurrent task when the current actual concurrent task amount is less than the current target concurrent task amount.
[0149] In an embodiment of the present invention, the concurrent scheduling device 50 may further include: a target concurrent task amount adjustment unit 53. The target concurrent task amount adjustment unit 53 is suitable for a target concurrent task amount adjustment unit.
[0150] In an embodiment of the present invention, the concurrent scheduling device 50 may further include: a message writing unit 54. The message writing unit 54 is adapted to write a message to the signal pipeline when the adjusted target concurrent task volume is greater than the target concurrent task volume before adjustment.
[0151] In a specific implementation, before writing a message to the signal pipe, the message writing unit 54 is also adapted to determine whether there is a blocked waiting task currently, and write a message to the signal pipe when there is a blocked waiting task currently.
[0152] In one embodiment of the present invention, the concurrent scheduling device 50 may further include: a second task creation indication unit 55. The second task creation indication unit 55 is adapted to block reading messages in the signal pipeline when the current actual concurrent task amount is greater than or equal to the current target concurrent task amount, and determine whether to output a concurrent task creation indication signal based on the message read from the signal pipeline.
[0153] In a specific implementation, the second task creation indication unit 55 is suitable for judging whether the current actual concurrent task volume is less than the current target concurrent task volume when a message is read from the signal pipeline, and outputting a concurrent task creation indication signal when the current actual concurrent task volume is less than the current target concurrent task volume.
[0154] The message read by the second task creation instruction unit 55 from the signal pipeline may be a message released after the concurrent task is completed, or a message written after the target concurrent task amount is added.
[0155] In an embodiment of the present invention, the concurrent scheduling device 50 may further include a storage unit 56. The storage unit 56 is adapted to store the current actual concurrent task quantity and the current number of blocked waiting tasks.
[0156] In an embodiment of the present invention, the concurrent scheduling device 50 may further include: a scheduling closing unit 57, wherein the scheduling closing unit 57 is adapted to close the concurrent scheduling device 50 when receiving a concurrent scheduling closing signal or a signal pipeline closing signal.
[0157] In a specific implementation, the data structure of the concurrent scheduling device may include:
[0158] 1) mu: sync.mutex. sync.mutex is a native mutex in Golang. This field is used to control the execution order of the three operations: writing the release message to the signal pipeline, adjusting the target concurrent task volume (Reset), and closing the concurrent scheduling (Close). This ensures that only one of the three operations can be executed at the same time, avoiding program errors caused by closing and writing concurrent operation signal pipelines.
[0159] 2) signal: chan struct{}, message. Chan is a native data structure of Golang and is also the core field of the concurrent scheduling device. Operating this field can simulate operations such as message reading, released message writing, and closing the signal pipeline.
[0160] 3) isClosed: bool, whether concurrent scheduling has been closed. This status field indicates whether concurrent scheduling has been closed.
[0161] 4)limit: int, target concurrent task volume. This field is used to identify the current target concurrent task volume.
[0162] 5) runningNum: int, actual concurrent number. This field is used to record the current actual number of concurrent tasks.
[0163] 6) blockingNum: int, blocking waiting number. This field is used to record the number of concurrent tasks currently waiting in queue.
[0164] Figure 6 FIG. 1 is a schematic diagram of an application of a concurrent scheduling device in an embodiment of the present invention. Figure 6 , assuming that the current target number of concurrent tasks is 2, the concurrent scheduling process is as follows:
[0165] First, the task execution module 61 sends two concurrent task requests to the concurrent scheduling device 62 .
[0166] The concurrent scheduling device 62 sends two concurrent task creation indication messages to the task execution module 61. The task execution module 61 asynchronously executes two tasks, namely, asynchronous execution task 1 and asynchronous execution task 2, based on the two concurrent task creation indication messages received.
[0167] Next, since the current actual concurrent task volume is equal to the current concurrent target task volume, when the task execution module 61 sends a concurrent task request to the concurrent scheduling device 62 again, the concurrent scheduling device 62 can block the message of reading the signal pipeline 63. The message blocked for reading can be the message released after the asynchronous execution of task 1 and the asynchronous execution of task 2.
[0168] After the concurrent scheduling device 62 reads the message from the signal pipe 63, it sends the concurrent task creation indication information to the task execution module 61 again. The task execution module 61 can asynchronously execute other tasks based on the concurrent task creation indication information, for example, asynchronously execute task 3 and asynchronously execute task 4.
[0169] In the above scheduling process, once the concurrent scheduling device 62 is closed or the signal channel is closed, the concurrent scheduling is terminated.
[0170] By using the above-mentioned concurrent scheduling device 62, it is possible to increase or decrease the target concurrency, stop concurrent scheduling in real time, and make concurrency control more flexible.
[0171] An embodiment of the present invention further provides a computer-readable storage medium on which a computer program is stored. The computer program is executed by a processor to implement the steps of any of the above methods.
[0172] In a specific implementation, the computer-readable storage medium may include: ROM, RAM, magnetic disk or optical disk, etc.
[0173] An embodiment of the present invention further provides a server, comprising a memory and a processor, wherein the memory stores a computer program that can be run on the processor, and the processor executes the steps of any of the above methods when running the computer program.
[0174] Regarding the various modules / units included in the various devices and products described in the above embodiments, they can be software modules / units, or hardware modules / units, or they can be partially software modules / units and partially hardware modules / units. For example, for various devices and products applied to or integrated in a chip, the various modules / units included therein can all be implemented in the form of hardware such as circuits, or at least some of the modules / units can be implemented in the form of software programs, which run on a processor integrated inside the chip, and the remaining (if any) modules / units can be implemented in the form of hardware such as circuits; for various devices and products applied to or integrated in a chip module, the various modules / units included therein can all be implemented in the form of hardware such as circuits, and different modules / units can be located in the same component (such as a chip, circuit module, etc.) or different components of the chip module, or at least some of the modules / units can be implemented in the form of hardware such as circuits. The element can be implemented in the form of a software program, which runs on a processor integrated inside the chip module, and the remaining (if any) modules / units can be implemented in the form of hardware such as circuits; for various devices and products applied to or integrated in the terminal, the various modules / units contained therein can be implemented in the form of hardware such as circuits, and different modules / units can be located in the same component (for example, chip, circuit module, etc.) or in different components in the terminal, or, at least some modules / units can be implemented in the form of a software program, which runs on a processor integrated inside the terminal, and the remaining (if any) modules / units can be implemented in the form of hardware such as circuits.
[0175] Although the present invention is disclosed as above, the present invention is not limited thereto. Any person skilled in the art can make various changes and modifications without departing from the spirit and scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the scope defined by the claims.
Claims
1. A concurrent scheduling method based on Golang, characterized in that: include: When receiving a concurrent task request, determine whether the current actual concurrent task volume is less than the current target concurrent task volume; When the current actual concurrent task amount is less than the current target concurrent task amount, a concurrent task creation indication signal is output to create a concurrent task; Wherein, the target concurrent task volume is adjustable.
2. The Golang-based concurrent scheduling method according to claim 1, characterized in that: Also includes: Receive target concurrent task volume adjustment information; Based on the target concurrent task amount adjustment information, the target concurrent task amount is adjusted, and the adjusted target concurrent amount is used as the current target concurrent amount.
3. The Golang-based concurrent scheduling method according to claim 2, characterized in that: Also includes: When the adjusted target concurrent task amount is greater than the target concurrent task amount before adjustment, a message is written to the signal pipe.
4. The Golang-based concurrent scheduling method according to claim 3, characterized in that: Before writing a message to the signal pipe, also include: Determine whether there is a blocking waiting task currently, and write a message to the signal pipe if there is a blocking waiting task currently.
5. The Golang-based concurrent scheduling method according to claim 1 or 2, characterized in that: Also includes: When the current actual concurrent task volume is greater than or equal to the current target concurrent task volume, block reading messages in the signal pipeline; Based on the message read from the signal pipe, determine whether to output the concurrent task creation indication signal.
6. The Golang-based concurrent scheduling method according to claim 5, characterized in that: The determining whether to output a concurrent task creation indication signal based on a message read from a signal pipeline includes: When a message is read from the signal pipeline, it is determined whether the current actual concurrent task volume is less than the current target concurrent task volume; When the current actual concurrent task amount is less than the current target concurrent task amount, a concurrent task creation indication signal is output.
7. The Golang-based concurrent scheduling method according to claim 5, characterized in that: The message read from the signal pipe is the message released after the concurrent task is completed.
8. The Golang-based concurrent scheduling method according to claim 5, characterized in that: The message read from the signal pipeline is the message written after the target concurrent task volume is added.
9. The Golang-based concurrent scheduling method according to claim 1, characterized in that: Also includes: Stores the current actual number of concurrent tasks and the number of tasks currently blocked and waiting.
10. The Golang-based concurrent scheduling method according to claim 1, characterized in that: Also includes: When a concurrent scheduling shutdown signal or a signal pipeline shutdown signal is received, the concurrent scheduling process ends.
11. A concurrent scheduling device based on Golang, characterized in that: include: A judgment unit, adapted to judge whether the current actual concurrent task amount is less than the current target concurrent task amount when receiving a concurrent task request; A first task creation indication unit, adapted to output a concurrent task creation indication signal to create a concurrent task when the current actual concurrent task amount is less than the current target concurrent task amount; Wherein, the target concurrent task volume is adjustable.
12. The concurrent scheduling device based on Golang as claimed in claim 11, characterized in that: Also includes: The target concurrent task amount adjustment unit is adapted to receive target concurrent task amount adjustment information, adjust the target concurrent task amount based on the target concurrent task amount adjustment information, and use the adjusted target concurrent amount as the current target concurrent amount.
13. The concurrent scheduling device based on Golang as claimed in claim 12, characterized in that: Also includes: The message writing unit is adapted to write a message to the signal pipeline when the adjusted target concurrent task volume is greater than the target concurrent task volume before adjustment.
14. The concurrent scheduling device based on Golang as claimed in claim 13, characterized in that: Before writing a message to the signal pipe, the message writing unit is further adapted to determine whether there is a currently blocked waiting task, and write the message to the signal pipe when there is a currently blocked waiting task.
15. The concurrent scheduling device based on Golang as claimed in claim 11 or 12, characterized in that: Also includes: The second task creation indication unit is adapted to block reading messages in the signal pipeline when the current actual concurrent task amount is greater than or equal to the current target concurrent task amount, and determine whether to output a concurrent task creation indication signal based on the message read from the signal pipeline.
16. The concurrent scheduling device based on Golang as claimed in claim 15, characterized in that: The second task creation indication unit is suitable for judging whether the current actual concurrent task amount is less than the current target concurrent task amount when reading a message from the signal pipeline, and outputting a concurrent task creation indication signal when the current actual concurrent task amount is less than the current target concurrent task amount.
17. The concurrent scheduling device based on Golang as claimed in claim 15, characterized in that: The message read by the second task creation instruction unit from the signal pipeline is a message released after the concurrent task is completed.
18. The concurrent scheduling device based on Golang as claimed in claim 15, characterized in that: The message read by the second task creation instruction unit from the signal pipeline is a message written after the target concurrent task quantity is added.
19. The concurrent scheduling device based on Golang as claimed in claim 11, characterized in that: Also includes: The storage unit is suitable for the current actual concurrent task volume and the number of currently blocked waiting tasks.
20. The concurrent scheduling device based on Golang as claimed in claim 11, characterized in that: Also includes: The scheduling closing unit is adapted to close the Golang-based concurrent scheduling device when a concurrent scheduling closing signal or a signal pipeline closing signal is received.
21. A computer-readable storage medium having a computer program stored thereon, characterized in that: The computer program is executed by a processor to implement the steps of the method according to any one of claims 1 to 11.
22. A server comprising a memory and a processor, wherein the memory stores a computer program that can be run on the processor, characterized in that: When the processor runs the computer program, the steps of the method according to any one of claims 1 to 11 are performed.