A task processing method and device, electronic equipment and storage medium
By introducing a combination of worker threads, executor threads, and coroutines into a general search engine, the problems of high thread synchronization overhead and resource waste are solved, resulting in more efficient task processing and faster request response.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- TENCENT TECHNOLOGY (SHENZHEN) CO LTD
- Filing Date
- 2021-07-14
- Publication Date
- 2026-05-19
AI Technical Summary
General-purpose search engines suffer from problems such as high thread synchronization overhead, resource waste due to thread suspension, and low request processing efficiency during task processing.
By employing a combination of worker threads, executor threads, scheduling coroutines, and worker coroutines, the task flow and execution order are determined. Other tasks can be executed by interrupting coroutines, making full use of thread resources and reducing overall resource consumption.
It improved request processing speed, reduced business processing time, and enhanced the user's search experience.
Smart Images

Figure CN115617467B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and provides a task processing method, apparatus, electronic device, and storage medium. Background Technology
[0002] With the continuous development of search engine technology, in order to meet diverse search needs, general-purpose search engines have been gradually and widely used.
[0003] In related technologies, after a general search engine obtains a retrieval request, it typically performs the following process: sending retrieval requests to various vertical search systems in parallel, and then presenting the vertical search results returned by each vertical search system. Vertical search is a specialized search service designed for a specific field, such as WeChat official account search, article search, and video search.
[0004] Typically, a general-purpose search engine divides the above process into various stages, and then executes each stage in the order they are executed. During the execution of each stage, the general-purpose search engine distributes the tasks corresponding to the current stage to threads for task processing.
[0005] However, using threads for task processing increases thread synchronization overhead. In addition, during task processing, situations such as thread suspension can lead to wasted thread resources and low request processing efficiency. Summary of the Invention
[0006] This application provides a request processing method, apparatus, electronic device, and storage medium to reduce resource consumption while improving request processing speed.
[0007] In a first aspect, embodiments of this application provide a task processing method, including:
[0008] In response to a retrieval request triggered by a target object, based on a preset task flow, a worker thread is invoked to determine each task corresponding to the task flow and the execution order between the tasks.
[0009] Invoke the executor, obtain the tasks and their execution order, and assign the tasks to at least one executor thread in sequence based on the execution order;
[0010] The scheduling coroutine contained in the at least one executor thread is invoked to assign the allocated task to at least one worker coroutine contained in the at least one executor thread for processing.
[0011] Secondly, embodiments of this application provide a task processing apparatus, including:
[0012] The determining unit is used to respond to a retrieval request triggered by a target object, and based on a preset task flow, to call a worker thread to determine each task corresponding to the task flow and to determine the execution order between the tasks.
[0013] The first allocation unit is used to invoke the executor, obtain the various tasks and the execution order, and allocate the various tasks to at least one executor thread in sequence based on the execution order;
[0014] The second allocation unit is used to call the scheduling coroutine contained in the at least one executor thread to assign the allocated task to at least one working coroutine contained in the at least one executor thread for processing.
[0015] Optionally, it also includes a synchronization unit, used to perform at least one of the following operations based on a preset task state synchronization method during the processing of at least one working coroutine:
[0016] Synchronize the task status of the worker thread and the at least one executor thread;
[0017] Synchronize the task status of the scheduling coroutines and worker coroutines contained in each executor thread;
[0018] Synchronize the task state for each executor thread and the worker coroutines contained in each executor thread.
[0019] Thirdly, embodiments of this application provide an electronic device, including a processor and a memory, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor performs the steps of the above-described task processing method.
[0020] Fourthly, embodiments of this application provide a computer-readable storage medium including a computer program, which, when run on an electronic device, causes the electronic device to perform the steps of the above-described task processing method.
[0021] In this embodiment of the application, after the task processing device obtains the retrieval request, it calls the worker thread to determine each task corresponding to the preset task flow and the execution order between each task. Then, it calls the executor to distribute each task to at least one executor thread based on the execution order. Next, the executor thread assigned to the task calls the included scheduling coroutine to assign the assigned task to the worker coroutine for processing.
[0022] Because coroutines can be interrupted to execute other tasks during task processing and then return to continue the original operation after completion, the processing resources of each thread can be fully utilized during task processing, reducing overall resource consumption. In addition, determining the execution order of each task based on the preset task flow can reduce the time spent on business processing and improve the user's search experience.
[0023] Other features and advantages of this application will be set forth in the description which follows, and will be apparent in part from the description, or may be learned by practicing the application. The objectives and other advantages of this application may be realized and obtained by means of the structures particularly pointed out in the written description, claims, and drawings. Attached Figure Description
[0024] The accompanying drawings, which are included to provide a further understanding of this application and form part of this application, illustrate exemplary embodiments and are used to explain this application, but do not constitute an undue limitation of this application. In the drawings:
[0025] Figure 1 This is a schematic diagram illustrating a possible application scenario provided in the embodiments of this application;
[0026] Figure 2 This is a schematic diagram illustrating the relationships between worker threads, executors, executor threads, and scheduling coroutines in a task processing device provided in this application embodiment.
[0027] Figure 3 This is a flowchart illustrating the task processing method provided in the embodiments of this application;
[0028] Figure 4A This is a schematic diagram of a shared queue provided in an embodiment of this application;
[0029] Figure 4B This is a schematic diagram of another shared queue provided in the embodiments of this application;
[0030] Figure 5A This is a schematic diagram illustrating task allocation during single-threaded execution, provided in an embodiment of this application.
[0031] Figure 5B This is a schematic diagram illustrating another task allocation method for multi-threaded execution provided in this application embodiment;
[0032] Figure 6 This is a schematic diagram of a local queue provided in an embodiment of this application;
[0033] Figure 7 This is a schematic diagram of a task execution graph provided in an embodiment of this application;
[0034] Figure 8 This is a flowchart illustrating the time consumption during parallel processing.
[0035] Figure 9 This is a logical diagram of a task processing flow provided in an embodiment of this application;
[0036] Figure 10 This is a schematic diagram of an interface provided in an embodiment of this application;
[0037] Figure 11 This is a schematic diagram of the composition structure of a task processing device provided in the embodiments of this application;
[0038] Figure 12 This is a schematic diagram of the hardware composition structure of an electronic device provided in an embodiment of this application;
[0039] Figure 13 This is a schematic diagram of the hardware composition structure of a terminal device provided in the embodiments of this application. Detailed Implementation
[0040] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of this application will be clearly and completely described below with reference to the accompanying drawings of the embodiments of this application. Obviously, the described embodiments are only some embodiments of the technical solutions of this application, and not all embodiments. Based on the embodiments recorded in this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the technical solutions of this application.
[0041] The following describes some of the concepts involved in the embodiments of this application.
[0042] 1. Thread: A thread is the smallest unit of program execution flow. A standard thread consists of a thread identifier, a current instruction pointer, a set of registers, and a stack.
[0043] 2. Coroutine: A coroutine is a program component that can be interrupted during execution to perform other tasks, and then return to continue the original operation after completion; it can be understood as two or more programs working together.
[0044] With the continuous development of search engine technology, general-purpose search engines have been widely adopted to meet diverse search needs. After receiving a search request, a general-purpose search engine typically performs the following process: sending search requests in parallel to various vertical search systems; and then, after receiving the vertical search results returned by each system, presenting the respective vertical search results. Vertical search refers to specialized search services tailored to a specific field, such as WeChat official account search, article search, and video search.
[0045] Typically, general-purpose search engines divide the above processing into various stages and then execute each stage in the order they occur. During the execution of each stage, the search engine distributes the tasks corresponding to that stage to threads for processing. However, using threads for task processing increases thread synchronization overhead. Furthermore, situations such as thread suspension during task processing can lead to wasted thread resources and low request processing efficiency.
[0046] Due to the problems of wasted thread resources and low request processing efficiency in related technologies, in this embodiment of the application, the task processing device includes a worker thread, an executor, an executor thread, a scheduling coroutine, and a worker coroutine. After obtaining a retrieval request, the worker thread is invoked to determine each task corresponding to the preset task flow and the execution order between each task. Then, the executor is invoked to distribute each task to at least one executor thread based on the execution order. Next, the executor thread assigned to the task invokes the included scheduling coroutine to assign the assigned task to the worker coroutine for processing.
[0047] Because coroutines can be interrupted to execute other tasks during task processing and then return to continue the original operation after completion, the processing resources of each thread can be fully utilized during task processing, reducing overall resource consumption. In addition, determining the execution order of each task based on the preset task flow can reduce the time spent on business processing and improve the user's search experience.
[0048] The preferred embodiments of this application are described below with reference to the accompanying drawings. It should be understood that the preferred embodiments described herein are for illustration and explanation only and are not intended to limit this application. Furthermore, the embodiments and features in the embodiments of this application can be combined with each other without conflict.
[0049] See Figure 1 As shown, it is a schematic diagram of a possible application scenario in the embodiments of this application.
[0050] The application scenario diagram includes two terminal devices 110 and one server 130. The application operation interface 120 can be accessed through terminal device 110. Terminal device 110 and server 130 can communicate via a communication network.
[0051] In this embodiment, the terminal device 110 is an electronic device used by a user, which may be, but is not limited to, a personal computer, mobile phone, tablet computer, laptop, e-book reader, smart home device, smart voice interaction device, in-vehicle terminal, etc. Each terminal device 110 communicates with the server 130 through a communication network.
[0052] In one alternative implementation, the communication network is a wired network or a wireless network, so the terminal device 110 and the server 130 can be directly or indirectly connected via wired or wireless communication, which is not limited herein.
[0053] Server 130 can be a standalone physical server 130, a server cluster or distributed system composed of multiple physical servers 130, or a cloud server 130 that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, content delivery networks (CDN), and big data and artificial intelligence platforms.
[0054] In this embodiment, the task processing device can be deployed on either the terminal device 110 or the server 130. When the task processing device is deployed on the terminal device 110, the terminal device 110 processes the task in response to a search request triggered by the user in the operation interface. When the task processing device is deployed on the server 130, after the user triggers a search operation in the operation interface of the terminal device 110, the terminal device 110 sends a search request to the server 130 in response to the search operation. Accordingly, the server 130 processes the task after receiving the search request from the terminal device.
[0055] See Figure 2 The diagram shown is a structural schematic of a task processing device provided in an embodiment of this application. The task processing device includes at least worker threads and executors. The executor includes at least one executor thread, and each executor thread includes a scheduling coroutine and at least one worker coroutine.
[0056] The system comprises several components: worker threads, executors, schedulers, and workers. Worker threads are used to invoke and determine the tasks within a predefined task flow, as well as the execution order of these tasks. Executors acquire the tasks and their execution order, and then assign each task to at least one executor thread based on that order. Schedulers distribute the tasks assigned by the at least one executor thread to at least one worker coroutine within that executor thread for processing. Worker coroutines then process the assigned tasks. The specific roles of worker threads, executors, executor threads, schedulers, and worker coroutines, as well as their interactions, are detailed below.
[0057] See Figure 3 As shown, this is a flowchart illustrating a task processing method provided in an embodiment of this application. This method is applied to a task processing device, and the specific task processing flow is as follows:
[0058] S301. In response to the retrieval request triggered by the target object, the task processing device calls the working thread based on the preset task flow, determines each task corresponding to the task flow, and determines the execution order between each task.
[0059] If the task processing device is a terminal device, then in response to a user-triggered search request triggered in the user interface, the terminal device, based on a preset task flow, invokes worker threads to determine the various tasks corresponding to the task flow and the execution order of these tasks. If the task processing device is a server, then after a user triggers a search operation in the terminal device's user interface, the terminal device, in response to the search operation, sends a search request to the server. Correspondingly, upon receiving the search request from the terminal device, the server, based on a preset task flow, invokes worker threads to determine the various tasks corresponding to the task flow and the execution order of these tasks.
[0060] It should be noted that in this embodiment of the application, the task flow can be set according to the specific scenario. In the following text, only the search scenario is used as an example for explanation.
[0061] For example, the preset search task flow is as follows:
[0062] First, based on other relevant information, the intent analysis service is invoked to identify the intent of the search terms input by the target user, yielding the intent identification results. This other relevant information includes, but is not limited to, the user's geographic location information, user profile information, and the contextual information of the search terms. The intent analysis service includes, but is not limited to, word segmentation, intent recognition, and entity recognition.
[0063] Then, based on the intent recognition results, each vertical search engine is invoked to perform a search, and the corresponding vertical search results are received.
[0064] Next, the mixed search service is invoked to sort the received vertical search results.
[0065] Finally, supplementary information such as relationship chain information and profile picture information is obtained, and based on this supplementary information, the sorted vertical search results are displayed.
[0066] In response to a retrieval request triggered by the target object, the task processing device, based on a preset search task flow, invokes worker threads to determine the various tasks corresponding to the task flow: Task A1, Task A2, Task A3, Task A4, Task A5, Task A6, and Task A7. The content of each task is as follows:
[0067] Task A1: Obtain the search terms for the target object input contained in the search request.
[0068] Task A2: Obtain other relevant information.
[0069] Task A3: Based on other relevant information, call the intent analysis service to perform intent recognition on the search terms input by the target object and obtain the intent recognition results.
[0070] Task A4: Based on the intent recognition results, call each vertical search engine to perform a search and receive the corresponding vertical search results.
[0071] Task A5: Call the mixed search service to sort the received vertical search results.
[0072] Task A6: Obtain supplementary information such as relationship chain information and profile picture information.
[0073] Task A7: Based on the supplementary information, process the sorted vertical search results to obtain the final search results.
[0074] The task processing device determines the execution order of the tasks as follows: tasks A1 and A2 are prerequisites for task A3; task A3 is a prerequisite for task A4; task A4 is a prerequisite for task A5; tasks A4 and A6 are prerequisites for task A5; and task A5 is a prerequisite for task A7. It should be noted that, in this embodiment, taking A1 and A3 as examples, the execution of A3 depends on the execution result of A1; therefore, A1 is called a prerequisite for A3.
[0075] In some embodiments, the worker thread invoked by the task processing device can be any idle worker thread in the worker thread pool. A thread pool is a form of multithreaded processing. When the system starts, the thread pool creates a large number of idle threads. After the thread pool receives a task, it starts a thread to execute the task. After the task is completed, the thread returns to the thread pool as an idle thread, waiting to execute the next task.
[0076] S302, The task processing device calls the executor to obtain each task and its execution order, and assigns each task to at least one executor thread in sequence based on the execution order.
[0077] Since an executor contains multiple executor threads, and too many threads can lead to scheduling overhead, thus affecting cache locality and overall performance, this embodiment uses a thread pool to maintain multiple executor threads within an executor to avoid the cost of creating and destroying threads when processing short-lived tasks. The thread pool can collectively manage and schedule the executor threads. Users can automatically or manually set the number of executor threads in the thread pool to control the number of running executor threads, thereby avoiding system congestion or waste of system resources. Furthermore, since there is an execution order among the tasks, after adding the target starting task to the shared queue, at least one thread in the executor executes each task starting from the target starting task according to the obtained target starting task and the execution order.
[0078] Specifically, the task processing device calls the executor to determine at least one target starting task from among the various tasks based on the execution order, and adds at least one target starting task to the shared queue;
[0079] Invoke at least one executor thread to retrieve at least one target initiation task from the shared queue.
[0080] In this embodiment, each executor thread in the executor shares a shared queue. To ensure efficiency when executor threads compete to acquire resources, the shared queue can be implemented without locks; that is, the shared queue is a lock-free queue. The lock-free implementation principle can be based on Compare and Swap (CAS) mechanisms, Fetch and Add (FAA) mechanisms, etc., and this application does not limit it to these.
[0081] To support different processing capabilities, when the task processing device calls the executor and adds the target starting task to the shared queue, if single-threaded execution is supported, a target starting task can be placed in the shared queue, and an executor thread can obtain the target starting task for subsequent processing. If multi-threaded parallel execution is supported, nodes without prior dependencies are placed in the shared queue, so that multiple executor threads can obtain tasks for subsequent processing.
[0082] Specifically, based on the preset configuration information, there are, but are not limited to, the following two possible methods:
[0083] Method 1: If the preset configuration information indicates support for single-threaded execution, the task processing device will take the first task executed in each task as a target starting task based on the execution order. Then, the task processing device will call executor thread x to obtain the target starting task from the shared queue. Executor thread x can be any one of multiple executor threads.
[0084] For example, see Figure 4A As shown, the preset configuration information indicates support for single-threaded execution. Therefore, the task processing device, based on the execution order, takes the first task to be executed among all tasks, task A1, as a target starting task. Then, it calls executor thread 1 to retrieve task A1 from the shared queue.
[0085] Method 2: If the preset configuration information indicates support for multi-threaded execution, the task processing device, based on the execution order, will select at least one task among the tasks that has no prior dependencies as at least one target starting task. Then, the task processing device will invoke at least one executor thread to obtain at least one target starting task from the shared queue.
[0086] For example, see Figure 4B As shown, the task processing device, based on the execution order, takes the tasks without prior dependencies—tasks A1, A2, and A6—as three target starting tasks. Then, it calls executor thread 1 to retrieve task A1 from the shared queue, executor thread 2 to retrieve task A2 from the shared queue, and executor thread 3 to retrieve task A6 from the shared queue.
[0087] S303, The task processing device calls a scheduling coroutine contained in at least one executor thread to assign the assigned task to at least one working coroutine contained in at least one executor thread for processing.
[0088] It should be noted that in this embodiment, a coroutine pool is used to maintain multiple executor threads within an executor thread. For example, the coroutine pool can be implemented using coroutine libraries such as libco and libaco. This embodiment does not limit this, and the following description will only use libco as an example.
[0089] In some embodiments, since a task may consist of multiple subtasks, in order to improve processing efficiency, the task processing device may assign executable subtasks to the working coroutine of executor thread x (executor thread x is any one of at least one executor thread) for task allocation during the process of calling the scheduling coroutine of executor thread x (executor thread x is any one of at least one executor thread) for processing, or assign executable subtasks to the working coroutines of other executor threads for processing.
[0090] Taking executor thread x as an example, specifically, based on the task attribute information carried in the subtask, there are, but are not limited to, the following two situations:
[0091] Scenario 1: If, within executor thread x, the task x assigned by the scheduling coroutine x contains subtask xn, and the task attribute information carried by subtask xn indicates that subtask xn supports single-threaded execution, then the task processing device calls the scheduling coroutine x to assign subtask xn to at least one worker coroutine contained in executor thread x for processing. Here, subtask xn can be any subtask contained in task x.
[0092] It should be noted that, in this embodiment of the application, the task processing device can call the scheduling coroutine x to parse the task x and determine the subtasks contained in the task.
[0093] Taking task A4 as an example, task A4 is: based on the intent recognition results, call each vertical search engine to perform a search and receive the corresponding vertical search results. Task A4 includes at least subtasks A4-1 and A4-2. Subtask A4-1 is: based on the intent recognition results, call the WeChat official account search engine to perform a search and receive the WeChat official account search results. Subtask A4-2 is: based on the intent recognition results, call the video search engine to perform a search and receive the video search results.
[0094] See Figure 5A As shown, assuming that task A4 is assigned to executor thread 1, the task processing device calls the scheduling coroutine of executor thread 1 to assign subtask A4-1 to working coroutine A in executor thread 1 for processing, and subtask A4-2 to working coroutine B in executor thread 1 for processing.
[0095] Scenario 2: If, within executor thread x, the task x assigned by the scheduling coroutine x contains a subtask xn, and the task attribute information carried by subtask xn indicates that subtask xn supports multi-threaded execution, then the task processing device calls the scheduling coroutine x of executor thread x to assign subtask xn to at least one working coroutine contained in other executor threads for processing. Here, subtask xn can be any subtask contained in task x.
[0096] Taking task A4 as an example, please refer to... Figure 5B As shown, task A4 includes at least subtasks A4-1 and A4-2. Assume that task A4 is assigned to executor thread 1. The task processing device calls the scheduling coroutine of executor thread 1 to assign subtask A1-1 to working coroutine A of executor thread 1 for processing, and subtask A1-2 to working coroutine B of executor thread 2 for processing.
[0097] To ensure efficiency and thread safety in task allocation across different threads, the scheduling coroutine in the task processing device can allocate tasks based on local queues. Specifically, the task processing device invokes at least one executor thread to add the allocated tasks to the local queues corresponding to each of the at least one executor thread; and invokes the scheduling coroutine contained in the at least one executor thread to retrieve the allocated tasks from the corresponding local queues.
[0098] For example, see Figure 6 As shown, the task processing device calls executor thread 1 to add the assigned task A1 to the local queue corresponding to executor thread 1. Then, it calls the scheduling coroutine contained in executor thread 1 to retrieve the assigned task A1 from the local queue corresponding to executor thread 1.
[0099] In some embodiments, the task processing apparatus can allocate tasks between executor threads based on a shared queue, and simultaneously allocate tasks between worker coroutines based on local threads. During the process where the task processing apparatus calls the scheduling coroutine x of executor thread x to assign a subtask xn to at least one worker coroutine within another executor thread for processing, the task processing apparatus calls the scheduling coroutine x of executor thread x to place the subtask xn into the shared queue. Then, other executor threads retrieve the subtask xn from the shared queue, and the task processing apparatus then calls the scheduling coroutines of the other executor threads to assign the subtask xn to at least one worker coroutine within those other executor threads for processing. Since the task allocation between executor threads based on a shared queue and the task allocation between worker coroutines based on local threads are the same as the task allocation process described above, they will not be repeated here.
[0100] In some embodiments, to coordinate the execution states of various tasks and subtasks, the task processing device can perform task state synchronization across threads and coroutines during executor execution. Specifically, during processing in at least one working coroutine, the task processing device performs task state synchronization on one or more of the following objects based on a preset task state synchronization method:
[0101] Worker thread and executor thread x;
[0102] The scheduler coroutines and worker coroutines contained in executor thread x;
[0103] Executor thread x, and the worker coroutines contained in executor thread x.
[0104] For example, the task processing device performs task state synchronization on worker thread 1 and executor thread 1, on the scheduling coroutine and worker coroutine A contained in executor thread 1, on the scheduling coroutine and worker coroutine B contained in executor thread 1, on the task state synchronization on executor thread 1 and worker coroutine A contained in executor thread 1, and on the task state synchronization on executor thread 1 and worker coroutine B contained in executor thread 1.
[0105] It should be noted that the preset task state synchronization method can be either a programming language-encapsulated synchronization method or a coroutine library-encapsulated synchronization method, as shown in Table 1. The programming language-encapsulated synchronization method is implemented based on std::mutex and std::condition_variable. std::mutex is used to create mutexes, and std::condition_variable contains classes and functions related to condition variables. The coroutine library-encapsulated synchronization method is based on libco's write event, switching the coroutine environment while waiting. After comparing the advantages and disadvantages of the language-encapsulated synchronization method and the coroutine library-encapsulated synchronization method, since the coroutine library-encapsulated synchronization method can support both coroutine and thread environments simultaneously, and can perform cross-thread notification and synchronization, it is the preferred choice for condition notification and event synchronization.
[0106] Table 1 Synchronization Methods
[0107]
[0108] To reduce the processing time of concurrent requests and improve the background request processing speed, thereby further enhancing task processing efficiency, the task processing device can also allocate tasks based on a directed acyclic graph (DAG). Specifically, based on each task and its execution order, the task processing device calls worker threads to generate a task execution graph. Each node in the task execution graph represents a task, and the connections between nodes represent the execution order. Correspondingly, the task processing device calls the executor to obtain the task execution graph and, based on the graph, retrieves the tasks and their execution order.
[0109] For example, see Figure 7 As shown, the task processing device calls worker threads based on each task and its execution order to generate a task execution graph. Each node in the task execution graph is used to represent each task: task A1, task A2, task A3, task A4, task A5, task A6, and task A7. The connection relationship between each node is used to represent the execution order between each task.
[0110] It should be noted that the task processing device, based on the task execution graph, can use, but is not limited to, topological sorting to obtain the individual tasks and their execution order. Topological sorting is used to determine the dependencies between tasks based on the connections between nodes in the task execution graph; therefore, the execution order obtained using topological sorting is not unique.
[0111] For example, see Figure 7 As shown, the task processing device calls the executor based on the task execution graph, and the execution order 1 using topological sorting is: Task A1 → Task A2 → Task A3 → Task A4 → Task A6 → Task A5 → Task A7.
[0112] For example, see Figure 7 As shown, the task processing device calls the executor based on the task execution graph, and the execution order 1 using topological sorting is: Task A1 → Task A2 → Task A6 → Task A3 → Task A4 → Task A5 → Task A7.
[0113] Since a task can consist of multiple subtasks, a node in the execution graph can correspond to a subgraph, and each node in the subgraph corresponds to a subtask. When the task processing device calls the scheduling coroutine of executor thread x to allocate tasks, it can use the scheduling coroutine of executor thread x to parse the subgraph and determine the subtasks contained in the task.
[0114] The task processing unit uses a directed acyclic graph (DAG) to handle requests, employing a topological sorting algorithm to traverse the nodes in the task execution graph. This minimizes dependencies between tasks, thereby reducing task scheduling time and overall task processing latency. See also... Figure 8As shown, if a layered concurrent execution approach is used, the entire process includes a query processing layer, a searcher layer, a pre-response layer, and a response layer. After receiving the search request, in the query processing layer, both the WeChat Official Account business and the video business need to perform query checks and query processing, which take 1 millisecond (ms) and 7 milliseconds, respectively. In the searcher layer, the WeChat Official Account business needs to perform a search, which takes 28 milliseconds, while the video business needs to perform pre-processing and searching, which takes 10 milliseconds. In the pre-response layer, the video business needs to perform duplication and obtain header information, which take 14 milliseconds and 10 milliseconds, respectively. Finally, the response layer performs the response. It can be seen that the entire process takes a total of 49 milliseconds to execute. However, after constructing a directed acyclic graph for all nodes, the overall execution time can be reduced to 35 milliseconds, a reduction of 30%, which is a very significant improvement.
[0115] After the task processing device calls the executor to determine that each node in the task execution graph has been completed, i.e., after each task has been processed, it can also notify the worker threads that the task is complete through synchronization variables. Synchronization variables can be, but are not limited to, counters, identifiers, etc.
[0116] In some embodiments, to fully adapt to various execution environments and maximize the utilization efficiency of physical hardware, the task processing device uses a thread pool mode in compute-intensive scenarios to fully utilize the computing power of multi-core CPUs and accelerate execution speed; in input / output (IO)-intensive scenarios, the task processing device uses a coroutine pool mode to fully utilize the computing power of single-core CPUs and reduce CPU idle waiting time.
[0117] It should be noted that the thread pool mode refers to the task processing device assigning each task to at least one executor thread in sequence and then calling at least one executor thread to directly process the task, without assigning the task to the corresponding worker coroutine for processing. In contrast, the coroutine pool mode refers to the task processing device assigning each task to at least one executor thread in sequence and then executing S303.
[0118] In some embodiments, to quickly respond to search requests, the task processing device can employ different caching granularities to cache the vertical search results returned by various vertical search engines. The caching granularity refers to caching all vertical search results or caching only a portion of them, and can be set according to the application scenario. In this way, the task processing device can reduce background processing time by refreshing the cache.
[0119] The present application will now be described with reference to a specific embodiment.
[0120] Taking the task processing device as a server as an example, see [link / reference]. Figure 9 As shown, the task processing device includes worker threads 1 to n, and an executor. The executor includes at least executor thread 1 and executor thread 2. Each executor thread contains a scheduling coroutine and multiple worker coroutines. The worker threads and executor threads can be implemented using thread pools, and the worker coroutines can be implemented using coroutine pools.
[0121] See Figure 10 As shown, the user enters the search term "xx technology" in the operation interface of the terminal device. In response to the search operation triggered by the user, the terminal device sends a corresponding search request to the server.
[0122] After receiving the retrieval request, the server, based on the preset task flow, calls worker thread 2 to determine the various tasks corresponding to the task flow: tasks A1-A7, and the execution order among tasks A1-A7, and generates... Figure 9 The task execution diagram is shown.
[0123] Then, the server calls the executor to obtain the task execution graph, and based on the task execution graph, obtains tasks A1-A7 and their execution order. Assuming that the preset configuration information indicates support for single-threaded execution based on the execution order, the target starting task is determined from tasks A1-A7 as task A1, and task A1 is added to the shared queue.
[0124] Next, the server invokes executor thread 2 to retrieve task A1 from the shared queue. The server uses the Central Processing Unit (CPU) to push and pop tasks onto or off the local queue of executor thread 2. After executor thread 2 retrieves task A1, the server invokes the scheduling coroutine contained in executor thread 2 to assign the assigned task A1 to the worker coroutine contained in executor thread 1 for processing.
[0125] Afterwards, the server calls the executor to determine that each node in the task execution graph has been completed, that is, after tasks A1 to A7 are completed, and then notifies worker thread 2 that the task is complete.
[0126] Finally, the task processing device calls worker thread 2 to return the search results to the terminal device, so that the terminal device can present the search results to the user.
[0127] Based on the same inventive concept, embodiments of this application provide a task processing device. For example... Figure 11As shown, this is a schematic diagram of the structure of the task processing device 1100, which may include:
[0128] The determining unit 1101 is used to respond to a retrieval request triggered by a target object, and based on a preset task flow, call a working thread to determine each task corresponding to the task flow and to determine the execution order between the tasks.
[0129] The first allocation unit 1102 is used to call the executor, obtain the various tasks and the execution order, and allocate the various tasks to at least one executor thread in sequence based on the execution order;
[0130] The second allocation unit 1103 is used to call the scheduling coroutine contained in the at least one executor thread to assign the allocated task to at least one working coroutine contained in the at least one executor thread for processing.
[0131] Optionally, after determining the tasks corresponding to the preset task flow by calling worker threads and determining the execution order of the tasks, the determining unit 1101 is further configured to:
[0132] Based on the tasks and the execution order, a worker thread is invoked to generate a task execution graph, wherein each node in the task execution graph represents a task, and the connection relationship between the nodes represents the execution order.
[0133] When the executor is invoked to obtain the various tasks and the execution order, the first allocation unit 1102 is further configured to:
[0134] The executor is invoked to obtain the task execution graph, and based on the task execution graph, the individual tasks and the execution order are obtained.
[0135] Optionally, when assigning each task to at least one executor thread based on the execution order, the first allocation unit 1102 is specifically used for:
[0136] The executor is invoked to determine at least one target starting task from the tasks based on the execution order, and the at least one target starting task is added to a shared queue.
[0137] Invoke at least one executor thread to retrieve the at least one target initiation task from the shared queue.
[0138] Optionally, when determining at least one target starting task from the tasks based on the execution order, the first allocation unit 1102 is specifically used for:
[0139] If the preset configuration information indicates support for single-threaded execution, then based on the execution order, the first task executed among the tasks is taken as a target starting task; or,
[0140] If the preset configuration information indicates that multi-threaded execution is supported, then based on the execution order, at least one task among the tasks that has no prior dependency relationship is taken as at least one target starting task.
[0141] Optionally, when the scheduling coroutine included in the at least one executor thread is invoked to assign the allocated task to at least one worker coroutine included in the at least one executor thread for processing, the second allocation unit 1103 is specifically used for:
[0142] If a task assigned by a scheduling coroutine in an executor thread contains a subtask, and the task attribute information carried in the subtask indicates that the subtask supports single-threaded execution, then the scheduling coroutine is invoked to assign the subtask to at least one worker coroutine in the executor thread for processing.
[0143] If a task assigned by a scheduling coroutine in an executor thread contains a subtask, and the task attribute information carried in the subtask indicates that the subtask supports multi-threaded execution, then the scheduling coroutine is invoked to assign the subtask to at least one worker coroutine in other executor threads for processing.
[0144] Optionally, after the invoking executor, obtaining the various tasks and the execution order, and assigning the various tasks sequentially to at least one executor thread based on the execution order, and before the invoking the scheduling coroutine contained in the at least one executor thread to assign the assigned tasks to at least one worker coroutine contained in the at least one executor thread for processing, the second allocation unit 1103 is further configured to:
[0145] Invoke at least one executor thread and add the assigned task to the local queue corresponding to each of the at least one executor thread;
[0146] The scheduling coroutine contained in the at least one executor thread is invoked to retrieve the assigned task from the corresponding local queue.
[0147] Optionally, it also includes a synchronization unit 1104, which is used to perform at least one of the following operations based on a preset task state synchronization method during the processing of at least one working coroutine:
[0148] Synchronize the task status of the worker thread and the at least one executor thread;
[0149] Synchronize the task status of the scheduling coroutines and worker coroutines contained in each executor thread;
[0150] Synchronize the task state for each executor thread and the worker coroutines contained in each executor thread.
[0151] For ease of description, the above sections are divided into modules (or units) according to their functions and described separately. Of course, in implementing this application, the functions of each module (or unit) can be implemented in one or more software or hardware components.
[0152] Regarding the apparatus in the above embodiments, the specific manner in which each unit executes the request has been described in detail in the embodiments related to the method, and will not be elaborated here.
[0153] Those skilled in the art will understand that various aspects of this application can be implemented as a system, method, or program product. Therefore, various aspects of this application can be specifically implemented in the following forms: a completely hardware implementation, a completely software implementation (including firmware, microcode, etc.), or a combination of hardware and software implementations, collectively referred to herein as a "circuit," "module," or "system."
[0154] Having introduced the task processing method and apparatus according to exemplary embodiments of this application, we will now introduce an electronic device according to another exemplary embodiment of this application.
[0155] Figure 12 This is a block diagram illustrating an electronic device 1200 according to an exemplary embodiment, the device comprising:
[0156] Processor 1210;
[0157] Memory 1220 is used to store executable instructions of processor 1210;
[0158] The processor 1210 is configured to execute instructions to implement the task processing method in this embodiment of the present disclosure, such as the steps shown in the task processing.
[0159] In an exemplary embodiment, a storage medium including operations is also provided, such as a memory 1220 including operations, the operations of which can be executed by a processor 1210 of an electronic device 1200 to complete the above-described method. Optionally, the storage medium may be a non-transitory computer-readable storage medium, such as a read-only memory (ROM), random access memory (RAM), compact disk read-only memory (CD-ROM), magnetic tape, floppy disk, and optical data storage device.
[0160] Based on the same inventive concept, see [reference] Figure 13 As shown in the figure, this application embodiment also provides a terminal device 1300, which can be an electronic device such as a smartphone, tablet computer, laptop computer or PC.
[0161] The terminal device 1300 includes a display unit 1340, a processor 1380, and a memory 1320. The display unit 1340 includes a display panel 1341, used to display information input by the user or information provided to the user, as well as various operating interfaces of the terminal device 1300. In this embodiment, it is mainly used to display the operating interfaces and shortcut windows of applications installed on the terminal device 1300. Optionally, the display panel 1341 can be configured using an LCD (Liquid Crystal Display) or OLED (Organic Light-Emitting Diode) display.
[0162] The processor 1380 is used to read a computer program and then execute the methods defined by the computer program. For example, the processor 1380 reads an application and runs the application on the terminal device 1300, displaying an operation interface on the display unit 1340. The processor 1380 may include one or more general-purpose processors, and may also include one or more DSPs (Digital Signal Processors) for performing related operations to implement the technical solutions provided in the embodiments of this application.
[0163] The memory 1320 generally includes main memory and secondary storage. Main memory can be RAM, ROM, and cache, etc. Secondary storage can be a hard disk, optical disk, USB flash drive, floppy disk, or tape drive, etc. The memory 1320 is used to store computer programs and other data. The computer programs include application programs, and the other data may include data generated after the operating system or application programs are run, including system data (e.g., operating system configuration parameters) and user data. In this embodiment, program instructions are stored in the memory 1320, and the processor 1380 executes the program instructions in the memory 1320 to implement the method described above.
[0164] In addition, the terminal device 1300 may also include a display unit 1340 for receiving input digital information, character information, or contact touch operations / non-contact gestures, and generating signal inputs related to user settings and function control of the terminal device 1300. Specifically, in this embodiment, the display unit 1340 may include a display panel 1341. The display panel 1341, for example, is a touch screen, which can collect touch operations performed by the user on or near it (such as operations performed by the user using a finger, stylus, or any suitable object or accessory on or near the display panel 1341), and drive the corresponding connection device according to a pre-set program. Optionally, the display panel 1341 may include two parts: a touch detection device and a touch controller. The touch detection device detects the user's touch position and the signal generated by the touch operation, and transmits the signal to the touch controller; the touch controller receives touch information from the touch detection device, converts it into touch point coordinates, sends it to the processor 1380, and can receive and execute commands sent by the processor 1380. In this embodiment, if a user selects a control in the operation interface, the touch detection device in the display panel 1341 detects the touch operation and sends the signal corresponding to the detected touch operation to the touch controller. The touch controller converts the signal into touch point coordinates and sends them to the processor 1380. The processor 1380 determines the control selected by the user based on the received touch point coordinates.
[0165] The display panel 1341 can be implemented using various types such as resistive, capacitive, infrared, and surface acoustic wave. In addition to the display unit 1340, the terminal device 1300 may also include an input unit 1330, which may include, but is not limited to, one or more of the following: a physical keyboard, function keys (such as volume control buttons, power buttons, etc.), a trackball, a mouse, and a joystick. Figure 13 The example given is that the input unit 1330 includes an image input device 1331 and other input devices 1332.
[0166] In addition to the above, the terminal device 1300 may also include a power supply 1390 for powering other modules, an audio circuit 1360, a near-field communication module 1370, and an RF circuit 1310. The terminal device 1310 may also include one or more sensors 1350, such as an accelerometer, a light sensor, a pressure sensor, etc. The audio circuit 1360 specifically includes a speaker 1361 and a microphone 1362, etc. For example, if a user can use voice control, the terminal device 1300 can collect the user's voice through the microphone 1362, control the device based on the user's voice, and play corresponding prompts through the speaker 1361 when necessary.
[0167] Based on the same inventive concept, this application also provides a computer program product or computer program, which includes computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform the task processing methods provided in the various optional implementations of the above embodiments.
[0168] In some possible implementations, various aspects of the task processing method provided in this application may also be implemented in the form of a program product, which includes a computer program. When the program product is run on a computer device, the computer program is used to cause the computer device to perform the steps in the task processing method according to the various exemplary embodiments of this application described above. For example, the computer device may perform the steps as shown in the task processing.
[0169] The program product may employ any combination of one or more readable media. A readable medium may be a readable signal medium or a readable storage medium. A readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of readable storage media (a non-exhaustive list) include: electrical connections having one or more wires, portable disks, hard disks, RAM, ROM, erasable programmable read-only memory (EPROM or flash memory), optical fibers, CD-ROMs, optical storage devices, magnetic storage devices, or any suitable combination thereof.
[0170] The program product of the embodiments of this application may be in the form of a CD-ROM and include program code, and may run on a computing device. However, the program product of this application is not limited thereto. In this document, the readable storage medium may be any tangible medium that contains or stores a program that may be used by or in conjunction with a command execution system, apparatus, or device.
[0171] A readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, carrying readable program code. Such propagated data signals may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. The readable signal medium may also be any readable medium other than a readable storage medium, capable of transmitting, propagating, or transmitting a program for use by or in conjunction with a command execution system, apparatus, or device. Although preferred embodiments of this application have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments as well as all changes and modifications falling within the scope of this application.
[0172] Obviously, those skilled in the art can make various modifications and variations to this application without departing from the spirit and scope of this application. Therefore, if such modifications and variations fall within the scope of the claims of this application and their equivalents, this application also intends to include such modifications and variations.
Claims
1. A task processing method, characterized in that, The method includes: In response to a retrieval request triggered by a target object, based on a preset task flow, a worker thread is invoked to determine each task corresponding to the task flow and the execution order between the tasks. Invoke the executor, obtain the tasks and their execution order, and assign the tasks to at least one executor thread in sequence based on the execution order; The scheduling coroutine contained in the at least one executor thread is invoked to assign the allocated task to at least one worker coroutine contained in the at least one executor thread for processing.
2. The method as described in claim 1, characterized in that, After the process of invoking worker threads based on a preset task flow, determining the tasks corresponding to the task flow, and determining the execution order of the tasks, the method further includes: Based on the tasks and the execution order, a worker thread is invoked to generate a task execution graph, wherein each node in the task execution graph represents a task, and the connection relationship between the nodes represents the execution order. The invocation of the executor, obtaining the various tasks and the execution order, includes: The executor is invoked to obtain the task execution graph, and based on the task execution graph, the individual tasks and the execution order are obtained.
3. The method as described in claim 1 or 2, characterized in that, The step of assigning each task sequentially to at least one executor thread based on the execution order includes: The executor is invoked to determine at least one target starting task from the tasks based on the execution order, and the at least one target starting task is added to a shared queue. Invoke at least one executor thread to retrieve the at least one target initiation task from the shared queue.
4. The method as described in claim 3, characterized in that, The step of determining at least one target starting task from the various tasks based on the execution order includes: If the preset configuration information indicates support for single-threaded execution, then based on the execution order, the first task executed among the tasks is taken as a target starting task; or, If the preset configuration information indicates that multi-threaded execution is supported, then based on the execution order, at least one task among the tasks that has no prior dependency relationship is taken as at least one target starting task.
5. The method as described in claim 1 or 2, characterized in that, The invocation of the scheduling coroutine contained in the at least one executor thread, and the assignment of the allocated task to at least one worker coroutine contained in the at least one executor thread for processing, includes: If a task assigned by a scheduling coroutine in an executor thread contains a subtask, and the task attribute information carried in the subtask indicates that the subtask supports single-threaded execution, then the scheduling coroutine is invoked to assign the subtask to at least one worker coroutine in the executor thread for processing. If a task assigned by a scheduling coroutine in an executor thread contains a subtask, and the task attribute information carried in the subtask indicates that the subtask supports multi-threaded execution, then the scheduling coroutine is invoked to assign the subtask to at least one worker coroutine in other executor threads for processing.
6. The method as described in claim 1 or 2, characterized in that, After the invoking of the executor, obtaining the various tasks and the execution order, and assigning the various tasks sequentially to at least one executor thread based on the execution order, and before the invoking of the scheduling coroutine contained in the at least one executor thread to assign the assigned tasks to at least one worker coroutine contained in the at least one executor thread for processing, the method further includes: Invoke at least one executor thread and add the assigned task to the local queue corresponding to each of the at least one executor thread; The scheduling coroutine contained in the at least one executor thread is invoked to retrieve the assigned task from the corresponding local queue.
7. The method as described in claim 1 or 2, characterized in that, Also includes: During the processing of at least one worker goroutine, based on a preset task state synchronization method, perform at least one of the following operations: Synchronize the task status of the worker thread and the at least one executor thread; Synchronize the task status of the scheduling coroutines and worker coroutines contained in each executor thread; Synchronize the task state for each executor thread and the worker coroutines contained in each executor thread.
8. A task processing device, characterized in that, include: The determining unit is used to respond to a retrieval request triggered by a target object, and based on a preset task flow, to call a worker thread to determine each task corresponding to the task flow and to determine the execution order between the tasks. The first allocation unit is used to invoke the executor, obtain the various tasks and the execution order, and allocate the various tasks to at least one executor thread in sequence based on the execution order; The second allocation unit is used to call the scheduling coroutine contained in the at least one executor thread to assign the allocated task to at least one working coroutine contained in the at least one executor thread for processing.
9. The apparatus as claimed in claim 8, characterized in that, After determining the tasks corresponding to the preset task flow, calling worker threads, and determining the execution order of the tasks, the determining unit is further configured to: Based on the tasks and the execution order, a worker thread is invoked to generate a task execution graph, wherein each node in the task execution graph represents a task, and the connection relationship between the nodes represents the execution order. When the executor is invoked to obtain the various tasks and the execution order, the first allocation unit is further configured to: The executor is invoked to obtain the task execution graph, and based on the task execution graph, the individual tasks and the execution order are obtained.
10. The apparatus as claimed in claim 8 or 9, characterized in that, When assigning each task to at least one executor thread based on the execution order, the first allocation unit is specifically used for: The executor is invoked to determine at least one target starting task from the tasks based on the execution order, and the at least one target starting task is added to a shared queue. Invoke at least one executor thread to retrieve the at least one target initiation task from the shared queue.
11. The apparatus as claimed in claim 10, characterized in that, When determining at least one target starting task from the tasks based on the execution order, the first allocation unit is specifically used for: If the preset configuration information indicates support for single-threaded execution, then based on the execution order, the first task executed among the tasks is taken as a target starting task; or, If the preset configuration information indicates that multi-threaded execution is supported, then based on the execution order, at least one task among the tasks that has no prior dependency relationship is taken as at least one target starting task.
12. The apparatus as claimed in claim 8 or 9, characterized in that, When the scheduling coroutine contained in the at least one executor thread is invoked to assign the allocated task to at least one worker coroutine contained in the at least one executor thread for processing, the second allocation unit is specifically used for: If a task assigned by a scheduling coroutine in an executor thread contains a subtask, and the task attribute information carried in the subtask indicates that the subtask supports single-threaded execution, then the scheduling coroutine is invoked to assign the subtask to at least one worker coroutine in the executor thread for processing. If a task assigned by a scheduling coroutine in an executor thread contains a subtask, and the task attribute information carried in the subtask indicates that the subtask supports multi-threaded execution, then the scheduling coroutine is invoked to assign the subtask to at least one worker coroutine in other executor threads for processing.
13. The apparatus as claimed in claim 8 or 9, characterized in that, After the invoking executor obtains the various tasks and the execution order, and assigns the various tasks sequentially to at least one executor thread based on the execution order, and before the invoking the scheduling coroutine contained in the at least one executor thread assigns the assigned tasks to at least one worker coroutine contained in the at least one executor thread for processing, the second allocation unit is further configured to: Invoke at least one executor thread and add the assigned task to the local queue corresponding to each of the at least one executor thread; The scheduling coroutine contained in the at least one executor thread is invoked to retrieve the assigned task from the corresponding local queue.
14. An electronic device, characterized in that, It includes a processor and a memory, wherein the memory stores a computer program that, when executed by the processor, causes the processor to perform the steps of any one of the methods described in claims 1 to 7.
15. A computer-readable storage medium, characterized in that, It includes a computer program that, when run on an electronic device, causes the electronic device to perform the steps of any of the methods described in claims 1 to 7.