Method and device for asynchronously obtaining multi-level request analysis result
By using the root node and child node state management mechanism in the method of asynchronously obtaining multi-level request parsing results, the problem of callback hell is solved, and more efficient asynchronous performance utilization and request processing are achieved.
Patent Information
- Application Number
- CN202410815738.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-24
- Publication Date
- 2024-10-01
AI Technical Summary
The existing method of asynchronously obtaining multi-level request parsing results will lead to callback hell when there are many requests, loss of asynchronous performance, and difficult to understand and maintain.
A method of asynchronously obtaining multi-level request parsing results is used to represent the completion of multi-level requests by creating a root node, and the first-level request is used as a child node of the root node. The node status is divided into PENDING, SCHEDULED, EXECUTED, RESOLVED and REJECTED, traverse the nodes to issue requests and update the status until the status of all nodes is RESOLVED, and summarize the results of requests at each level.
While making full use of asynchronous performance, it avoids the occurrence of callback hell and shortens the processing time of multi-level requests.
Smart Images

Figure CN118733919A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to a method for asynchronously obtaining multi-level request parsing results. Background Art
[0002] Multi-level requests mean that parsing results need to be obtained from one or more requests (called first-level requests). Some or all first-level requests also have subordinate requests (called second-level requests), and some or all second-level requests also have subordinate requests (called second-level requests), and so on. Ultimately, the parsing results of all requests need to be aggregated into the results required by the multi-level requests.
[0003] For example, a multi-level request is to obtain the summary information of a website A. Website A has a homepage, which links to 5 pages, some of which link to other pages, etc. Then the first-level request of the multi-level request is one, which is to obtain the summary information of the homepage of website A; there are five second-level requests, which respectively obtain the summary information of the 5 pages linked to the homepage of website A, etc. When the summary information of all nested pages is obtained, the summary information of website A is obtained.
[0004] The existing method of obtaining the parsing results of multi-level requests in an asynchronous manner is to request layer by layer in sequence to ensure that all requests have been completed and obtain the final correct result. This method not only loses asynchronous performance, but also forms callback hell when there are many requests. Callback hell refers to the situation in asynchronous programming where multiple layers of nested callback functions make the code difficult to understand, maintain, and expand. Therefore, it is necessary to obtain the parsing results of multi-level requests while fully utilizing asynchronous performance and avoiding callback hell.
[0005] Existing technologies, such as the Scrapy asynchronous request library in Python and Promise in JavaScript, need to issue requests layer by layer when obtaining the parsing results of multi-level requests. Each request is issued in sequence at a certain layer. Only after all requests at that layer obtain the parsing results can the request for the next layer be issued, and so on. This approach will form a callback hell when there are many requests, making code writing and maintenance extremely difficult and wasting asynchronous performance. Summary of the invention
[0006] The technical problem to be solved by this application is how to obtain the parsing results of multi-level requests while fully utilizing asynchronous performance and avoiding callback hell.
[0007] In order to solve the above technical problems, the present application proposes a method for asynchronously obtaining the parsing results of multi-level requests, including the following steps. Step S1: Create a root node to represent a multi-level request, and the state of the root node represents the completion status of the multi-level request. Step S2: Use the first-level request of the multi-level request as the first-level child node of the root node, and the state of each first-level child node represents the completion status of each first-level request; each node from the first-level node down has 5 states, namely: PENDING means that the request has not been issued yet, SCHEDULED means that the request has been issued but the result has not been obtained, EXECUTED means that the result has been obtained but the parsing is not completed, RESOLVED means that the result has been obtained and the parsing is completed, and REJECTED means that an exception occurred during the execution process. Step S3: Traverse all nodes downward from the root node, issue requests to all nodes in the PENDING state, and change the state of these nodes to SCHEDULED; when any node receives the result, the state of the node is changed from SCHEDULED to EXECUTED; if the received result indicates that a request has a lower level request, then add a new level of child nodes below the corresponding node; the state of the newly added child nodes represents the completion of the corresponding level request, which is initially PENDING; if the received result indicates that a request has no lower level request, then change the state of the node corresponding to the request from EXECUTED to RESOLVED; repeat step S3 until the state of all nodes except the root node is RESOLVED. Step S4: Summarize the results of requests at each level to obtain the final result of multi-level requests.
[0008] Furthermore, in step S1, the callback function after the multi-level request is completed, the context information shared during the operation, and the limit of the request depth are also saved.
[0009] Furthermore, in the step S1, the callback function receives the callback parameters at the time of initialization and the context information in the system.
[0010] Furthermore, in step S2, the root node has as many first-level child nodes as the multi-level requests have first-level requests.
[0011] Furthermore, in step S3, if an exception occurs during the result parsing of a request, the state of the node is changed to REJECTED; when any node is in the REJECTED state, the entire method terminates and enters the failure processing logic.
[0012] Furthermore, in step S3, the tree structure composed of the root node, the first-level nodes, ... changes continuously with the number of iterations.
[0013] Furthermore, in step S3, each node corresponds to either a request without a next-level request or a new multi-level request; the new multi-level request serves as a sub-request of the original multi-level request.
[0014] Furthermore, in step S4, the initially saved callback function is executed to summarize the results of requests at each level.
[0015] The present application also proposes a device for asynchronously obtaining the parsing results of multi-level requests, including a tree creation unit, a tree derivation unit, an iteration unit, and a summary unit. The tree creation unit is used to create a root node to represent a multi-level request, and the state of the root node represents the completion status of the multi-level request. The tree derivation unit is used to use the first-level request of the multi-level request as the first-level child node of the root node, and the state of each first-level child node represents the completion status of each first-level request; each node from the first-level node downwards has 5 states, namely: PENDING means that the request has not been issued yet, SCHEDULED means that the request has been issued but the result has not been obtained, EXECUTED means that the result has been obtained but the parsing has not been completed, RESOLVED means that the result has been obtained and the parsing is completed, and REJECTED means that an exception occurred during the execution process. The iteration unit is used to traverse all nodes from the root node downwards, issue requests to all nodes with a status of PENDING, and change the status of these nodes to SCHEDULED; when any node receives a result, the status of the node is changed from SCHEDULED to EXECUTED; if the received result indicates that a request has a lower level request, a new level of child node is added below the corresponding node; the status of the newly added child node represents the completion of the request at the corresponding level, which is initially PENDING; if the received result indicates that a request has no lower level request, the status of the node corresponding to the request is changed from EXECUTED to RESOLVED; the iteration unit is repeatedly executed until the status of all nodes except the root node is RESOLVED. The summary unit is used to summarize the results of requests at each level to obtain the final result of multi-level requests.
[0016] The technical effect achieved by this application is: shortening the processing time of multi-level requests, avoiding the occurrence of callback hell while making full use of asynchronous characteristics. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] Figure 1 It is a flowchart of the method for asynchronously obtaining multi-level request parsing results proposed in this application.
[0018] Figure 2 It is a structural diagram of the device for asynchronously obtaining multi-level request parsing results proposed in this application.
[0019] Explanation of the reference numerals in the figure: tree creation unit 1, tree derivation unit 2, iteration unit 3, summary unit 4. DETAILED DESCRIPTION
[0020] See also Figure 1 The method for asynchronously obtaining multi-level request parsing results proposed in this application includes the following steps.
[0021] Step S1: Create a root node to represent a multi-level request. The state of the root node represents the completion of the multi-level request. Save the callback function after the multi-level request is completed, the context information shared during the system operation, the request depth limit and other information. The callback function receives the callback parameters when the system is initialized and the context information in the system. The request depth limit is used to avoid falling into an infinite loop.
[0022] Step S2: Use the first-level request of the multi-level request as the first-level child node of the root node. The root node has as many first-level child nodes as the number of first-level requests of the multi-level request. The state of each first-level child node represents the completion status of each first-level request. Each node from the first-level node downwards (such as the second-level node, the third-level node...) has 5 states, namely: PENDING means that the request has not been issued yet, SCHEDULED means that the request has been issued but the result has not been obtained, EXECUTED means that the result has been obtained but the parsing is not complete, RESOLVED means that the result has been obtained and the parsing is complete, and REJECTED means that an exception occurred during the execution process.
[0023] Step S3: Traverse all nodes (first-level nodes, second-level nodes, third-level nodes, etc.) from the root node downwards, issue requests to all nodes in the PENDING state, and change the states of these nodes to SCHEDULED. If the state of a node is PENDING, the node indicates a request that has not yet been issued. If the state of a node is SCHEDULED, the node indicates that a request has been issued but no result has been obtained.
[0024] When any node receives the result, the status of the node is changed from SCHEDULED to EXECUTED. If the received result indicates that a request has a lower-level request, a new level of child node is added below the corresponding node. The status of the newly added child node represents the completion status of the request at the corresponding level, which is initially PENDING. If the received result indicates that a request has no lower-level request, the status of the node corresponding to the request is changed from EXECUTED to RESOLVED. If an exception occurs during the result parsing of a request, the status of the node is changed to REJECTED. When any node is in the REJECTED state, the entire method terminates and enters the failure handling logic set by the user, such as starting over from the beginning. Then repeat step S3 until the status of all nodes except the root node is RESOLVED.
[0025] The tree structure composed of the root node, the first-level node, the second-level node, etc. changes continuously with the number of iterations, wherein each node corresponds to a request without a next-level request, or corresponds to a new multi-level request (i.e., a sub-request of the original multi-level request corresponding to the root node). When step S3 is executed for the first time, the tree structure has only the root node and the first-level node, that is, the first-level request is issued to all the first-level nodes at the same time. When step S3 is executed for the second time, the tree structure has the root node and the first-level node, and a second-level node may be newly added. When step S3 is executed for the third time, the tree structure may also have second-level nodes and third-level nodes.
[0026] Step S4: Execute the callback function saved initially to summarize the results of requests at each level and obtain the final results of the multi-level requests.
[0027] The existing method of obtaining parsing results of multi-level requests in an asynchronous manner parses the results in units of levels. After all the results of the first-level requests are obtained, the second-level requests are issued. Obviously, a lot of time is spent waiting for the slower results of each level of requests.
[0028] The method proposed in the present application for asynchronously obtaining parsing results of multi-level requests no longer parses results by level, but instead records the status of each request in real time, and issues requests to all nodes with a PENDING status each time the result of a request is received. At this time, requests of different levels may be issued at the same time, thus avoiding waiting time and shortening the total time for obtaining the parsing results of the entire multi-level request.
[0029] The method for asynchronously obtaining multi-level request parsing results proposed in this application is that all nodes do not have to wait for other nodes to complete before they can be scheduled in order to consider the order and integrity of the final result (the consequence is callback hell). In an asynchronous system, it can be considered that the completion time of all tasks is random. The core of this application is that there is no need to wait, that is, there is no need to care about when to schedule, nor when to summarize the final or interim results. If the subsequent steps depend on the previous results, the subsequent steps can be automatically processed after the results are generated. In this application, if a node request or parsing fails, the entire process will fail, and the remaining unscheduled nodes will no longer be scheduled. After the failure, the failed processing logic will be automatically executed (the logic can be added by the user during initialization). Therefore, this application avoids the generation of callback hell while making full use of the asynchronous characteristics.
[0030] See also Figure 2 The device for asynchronously obtaining multi-level request parsing results proposed in this application includes a tree creation unit 1, a tree derivation unit 2, an iteration unit 3, and a summary unit 4. Figure 2 The device shown corresponds to Figure 1 The method shown.
[0031] The tree creation unit 1 is used to create a root node to represent a multi-level request, and the state of the root node represents the completion status of the multi-level request.
[0032] The tree derivation unit 2 is used to use the first-level request of the multi-level request as the first-level child node of the root node. The state of each first-level child node represents the completion status of each first-level request. Each node from the first-level node downwards has 5 states, namely: PENDING means that the request has not been issued yet, SCHEDULED means that the request has been issued but the result has not been obtained, EXECUTED means that the result has been obtained but the parsing is not completed, RESOLVED means that the result has been obtained and the parsing is completed, and REJECTED means that an exception occurs during the execution process.
[0033] The iteration unit 3 is used to traverse all nodes downward from the root node, issue requests to all nodes with a status of PENDING, and change the status of these nodes to SCHEDULED. When any node receives a result, the status of the node is changed from SCHEDULED to EXECUTED. If the received result indicates that a request has a lower level request, a new level of child node is added below the corresponding node. The status of the newly added child node represents the completion status of the request at the corresponding level, which is initially PENDING. If the received result indicates that a request has no lower level request, the status of the node corresponding to the request is changed from EXECUTED to RESOLVED. If an exception occurs during the result parsing of a request, the status of the node is changed to REJECTED. The iteration unit 3 is executed repeatedly until the status of all nodes except the root node is RESOLVED.
[0034] The summarizing unit 4 is used to summarize the results of requests at each level to obtain the final results of multi-level requests.
[0035] The above are only preferred embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present application should all be included in the protection scope of the present application.
Claims
1. A method for asynchronously obtaining multi-level request parsing results, characterized in that: The method comprises the following steps: Step S1: Create a root node to represent a multi-level request, and the state of the root node represents the completion status of the multi-level request; Step S2: The first-level request of the multi-level request is used as the first-level child node of the root node, and the state of each first-level child node represents the completion status of each first-level request; each node from the first-level node downwards has 5 states, namely: PENDING means that the request has not been issued yet, SCHEDULED means that the request has been issued but the result has not been obtained, EXECUTED means that the result has been obtained but the parsing is not completed, RESOLVED means that the result has been obtained and the parsing is completed, and REJECTED means that an exception occurs during the execution process; Step S3: Traverse all nodes downward from the root node, issue requests to all nodes in the PENDING state, and change the state of these nodes to SCHEDULED; when any node receives the result, the state of the node is changed from SCHEDULED to EXECUTED; if the received result indicates that a request has a lower level request, add a new level of child node under the corresponding node; the state of the newly added child node represents the completion of the request at the corresponding level, which is initially PENDING; if the received result indicates that a request has no lower level request, change the state of the node corresponding to the request from EXECUTED to RESOLVED; repeat step S3 until the state of all nodes except the root node is RESOLVED; Step S4: Summarize the results of requests at each level to obtain the final result of multi-level requests.
2. The method for asynchronously obtaining multi-level request parsing results according to claim 1, characterized in that: In the step S1, the callback function after the multi-level request is completed, the context information shared during the operation, and the limit of the request depth are also saved.
3. The method for asynchronously obtaining multi-level request parsing results according to claim 2, characterized in that: In the step S1, the callback function receives the callback parameters during initialization and the context information in the system.
4. The method for asynchronously obtaining multi-level request parsing results according to claim 1, characterized in that: In step S2, the root node has as many first-level child nodes as the multi-level requests have first-level requests.
5. The method for asynchronously obtaining multi-level request parsing results according to claim 1, characterized in that: In step S3, if an exception occurs during the result parsing of a request, the state of the node is changed to REJECTED; when any node is in the REJECTED state, the entire method terminates and enters the failure processing logic.
6. The method for asynchronously obtaining multi-level request parsing results according to claim 1, characterized in that: In step S3, the tree structure composed of the root node, the first-level nodes, ... changes continuously with the number of iterations.
7. The method for asynchronously obtaining multi-level request parsing results according to claim 6, characterized in that: In step S3, each node corresponds to either a request without a next-level request or a new multi-level request; the new multi-level request serves as a sub-request of the original multi-level request.
8. The method for asynchronously obtaining multi-level request parsing results according to claim 1, characterized in that: In step S4, the initially saved callback function is executed to summarize the results of requests at each level.
9. A device for asynchronously obtaining multi-level request parsing results, characterized in that: It includes tree creation unit, tree derivation unit, iteration unit and summary unit; The tree creation unit is used to create a root node to represent a multi-level request, and the state of the root node represents the completion status of the multi-level request; The tree derivation unit is used to use the first-level request of the multi-level request as the first-level child node of the root node, and the state of each first-level child node represents the completion status of each first-level request; each node from the first-level node downwards has 5 states, namely: PENDING means that the request has not been issued yet, SCHEDULED means that the request has been issued but the result has not been obtained, EXECUTED means that the result has been obtained but the parsing has not been completed, RESOLVED means that the result has been obtained and the parsing has been completed, and REJECTED means that an exception occurs during the execution process; The iteration unit is used to traverse all nodes from the root node downwards, issue requests to all nodes in the PENDING state, and change the states of these nodes to SCHEDULED; when any node receives a result, the state of the node is changed from SCHEDULED to EXECUTED; if the received result indicates that a request has a lower level request, a new level of child node is added below the corresponding node; the state of the newly added child node represents the completion of the request at the corresponding level, which is initially PENDING; if the received result indicates that a request has no lower level request, the state of the node corresponding to the request is changed from EXECUTED to RESOLVED; the iteration unit is repeatedly executed until the states of all nodes except the root node are RESOLVED; The summarizing unit is used to summarize the results of requests at each level to obtain the final results of multi-level requests.
