Command execution acceleration method and apparatus, electronic device, and storage medium
By pre-importing a preset command library on the server and adopting a C/S architecture, the problem of low execution efficiency of Python commands on devices with low CPU performance is solved. Dynamic caching of the command library and reasonable use of memory are realized, improving user experience and execution efficiency.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- MAIPU COMM TECH CO LTD
- Filing Date
- 2022-12-22
- Publication Date
- 2026-06-16
AI Technical Summary
Python commands are inefficient on devices with low CPU performance, resulting in a poor user experience, a problem that current technologies cannot effectively solve.
The system adopts a client/server software architecture, separating Python commands into client and server sides. The server side pre-imports a preset command library, and the client and server sides communicate with each other through process communication. Through the command tree and command library dynamic caching mechanism, the command library is directly called to execute commands, avoiding repeated import of command libraries and memory waste.
It improves the execution efficiency of Python commands on devices with low CPU performance, enhances the user experience, reduces command library import time and memory consumption, and supports cross-platform portability and strong scalability.
Smart Images

Figure CN115827277B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of Python software development, and more specifically, to a command execution acceleration method, apparatus, electronic device, and storage medium. Background Technology
[0002] Python is widely used in the software development field due to its portability and rich libraries, which facilitates rapid updates and iterations of new products.
[0003] Because Python is an interpreted language, Python commands are converted into corresponding machine code by the Python interpreter during execution. Compared with compiled languages, this involves an additional decoding process, resulting in slower execution speeds on devices with the same CPU (Central Processing Unit), especially on devices with low CPU performance.
[0004] Currently, there are two main scenarios in software development: 1. Rapid development using Python on devices with low CPU performance; 2. Running existing systems on devices with low CPU performance, such as the open-source network operating system SONiC (Software for open networking in the cloud), which contains a large number of Python commands for configuring device business functions.
[0005] In Scenario 1, replacing the CPU with a more powerful one would increase hardware costs, while using another language would result in lower development efficiency and portability. In Scenario 2, implementing the functionality of existing Python commands in other ways would consume a significant amount of manpower.
[0006] Therefore, due to certain inherent characteristics, Python commands execute slowly on devices with low CPU performance, directly resulting in a poor user experience. Replacing hardware or refactoring the software both consume significant resources. Current technology cannot currently resolve the issue of low execution efficiency of Python commands on devices with low CPU performance. Summary of the Invention
[0007] The purpose of this application is to provide a command execution acceleration method, apparatus, electronic device, and storage medium to solve the problem of low execution efficiency of Python commands on devices with low CPU performance. To achieve the above objective, the technical solution adopted in this application is as follows:
[0008] In a first aspect, embodiments of this application provide a command execution acceleration method applied to an electronic device, wherein the electronic device runs a client and a server, the client and the server communicate with each other, and the server has a preset command library pre-imported, and the method includes:
[0009] The client receives the current command entered by the user through the command-line interface and sends the current command to the server;
[0010] The server receives the current command and determines whether the command library corresponding to the current command has been imported.
[0011] If the command library corresponding to the current command has been imported, the server executes the current command to obtain the command execution result and returns the command execution result to the client;
[0012] The client displays the result of the command execution.
[0013] Optionally, the method further includes:
[0014] If the command library corresponding to the current command is not imported, the server queries the pre-established command tree to obtain the command library corresponding to the current command and imports the command library corresponding to the current command; wherein, the command tree maintains the association relationship between each command keyword and its corresponding command library;
[0015] The server executes the current command, obtains the command execution result, and returns the command execution result to the client.
[0016] Optionally, the command tree also maintains the association between each command keyword and its corresponding command execution entry point;
[0017] The step of the server executing the current command, obtaining the command execution result, and returning the command execution result to the client includes:
[0018] The server queries the command tree to obtain the command execution entry point corresponding to the current command, initializes system variables through the command execution entry point, and executes the current command to obtain the command execution result;
[0019] If the current command is an interactive command, the server returns the command execution result to the client through the interactive command interface;
[0020] If the current command is a real-time command, the server will return the command execution result to the client in real time.
[0021] Optionally, the method further includes:
[0022] Upon receiving a client startup command, the client initializes itself to complete the construction of basic command-line information.
[0023] Optionally, the client performs initialization to complete the construction of command-line basic information, including:
[0024] Traverse the system directory and search for executable Python command files to obtain the keyword for each command;
[0025] Import the Python CMD library, inherit the Cmd class and initialize it, and construct the command line for each of the stated command keywords;
[0026] Complete the initialization of the Readline, which is used to read the entire line of command line input by the user.
[0027] Optionally, the method further includes:
[0028] Upon receiving the system startup command, the server initializes itself to import the preset command library and initialize the relevant task threads.
[0029] Optionally, the server performs initialization to complete the steps of importing the preset command library and initializing the relevant task threads, including:
[0030] Traverse the system directory and search for executable Python command files to obtain the keyword for each command;
[0031] Obtain the command library and program execution entry point corresponding to each command keyword, and establish the association between each command keyword and its corresponding command library and program execution entry point to obtain a command tree;
[0032] The preset command library is obtained by querying the command tree according to the user's preset command, and then imported into the preset command library;
[0033] Initialize the command library aging thread; wherein, the command library aging thread is used to periodically query the currently imported command libraries and remove them when the currently imported command libraries have expired and have not been used;
[0034] Complete the output redirection of all commands to achieve unified management of the execution output of all commands;
[0035] Rewrite the interactive command interface to take over all interactive commands.
[0036] Secondly, this application also provides a command execution acceleration device for use in electronic devices. The device includes a client and a server, the client and the server communicate with each other through processes, and the server has a preset command library pre-loaded into it.
[0037] The client is used to receive the current command input by the user through the command line interface and send the current command to the server;
[0038] The server is used to receive the current command and determine whether the command library corresponding to the current command has been imported.
[0039] The server is also configured to, if the command library corresponding to the current command has been imported, execute the current command to obtain the command execution result, and return the command execution result to the client;
[0040] The client is also used to display the execution result of the command.
[0041] Thirdly, embodiments of this application also provide an electronic device, including a processor and a memory, wherein the memory is used to store a program, and the processor is used to implement the command execution acceleration method in the first aspect above when executing the program.
[0042] Fourthly, embodiments of this application also provide a computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements the command execution acceleration method described in the first aspect above.
[0043] Compared to existing technologies, this application provides a command execution acceleration method, apparatus, electronic device, and storage medium. The electronic device runs a client and a server, which communicate with each other. The server has a pre-loaded preset command library. The client receives the current command input by the user through the command-line interface and sends it to the server. After receiving the current command, the server first determines whether the corresponding command library has been imported. If it has, the server directly executes the current command, obtains the command execution result, and returns the result to the client, which then displays the result. In other words, using a C / S software architecture, the execution process of the existing Python commands is optimized without affecting them. By pre-loading the preset command library on the server, the corresponding command library can be directly called to execute commands during execution. Dynamic caching of the command library is supported, eliminating the need for importing the command library and avoiding excessive memory usage, thereby significantly improving the execution efficiency of Python commands and enhancing the user experience. Attached Figure Description
[0044] Figure 1 The diagram illustrates a software architecture diagram of a command execution acceleration method provided in an embodiment of this application.
[0045] Figure 2 This application provides a flowchart illustrating a command execution acceleration method according to an embodiment. Figure 1 .
[0046] Figure 3 This application provides a flowchart illustrating a command execution acceleration method according to an embodiment. Figure 2 .
[0047] Figure 4 A block diagram of a command execution acceleration device provided in an embodiment of this application is shown.
[0048] Figure 5 A block diagram of an electronic device provided in an embodiment of this application is shown.
[0049] Icons: 100 - Command execution acceleration device; 101 - Client; 102 - Server; 10 - Electronic device; 11 - Processor; 12 - Memory; 13 - Bus. Detailed Implementation
[0050] The technical solutions in the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings.
[0051] To address the issue of low execution efficiency of Python commands on devices with low CPU performance, this application adopts a C / S (client / server) software architecture. Without affecting the original Python commands, it optimizes the execution process of the existing Python commands. By pre-importing preset command libraries on the server side, commands can be directly executed by calling the corresponding command libraries. Dynamic caching of command libraries is supported, eliminating the need for importing command libraries and avoiding excessive memory usage. This significantly improves the execution efficiency of Python commands and enhances the user experience.
[0052] Please refer to Figure 1 , Figure 1 The software architecture diagram of the command execution acceleration method provided in the embodiments of this application is shown, such as... Figure 1 As shown in the figure, the embodiment of this application adopts a C / S architecture, which separates the original Python commands in the system into two parts: client and server. Both the client and server are coded in Python, and the client and server communicate with each other through processes, for example, using sockets.
[0053] In this embodiment, there can be multiple clients, meaning the client supports multiple users; while there is only one server, the purpose of which is to solve the problem of high memory consumption. Furthermore, the client and server run on the same electronic device, which can be a host computer, a personal computer, a server, or other devices that can perform the same or similar functions; this embodiment does not impose any restrictions on this.
[0054] The client is primarily responsible for command construction, command parsing, and command output, and can include the following aspects:
[0055] 1. Command Scan: Performs a dynamic scan of Python commands in the current system and stores the scan results. That is, it scans all Python commands in the current system to obtain command keywords, such as names.
[0056] 2. Command Line Construction: Based on the scan results, construct the CLI (command-line interface) for Python commands. The CLI is an interactive interface used by users to input commands via the keyboard, which are then executed by the electronic device.
[0057] 3. Command line parsing: Obtain the Python command currently entered by the user and complete the parsing of the user's input command.
[0058] 4. Message sending and receiving: Encapsulate user input commands and send them to the server, and receive and parse / recognize the data returned by the server after executing the user input commands.
[0059] 5. Command result display: Display the execution result of the user-input command on the current screen.
[0060] The server is primarily responsible for command collection, command library preloading, command caching mechanisms, and command execution, and can include the following aspects:
[0061] 1. Command Initialization: Collect all Python commands in the current system to generate a command tree. The command tree includes the association between each command keyword and its corresponding command library and command execution entry point. At the same time, based on the generated command tree, the command library corresponding to each command keyword is preloaded.
[0062] 2. Message parsing: Parse the user input command sent by the client to obtain the command keyword, index the corresponding command library from the command tree using the command keyword, and selectively import it. That is, check whether the corresponding command library has been imported. If it has, no processing is done. If not, import the command library.
[0063] 3. Command caching mechanism: By adding an aging mechanism, the imported command libraries are monitored periodically. If they are not used within a timeout period (e.g., 5 minutes), the corresponding command library is removed, thereby reducing memory consumption and realizing dynamic memory allocation and release.
[0064] 4. Command Execution: Initialize the system parameters required by the command based on the final parsing result of the user-input command, and call the corresponding command entry method to execute the command and obtain the result.
[0065] 5. Command Output: Dynamically identify the command type and return the result to the client. That is, if the user inputs an interactive command, the current status of command execution is returned to the client through the interactive command interface, waiting for the user to input whether to continue executing the command; if the user inputs a real-time command, the result is returned to the client in real time.
[0066] The following is based on Figure 1 The software architecture shown in this application provides a detailed description of the command execution acceleration method provided in the embodiments of this application.
[0067] Please refer to Figure 2 , Figure 2 This paper illustrates a flowchart of a command execution acceleration method provided in an embodiment of the present application, which may include the following steps:
[0068] S103, the client receives the current command entered by the user through the command line interface and sends the current command to the server.
[0069] In this embodiment, when the client receives the current command input by the user via CLI, it constructs the corresponding command characters and sends them to the server, which may include:
[0070] The first step is to determine whether the current command is to be executed or completed by parsing the command header and command parameters. For example, when a user enters a command, they will press the "Tab" key to complete the command and press the "Enter" key to execute the command. Therefore, by parsing the command header and command parameters, we can determine whether the user pressed the "Tab" key or the "Enter" key to determine whether the current command is to be executed or completed.
[0071] The second step is to construct the corresponding command characters according to the preset information format and send the command characters to the server. The preset information format refers to the preset interaction format between the client and the server, which aims to prevent abnormal client access. The preset information format can define a flag field, or a flag information can be added before the command. It can be flexibly set according to actual needs, and this application embodiment does not impose any restrictions on it.
[0072] S105, the server receives the current command and determines whether the command library corresponding to the current command has been imported.
[0073] S107. If the command library corresponding to the current command has been imported, the server executes the current command, obtains the command execution result, and returns the command execution result to the client.
[0074] In this embodiment, when the server receives the current command sent by the client, it parses the current command and dynamically imports the corresponding command library. That is, it first obtains all parameters of the current command sent by the client according to a preset information format; then, it determines whether the command library corresponding to the current command has been imported based on the parameters. If it has been imported, it directly calls the entry method of the corresponding command to execute the current command; if it has not been imported, it obtains the command library corresponding to the current command by querying the command tree, imports it, and then calls the entry method of the corresponding command to execute the current command.
[0075] It should be noted that the command library corresponding to the current command has been imported. This may be because the command library is included in the preset command library that the server has imported in advance, or it may be the command library that the server imported when executing historical commands. This application embodiment does not impose any restrictions on this.
[0076] S109, the client displays the command execution result.
[0077] In this embodiment, after the server executes the current command and obtains the command execution result, it will also encapsulate the command execution result according to a preset information format and return it to the client. After receiving it, the client will decapsulate it according to the preset information format and display the command execution result on the current screen.
[0078] In this embodiment, when a Python command is executed, if it involves auto-completion of subcommands or display of help information, the time consumed is mainly due to the import of command libraries. Compared to existing technologies, on devices with low CPU performance, it can take several seconds to obtain the corresponding execution results, resulting in a poor user experience. This application's auto-completion can be completed quickly, and help information is displayed instantly. Furthermore, this application does not handle complex command logic, and the time consumed by importing command libraries accounts for a significant proportion of the total command execution time. Therefore, adopting this application can significantly reduce command execution time and improve the user experience.
[0079] The foregoing embodiments are described from the perspective of command execution flow. In order to better understand the embodiments of this application, the following will be described from the perspective of the overall process.
[0080] Please refer to Figure 3 , Figure 3 This illustration shows another flowchart of the command execution acceleration method provided in an embodiment of this application, which may include the following steps:
[0081] S101: Upon receiving the client's startup command, the client initializes itself to complete the construction of basic command-line information.
[0082] S102, upon receiving the system startup command, the server performs initialization to complete the import of the preset command library and the initialization of related task threads.
[0083] In this embodiment, the overall flow of the command execution acceleration method can include an initialization flow and a command execution flow. The initialization flow includes: initialization of socket / signal detection, client initialization, and server initialization, which will be described separately below.
[0084] Optionally, the initialization of socket / signal detection, etc., may include:
[0085] 1. Socket initialization: Sockets are used for data exchange between clients and servers, such as commands and control information.
[0086] 2. Information detection initialization: Signal detection is used to handle abnormal process information and prevent abnormal exit.
[0087] Optionally, when the user executes the client program, the client will perform initialization, which may include:
[0088] 1. Traverse the system directory and search for executable Python command files to obtain the keyword for each command.
[0089] Among them, executable Python command files can be executable files ending with ".py" or containing Python language keywords in their content.
[0090] 2. Import the Python CMD module, inherit the Cmd class and initialize it to build the command line for each command keyword.
[0091] The CMD module is a Python library used to build interactive shells. By inheriting from the Python Cmd class, you can build the most basic command lines. Based on each command keyword obtained in the previous step, you can dynamically create command lines for each command keyword.
[0092] Furthermore, relevant methods of the Python CMD module can be rewritten according to actual needs. For example, the original Python CMD module, for the command "user enters Enter", will execute the previous command by default. If it is not necessary to execute the previous command, the result of "user enters Enter" needs to be rewritten. In other words, the Python CMD module has many methods. If the implementation of a method needs to be modified, the corresponding method needs to be rewritten. Rewriting is to change the original implementation of the Python CMD module. If no change is needed, rewriting is not required. This application embodiment does not impose any restrictions on this.
[0093] 3. Complete the initialization of Readline, which is a Python library used to read the entire line of command line input by the user.
[0094] Optionally, the server may perform initialization during system startup, and the initialization process may include:
[0095] 1. Traverse the system directory and search for executable Python command files to obtain the keyword for each command. This part is similar to the client initialization process, and will not be described in detail in this embodiment.
[0096] 2. Obtain the command library and program execution entry point corresponding to each command keyword, and establish the association between each command keyword and its corresponding command library and program execution entry point to obtain the command tree.
[0097] 3. Query the command tree based on the user's preset commands to obtain the preset command library, and import the preset command library.
[0098] Among them, the user-preset commands can be some commonly used commands. By pre-importing the command library corresponding to the commonly used commands, the corresponding command library can be directly called to execute commands, which can greatly improve the execution efficiency of Python commands and improve the user experience.
[0099] 4. Initialize the command library aging thread. The command library aging thread is used to periodically query the currently imported command libraries and remove them when they expire and are not used, thereby reducing memory consumption and realizing dynamic memory allocation and release.
[0100] It's important to note that the command library aging thread removes command libraries imported by the server when executing historical commands, not pre-imported default command libraries. Since default command libraries are frequently used by users, they are kept resident, saving time spent importing command libraries during command execution. The command library aging thread dynamically releases command libraries imported by the server when executing historical commands, reducing unnecessary memory consumption caused by loading large amounts of command libraries. This achieves the dual positive effects of command acceleration and efficient memory usage.
[0101] 5. Complete the output redirection of all commands to achieve unified management of the execution output of all commands.
[0102] In Python, the output of each command may be different. For example, it may be output to different network ports or interfaces. Therefore, in order to ensure that the server can receive the output of all commands, it is necessary to redirect the output of all commands and manage the output of all commands in a unified manner.
[0103] 6. Rewrite the interactive command interface to take over all interactive commands.
[0104] Based on the foregoing, user-inputted commands may be real-time or interactive. If the client cannot detect interactive commands, the corresponding commands will fail to execute. Therefore, to ensure interaction with the user when interactive commands are involved, the interactive command interface needs to be rewritten to handle all interactive commands.
[0105] S103, the client receives the current command entered by the user through the command line interface and sends the current command to the server.
[0106] S105, the server receives the current command and determines whether the command library corresponding to the current command has been imported.
[0107] S106. If the command library corresponding to the current command is not imported, the server queries the pre-established command tree to obtain the command library corresponding to the current command and imports the command library corresponding to the current command. The command tree maintains the association between each command keyword and its corresponding command library.
[0108] In this embodiment, the server obtains the command library corresponding to the current command by querying the command tree, imports the command library corresponding to the current command, executes the current command to obtain the command execution result, and returns the command execution result to the client.
[0109] S107. If the command library corresponding to the current command has been imported, the server executes the current command, obtains the command execution result, and returns the command execution result to the client.
[0110] In this embodiment, the command tree also maintains the association between each command keyword and its corresponding command execution entry point. Therefore, the process of the server executing the current command, obtaining the command execution result, and returning the command execution result to the client can include:
[0111] S1, the server queries the command tree to obtain the command execution entry point corresponding to the current command, initializes system variables through the command execution entry point, and executes the current command to obtain the command execution result.
[0112] S2. If the current command type is an interactive command, the server will return the command execution result to the client through the interactive command interface.
[0113] S3. If the current command is a real-time command, the server will return the command execution result to the client in real time.
[0114] In other words, the server retrieves the command execution entry point corresponding to the current command by querying the command tree, then initializes system variables, executes the current command, and obtains the command execution result. Afterward, the server returns the command execution result to the client based on the type of the current command.
[0115] Optionally, the server can set flags before and during the execution of the current command, based on the command's preset type and whether an interactive interface is called. For example, "false" for interactive commands and "true" for real-time commands. Then, when the command execution result is obtained, the server can output the result based on the flags. That is, if it's an interactive command, since the server needs to interact with the client during command execution and has already taken over the interactive command interface through the initialization process, it returns the intermediate state of command execution to the client through the interactive command interface and waits for the client's feedback on whether to continue executing the command. If it's a real-time command, the server will return the command execution result to the client in real time.
[0116] S109, the client displays the command execution result.
[0117] In this embodiment, after receiving the command execution result returned by the server, the client will determine the type of the command execution result and return the command execution result to the user according to the command type.
[0118] Optionally, the type of command execution result can be: real-time result, interactive result, or execution complete. If it is an interactive result, the command execution result will be displayed on the current screen, and the system will continue to wait for user input; if it is a real-time result, the real-time output of the command execution will be displayed on the current screen; ordinary commands will wait for the server to send a command execution complete marker before displaying the result.
[0119] Compared with the prior art, the embodiments of this application have the following beneficial effects:
[0120] First, without making any modifications to the executed Python commands, a C / S architecture Python command execution mechanism was built, which can significantly improve the execution efficiency of Python commands, enhance the user experience, and provide an optimization solution for running Python on devices with low CPU performance.
[0121] Secondly, the software architecture is simple, can quickly achieve the results of accelerated Python command execution, is cross-platform portable, centrally manages commands to facilitate users to add additional customization requirements, and has strong scalability.
[0122] Third, the client implements dynamic scanning of system Python commands and dynamically builds a Python CLI without modifying the original code logic; the server dynamically scans system Python commands to generate a command tree, which includes the association between each command keyword and its corresponding command library and command execution entry point, without depending on the original Python commands.
[0123] Fourth, the server adopts a persistent + cache mechanism for the command library. That is, the preset command libraries that users frequently use are kept persistent, while other imported command libraries are dynamically released. This can reduce the time spent importing libraries during command execution and the unnecessary memory consumption caused by loading a large number of libraries, achieving the dual positive effects of command acceleration and reasonable memory use.
[0124] In order to perform the corresponding steps in the above method embodiments and various possible implementations, an implementation of a command execution acceleration device is given below.
[0125] Please refer to Figure 4 , Figure 4 A block diagram of a command execution acceleration device 100 provided in an embodiment of this application is shown. The command execution acceleration device 100 includes: a client 101 and a server 102.
[0126] Client 101 is used to receive the current command entered by the user through the command line interface and send the current command to the server.
[0127] Server 102 is used to receive the current command and determine whether the command library corresponding to the current command has been imported.
[0128] Server 102 is also used to execute the current command if the command library corresponding to the current command has been imported, obtain the command execution result, and return the command execution result to the client.
[0129] Client 101 is also used to display the results of command execution.
[0130] Optionally, server 102 is also used for:
[0131] If the command library corresponding to the current command is not imported, the command library corresponding to the current command is obtained by querying the pre-built command tree, and then the command library corresponding to the current command is imported; the command tree maintains the association between each command keyword and its corresponding command library;
[0132] The current command is executed to obtain the command execution result, and the command execution result is returned to the client.
[0133] Optionally, the command tree also maintains the association between each command keyword and its corresponding command execution entry point; the way the server 102 executes the current command, obtains the command execution result, and returns the command execution result to the client may include:
[0134] The command tree is queried to obtain the command execution entry point corresponding to the current command. System variables are initialized through the command execution entry point, and the current command is executed to obtain the command execution result.
[0135] If the current command is an interactive command, the command execution result will be returned to the client through the interactive command interface;
[0136] If the current command is a real-time command, the command execution result will be returned to the client in real time.
[0137] Optionally, client 101 is also used to initialize upon receiving a client startup command to complete the construction of command-line basic information.
[0138] Optionally, the initialization process of client 101 to complete the construction of command-line basic information may include:
[0139] Traverse the system directory and search for executable Python command files to obtain the keyword for each command;
[0140] Import the Python CMD module, inherit the Cmd class and initialize it, and construct the command line for each command keyword;
[0141] Complete the initialization of Readline, which is used to read the entire line of command line input by the user.
[0142] Optionally, the server 102 is also used to initialize when it receives a system startup command, so as to complete the import of a preset command library and the initialization of related task threads.
[0143] Optionally, the initialization of server 102 to complete the import of the preset command library and the initialization of related task threads may include:
[0144] Traverse the system directory and search for executable Python command files to obtain the keyword for each command;
[0145] Obtain the command library and program execution entry point corresponding to each command keyword, and establish the association between each command keyword and its corresponding command library and program execution entry point to obtain the command tree;
[0146] The preset command library is obtained by querying the command tree based on the user's preset commands, and then imported into the preset command library;
[0147] Initialize the command library aging thread; the command library aging thread is used to periodically query the currently imported command libraries and remove them when they expire and are not used.
[0148] Complete the output redirection of all commands to achieve unified management of the execution output of all commands;
[0149] Rewrite the interactive command interface to take over all interactive commands.
[0150] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working process of the command execution acceleration device described above can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.
[0151] Please refer to Figure 5 , Figure 5 The diagram shows a block diagram of an electronic device 10 provided in an embodiment of this application, including a processor 11, a memory 12 and a bus 13, wherein the processor 11 is connected to the memory 12 via the bus 13.
[0152] The memory 12 is used to store programs. After receiving an execution instruction, the processor 11 executes the program to implement the command execution acceleration method disclosed in the above embodiments.
[0153] The memory 12 may include high-speed random access memory (RAM) or non-volatile memory (NVM).
[0154] Processor 11 may be an integrated circuit chip with signal processing capabilities. In implementation, each step of the above method can be completed through integrated logic circuits in the hardware of processor 11 or through software instructions. Processor 11 can be a general-purpose processor, including a Central Processing Unit (CPU), a Microcontroller Unit (MCU), a Complex Programmable Logic Device (CPLD), a Field Programmable Gate Array (FPGA), embedded ARM chips, etc.
[0155] This application also provides a computer-readable storage medium storing a computer program thereon, which, when executed by the processor 11, implements the command execution acceleration method disclosed in the above embodiments.
[0156] In summary, the command execution acceleration method, apparatus, electronic device, and storage medium provided in this application adopt a C / S software architecture. It constructs an optimized execution process for the original Python commands without affecting them. By pre-importing a preset command library on the server side, the corresponding command library can be directly called to execute commands during execution. It supports dynamic caching of the command library, eliminating the need for importing the command library and avoiding excessive memory usage. This significantly improves the execution efficiency of Python commands and enhances the user experience.
[0157] The above description is merely a preferred embodiment of this application and is not intended to limit this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the protection scope of this application.
Claims
1. A method of command execution acceleration, the method comprising: Applied to an electronic device, the electronic device running a client and a server, wherein the client and the server perform inter-process communication, the method includes: Upon receiving a system startup command, the server initializes to import a preset command library and initialize related task threads. This initialization includes: obtaining the command library and program execution entry point corresponding to each command keyword, establishing an association between each command keyword and its corresponding command library and program execution entry point to obtain a command tree; querying the command tree according to the user's preset command to obtain the preset command library, and importing the preset command library; and initializing a command library aging thread, which periodically queries the currently imported command libraries and removes them if they expire unused. The client receives the current command entered by the user through the command-line interface and sends the current command to the server; The server receives the current command and determines whether the command library corresponding to the current command has been imported. If the command library corresponding to the current command has been imported, the server executes the current command to obtain the command execution result and returns the command execution result to the client; The client displays the result of the command execution.
2. The method of claim 1, wherein, The method further includes: If the command library corresponding to the current command is not imported, the server queries the pre-established command tree to obtain the command library corresponding to the current command and imports the command library corresponding to the current command; wherein, the command tree maintains the association relationship between each command keyword and its corresponding command library; The server executes the current command, obtains the command execution result, and returns the command execution result to the client.
3. The method of claim 2, wherein, The command tree also maintains the association between each command keyword and its corresponding command execution entry point; The step of the server executing the current command, obtaining the command execution result, and returning the command execution result to the client includes: The server queries the command tree to obtain the command execution entry point corresponding to the current command, initializes system variables through the command execution entry point, and executes the current command to obtain the command execution result; If the current command is an interactive command, the server returns the command execution result to the client through the interactive command interface; If the current command is a real-time command, the server will return the command execution result to the client in real time.
4. The method of claim 1, wherein, The method further includes: Upon receiving a client startup command, the client initializes itself to complete the construction of basic command-line information.
5. The method of claim 4, wherein, The client is initialized to complete the construction of basic command-line information, including: Traverse the system directory and search for executable Python command files to obtain the keyword for each command; Import the Python CMD library, inherit the Cmd class and initialize it, and construct the command line for each of the stated command keywords; Complete the initialization of the Readline, which is used to read the entire line of command line input by the user.
6. The method of claim 1, wherein, The server initialization process, which includes importing the preset command library and initializing related task threads, further includes: Traverse the system directory and search for executable Python command files to obtain the keyword for each command; Complete the output redirection of all commands to achieve unified management of the execution output of all commands; Rewrite the interactive command interface to take over all interactive commands.
7. A command execution acceleration device characterized by comprising: Applied to electronic devices, the device includes a client and a server, wherein the client and the server perform inter-process communication; The server is configured to initialize upon receiving a system startup command to import a preset command library and initialize related task threads. The initialization process includes: obtaining the command library and program execution entry point corresponding to each command keyword, establishing an association between each command keyword and its corresponding command library and program execution entry point to obtain a command tree; querying the command tree according to a user-preset command to obtain the preset command library, and importing the preset command library; and initializing a command library aging thread, which periodically queries the currently imported command libraries and removes them if they expire unused. The client is used to receive the current command input by the user through the command line interface and send the current command to the server; The server is also used to receive the current command and determine whether the command library corresponding to the current command has been imported; The server is also configured to, if the command library corresponding to the current command has been imported, execute the current command to obtain the command execution result, and return the command execution result to the client; The client is also used to display the execution result of the command.
8. An electronic device, characterized in that, It includes a processor and a memory, the memory being used to store a program, and the processor being used to implement the command execution acceleration method according to any one of claims 1-6 when executing the program.
9. A computer-readable storage medium, characterized in that, It stores a computer program that, when executed by a processor, implements the command execution acceleration method as described in any one of claims 1-6.