Python code hot reloading method, system and medium

By creating hook functions in the Python interpreter to track and classify the import modules, identifying which modules need to be retained or removed, solving the performance overhead problems caused by the startup of the Python interpreter and the initialization of the computing library, and achieving the effect of improving system performance and ensuring the timeliness of business scripts.

CN118349218BActive Publication Date: 2025-03-14FENGHE INFORMATION TECH (SHANGHAI) CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202410421848.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-04-09
Publication Date
2025-03-14
Estimated Expiration
2044-04-09

AI Technical Summary

Technical Problem

In the scenario of heavy computing tasks, the time overhead caused by the startup of the Python interpreter and the initialization of the computing library have a significant impact on system performance and needs improvement.

Method used

By creating hook functions to track the import module of the Python interpreter and classify it based on the feature data of the imported module, identify which modules need to be retained and which need to be removed, thus avoiding the performance overhead of repeatedly starting the interpreter.

Benefits of technology

This achieves the additional performance overhead caused by repeatedly starting the Python interpreter. For systems that require high-frequency execution of large numbers of scripts with short execution time, the system performance can be greatly improved, the import overhead of pandas modules can be saved, and the timeliness of business scripts can be ensured.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118349218B_ABST
    Figure CN118349218B_ABST
Patent Text Reader

Abstract

The present invention provides a Python code hot reload method, system and medium, wherein the method comprises: creating a hook function to track the import module of the Python interpreter; classifying based on the feature data of the import module to identify whether the current import module is retained, the feature data comprises a first feature and a second feature, wherein when the first feature is identified based on the feature data of the import module, the corresponding import module is retained, wherein the retained import module comprises pandas; when the second feature is identified based on the feature data of the import module, the corresponding import module is removed, wherein the removed import module comprises utils. The present invention avoids the additional performance overhead caused by repeatedly starting the Python interpreter, and for a system that needs to execute a large number of scripts with a short execution time at a high frequency, the system performance can be greatly improved, the import overhead of the pandas module can be saved, and the timeliness of the business script utils is guaranteed.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of data processing technology, and in particular to a Python code hot reloading method, system and medium. Background Art

[0002] Many website systems often need to use Python scripts to complete some complex business data calculations. These scripts often need to be updated and adjusted. In order to meet the needs of real-time updates of Python scripts, currently, a separate Python interpreter process is usually started for each script execution.

[0003] Among them, Python calculations usually rely on some relatively large computing libraries (such as scipy, numpy, pandas, etc.), and each time the interpreter is started, these computing libraries need to be re-imported and the corresponding initialization operations need to be performed. However, in scenarios with heavy computing tasks, the time overhead caused by interpreter startup and computing library initialization will have a significant impact on system performance and needs to be improved. Summary of the invention

[0004] The purpose of the present invention is to provide a Python code hot reloading method, system and medium for solving the problem of Python code hot reloading.

[0005] In a first aspect, the present application provides a Python code hot reloading method, the method comprising:

[0006] Create a hook function to track the Python interpreter's imported modules;

[0007] Classification is performed based on feature data of the import module to identify whether the current import module is retained, the feature data includes a first feature and a second feature, wherein:

[0008] When the first feature is identified based on the feature data of the import module, the corresponding import module is retained, wherein the retained import module includes pandas;

[0009] When the second feature is identified based on the feature data of the import module, the corresponding import module is removed, wherein the removed import module includes utils.

[0010] In a possible implementation of the present application, the creation of a hook function to track the import module of the Python interpreter specifically includes:

[0011] During the execution of the Python script, the import behavior is rewritten to replace the class function to obtain the hook function;

[0012] Based on the hook function, the import modules involved in each import behavior are recorded in the class attributes;

[0013] The recorded module information of the imported module is used as a basis for maintaining the interpreter when the script execution ends.

[0014] In a possible implementation of the present application, the classification based on the feature data of the import module to identify whether the current import module is retained specifically includes:

[0015] Identify the type of the imported module, wherein the type includes pandas and utils;

[0016] Check the path to which the import module belongs to obtain the characteristic data;

[0017] Based on the path to which it belongs, it is determined whether the current path is in the path list of the default module library of the Python interpreter, where:

[0018] If so, the first feature is identified; if not, the second feature is identified.

[0019] In a possible implementation of the present application, when the first feature is identified based on the feature data of the import module, the corresponding import module is retained, specifically including:

[0020] After the first feature is identified based on the belonging path, determining that the current imported module is an unmodifiable module;

[0021] Record the name of the current imported module in the class attribute of the Module-Context class, so as to retain the corresponding imported module. When importing, the imported module specifically recursively imports pandas.core and pandas.api.

[0022] In a possible implementation of the present application, when the second feature is identified based on the feature data of the import module, the corresponding import module is removed, specifically including:

[0023] After identifying the second feature based on the belonging path, determining that the current imported module is a module to be removed;

[0024] Record the name of the currently imported module in the class attribute of the Module-Context class, and traverse the module cache of the current Python interpreter to remove the module to be removed.

[0025] In a possible implementation of the present application, the method further includes: starting a main process in the Python code and maintaining a target process pool, wherein the target process pool includes processes of multiple Python interpreters.

[0026] In a possible implementation of the present application, the main process is used to accept Python script execution requests and allocate the corresponding execution requests to idle Python interpreter working processes.

[0027] In a second aspect, the present application provides a Python code hot reload system, the system comprising:

[0028] The tracing module is used to create hook functions to trace the import modules of the Python interpreter;

[0029] An identification module is used to classify based on the feature data of the import module to identify whether the current import module is retained, the feature data includes a first feature and a second feature, wherein:

[0030] A retention module, used for retaining the corresponding import module when the first feature is identified based on the feature data of the import module, wherein the retained import module includes pandas;

[0031] A removal module is used to remove the corresponding import module when the second feature is identified based on the feature data of the import module, wherein the removed import module includes utils.

[0032] In a third aspect, the present application provides a computer-readable storage medium as described above, on which a computer program is stored, and when the program is executed by a processor, the Python code hot reloading method is implemented.

[0033] In a fourth aspect, the present application provides an electronic device as mentioned above, comprising: a processor and a memory; wherein the memory is used to store a computer program, and the processor is used to load and execute the computer program, so that the electronic device executes the Python code hot reloading method.

[0034] As described above, the Python code hot reloading method, system and medium of the present invention avoid the additional performance overhead caused by repeatedly starting the Python interpreter. For systems that need to execute a large number of scripts with short execution time at high frequency, the performance of the system can be greatly improved, the import overhead of the pandas module can be saved, and the timeliness of the business script utils is guaranteed. BRIEF DESCRIPTION OF THE DRAWINGS

[0035] Figure 1Shown is a schematic diagram of the method steps of the Python code hot reloading method in one embodiment of the present invention;

[0036] Figure 2 Shown is a schematic diagram of the method steps of the Python code hot reloading method in one embodiment of the present invention;

[0037] Figure 3 Shown is a schematic diagram of the method steps of the Python code hot reloading method in one embodiment of the present invention;

[0038] Figure 4 Shown is a schematic diagram of the method steps of the Python code hot reloading method in one embodiment of the present invention;

[0039] Figure 5 Shown is a schematic diagram of the method steps of the Python code hot reloading method in one embodiment of the present invention;

[0040] Figure 6 Shown is a schematic diagram of the business script content in an embodiment of the Python code hot reloading method of the present invention;

[0041] Figure 7 Shown is a schematic diagram of the structure of a Python code hot reload system in one embodiment of the present invention;

[0042] Figure 8 Shown is a schematic structural diagram of an electronic device in an embodiment of the present invention.

[0043] Component number description

[0044] Steps S102 to S108

[0045] Steps S202 to S206

[0046] Steps S302 to S306

[0047] Steps S402 to S404

[0048] Steps S502 to S504

[0049] 70 Python code hot reload system

[0050] 71 Tracking module

[0051] 72 Identification Module

[0052] 73 Reserved Modules

[0053] 74 Remove module DETAILED DESCRIPTION

[0054] The following describes the embodiments of the present invention by specific examples, and those skilled in the art can easily understand other advantages and effects of the present invention from the contents disclosed in this specification. The present invention can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present invention. It should be noted that the following embodiments and features in the embodiments can be combined with each other without conflict.

[0055] It should be noted that the illustrations provided in the following embodiments are only schematic illustrations of the basic concept of the present invention, and thus the drawings only show components related to the present invention rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component may be changed arbitrarily, and the component layout may also be more complicated.

[0056] See also Figure 1 In one embodiment of the invention, the Python code hot reloading method of the present invention includes the following steps:

[0057] Step S102, creating a hook function to track the import module of the Python interpreter;

[0058] Step S104, classifying based on feature data of the import module to identify whether the current import module is retained, the feature data including a first feature and a second feature;

[0059] Step S106, when the first feature is identified based on the feature data of the import module, the corresponding import module is retained;

[0060] Step S108: When the second feature is identified based on the feature data of the import module, the corresponding import module is removed.

[0061] It should be noted that, in this embodiment, the scenario to which the present application is applicable specifically starts a main process in the Python code and maintains a target process pool, wherein the target process pool includes multiple Python interpreter processes, and the main process is used to accept Python script execution requests and assign corresponding execution requests to idle Python interpreter working processes, thereby creating a dog function to track the import module of the Python interpreter before the working process executes the script. Then, the imported modules are classified and recorded according to their feature data, wherein the feature data includes a first feature and a second feature, and whether the current imported module needs to be retained is identified based on different feature data. Specifically, when the first feature is identified based on the feature data of the imported module, the corresponding imported module is retained, that is, it corresponds to retaining modules that cannot be modified (such as computing libraries, standard libraries), wherein the retained imported modules include pandas; when the second feature is identified based on the feature data of the imported module, the corresponding imported module is removed, that is, it corresponds to removing modifiable modules (such as computing scripts) from the Python interpreter when the script execution ends, wherein the removed imported modules include utils, wherein pandas corresponds to the computing library, and utils corresponds to the computing script.

[0062] Further, in one embodiment of the invention, if Figure 2 As shown, the creation of a hook function to track the import module of the Python interpreter specifically includes the following steps:

[0063] Step S202, during the execution of the Python script, rewrite the import behavior to replace it with a class function to obtain the hook function;

[0064] Step S204, recording the import modules involved in each import behavior into the class attributes based on the hook function;

[0065] Step S206, using the recorded module information of the imported module as a basis for maintaining the interpreter when the script execution ends.

[0066] It should be noted that, in this embodiment, creating a hook function is a major innovation in this embodiment. Specifically, the implementation principle is based on the rewriting and replacement (patch) of the import method of Python's built-in module import-lib and the __import__ method of built-ins, replacing them with a class function, that is, during the execution of the Python script, the import behavior is rewritten to replace it with a class function, thereby obtaining the hook function defined in this embodiment. Furthermore, all import (import module) behaviors during the execution of the Python script itself within the working process will be redirected to the class function - this is based on the general logic of Python's import operation, and the module information involved in each import behavior is recorded in the class attribute, corresponding to recording the import module involved in each import behavior in the class attribute based on the hook function, and using the recorded module information of the import module as the basis for maintaining the interpreter at the end of the script execution, that is, as the basis for maintaining the interpreter cache (sys.modules) at the end of the subsequent business script, specifically referred to as the Module-Context class.

[0067] Further, in one embodiment of the invention, if Figure 3 As shown, the classification based on the characteristic data of the import module to identify whether the current import module is retained specifically includes the following steps:

[0068] Step S302, identifying the type of the imported module, wherein the type includes pandas and utils;

[0069] Step S304, checking the path to which the import module belongs to obtain the characteristic data;

[0070] Step S306, based on the path to which it belongs, determine whether the current path is in the path list of the default module library of the Python interpreter. If so, the first feature is identified; if not, the second feature is identified.

[0071] Specifically, in this embodiment, the type of the imported module is described in the above embodiment examples, corresponding to a computing library, a standard library or a computing script. Specifically, the type includes, for example, the computing library pandas, and the computing script utils. Furthermore, the characteristic data of the imported module corresponds to the path to which the imported module belongs, so that it is possible to determine whether the current path is in the path list of the default module library of the Python interpreter based on the path to which it belongs. If so, the first feature is identified, that is, it corresponds to an unmodifiable module and needs to be retained; if not, the second feature is identified, that is, it corresponds to a modifiable module and needs to be removed.

[0072] Further, in one embodiment of the invention, if Figure 4 As shown, when the first feature is identified based on the feature data of the import module, the corresponding import module is retained, which specifically includes the following steps:

[0073] Step S402, after identifying the first feature based on the belonging path, determining that the current imported module is an unmodifiable module;

[0074] Step S404, recording the name of the current imported module into the class attribute of the Module-Context class, thereby retaining the corresponding imported module.

[0075] It should be noted that, in this embodiment, for the imported module pandas of the import operation, check whether the path to which the module belongs is in the path list of the default module library of the Python interpreter. If so, it means that it does not belong to the user's calculation script and is an unmodifiable module, because the default module of the single-process Python interpreter will not change. The current module pandas is an unmodifiable module, and the name is recorded in the corresponding class attribute of the Module-Context class as a module to be retained. In this import operation, due to the general logic of the Python import process, modules belonging to the pandas function library such as pandas.core, pandas.api will be recursively imported and will also be recorded in the modules to be retained.

[0076] It is worth mentioning that identifying the corresponding path is to check whether the "__path__ and __file__ attributes" of the imported module are in the path list of the default module library of the Python interpreter.

[0077] Further, in one embodiment of the invention, if Figure 5 As shown, when the second feature is identified based on the feature data of the import module, the corresponding import module is removed, which specifically includes the following steps:

[0078] Step S502, after identifying the second feature based on the belonging path, determining that the current imported module is a module to be removed;

[0079] Step S504, record the name of the currently imported module into the class attribute of the Module-Context class, and traverse and check the module cache of the current Python interpreter to remove the module to be removed.

[0080] It should be noted that, in this embodiment, for the imported module utils of the import operation, its path is checked. Specifically, as in the above embodiment, whether the "__path__ and __file__ attributes" of the imported module are in the path list of the default module library of the Python interpreter, wherein the path to which utils belongs does not belong to the path list of the default module library of the Python interpreter, and therefore recording the name as the module to be removed can avoid using the unupdated script content the next time the process executes the business script. After the execution of the script content of demo.py is completed, the module cache of the current Python interpreter, that is, sys.modules, is traversed and checked. During the traversal process, the name of each module is obtained, compared with the information recorded by the Module-Context class during the import process, and corresponding retention and removal actions are performed.

[0081] Furthermore, also due to the general logic of Python import, the modules involved in the business script utils.py will be recorded as modules to be retained or removed according to the classification standards in the above embodiment, that is, the corresponding non-modifiable modules need to be retained, and the modifiable modules need to be removed.

[0082] Furthermore, in one embodiment of the invention, this embodiment is based on the process of celery allocating tasks to subprocesses, wherein, for example, a python business script demo.py executed by a task needs to import the pandas computing library and the Helper class in another business script utils.py in its path.

[0083] Specifically, Figure 6 As shown in the figure, it is a schematic diagram of the business script content. For the first imported sentence "importpandas as pd", checking the path shows that the corresponding feature data is the first feature, that is, the current module pandas is an unmodifiable module, and the name is recorded in the corresponding class attribute of the Module-Context class as a module to be retained. Therefore, when executing the first import operation, since pandas and its submodules already exist in the cache of the Python interpreter, they will not be imported repeatedly, which can save the import overhead of the pandas module.

[0084] Furthermore, if Figure 6As shown, for the "from utils import Helper" imported in the second sentence, checking the corresponding path shows that its feature data is the second feature, that is, the path list that does not belong to the default module library of the Python interpreter. Therefore, the name is recorded as the module to be removed. This is to avoid using the unupdated script content when the business script is executed next time in the process, so it needs to be removed. That is, when the second sentence is executed, since it is not in the cache of the Python interpreter, the script needs to be re-imported, thereby ensuring the timeliness of the business script utils.py.

[0085] An embodiment of the present application also provides a Python code hot reloading system, which can implement the Python code hot reloading method described in the present application. However, the implementation device of the Python code hot reloading method described in the present application includes but is not limited to the structure of the Python code hot reloading system listed in the present embodiment. All structural deformations and replacements of the prior art made according to the principles of the present application are included in the protection scope of the present application.

[0086] See also Figure 7 In one embodiment, this embodiment provides a Python code hot reload system 70, the system comprising:

[0087] A tracing module 71 is used to create a hook function to trace the import module of the Python interpreter;

[0088] The identification module 72 is used to classify based on the feature data of the import module to identify whether the current import module is retained, wherein the feature data includes a first feature and a second feature, wherein:

[0089] A retaining module 73, configured to retain the corresponding import module when the first feature is identified based on the feature data of the import module, wherein the retained import module includes pandas;

[0090] The removal module 74 is used to remove the corresponding import module when the second feature is identified based on the feature data of the import module, wherein the removed import module includes utils.

[0091] It should be noted that those skilled in the art should also understand that Figure 7 The division of the modules in the embodiment is merely a division of logical functions, and in actual implementation, all or part of them can be integrated into one or more physical entities, and these modules can be implemented entirely in the form of software called by processing elements, or entirely in the form of hardware, or some modules can be implemented in the form of software called by processing elements, and some modules can be implemented in the form of hardware.

[0092] Specifically, in this embodiment, the tracking module 71 is used to create a hook function to track the import module of the Python interpreter. Specifically, during actual execution, it is necessary to rewrite the import behavior during the execution of the Python script and replace it with a class function to obtain the hook function, and based on the hook function, the import module involved in each import behavior is recorded into the class attribute, so that the module information of the recorded import module is used as the basis for maintaining the interpreter when the script execution ends.

[0093] It should be noted that, in this embodiment, creating a hook function is a major innovation in this embodiment. Specifically, the implementation principle is based on the rewriting and replacement (patch) of the import method of Python's built-in module import-lib and the __import__ method of built-ins, replacing them with a class function, that is, during the execution of the Python script, the import behavior is rewritten to replace it with a class function, thereby obtaining the hook function defined in this embodiment. Furthermore, all import (import module) behaviors during the execution of the Python script itself within the working process will be redirected to the class function - this is based on the general logic of Python's import operation, and the module information involved in each import behavior is recorded in the class attribute, corresponding to recording the import module involved in each import behavior in the class attribute based on the hook function, and using the recorded module information of the import module as the basis for maintaining the interpreter at the end of the script execution, that is, as the basis for maintaining the interpreter cache (sys.modules) at the end of the subsequent business script, specifically referred to as the Module-Context class.

[0094] Furthermore, the identification module 72 is used to classify based on the feature data of the imported module to identify whether the current imported module is retained, the feature data including a first feature and a second feature. During actual execution, the type of the imported module is specifically identified, wherein the types include pandas and utils, thereby checking the path to which the imported module belongs to obtain the feature data; and then judging whether the current path is in the path list of the default module library of the Python interpreter based on the path to which it belongs, if so, the first feature is identified; if not, the second feature is identified.

[0095] Furthermore, the retention module 73 is used to retain the corresponding import module when the first feature is identified based on the feature data of the import module, wherein the retained import modules include pandas. During actual execution, after the first feature is identified based on the belonging path, it is determined that the current import module is an unmodifiable module, and the name of the current import module is recorded in the class attribute of the Module-Context class, thereby retaining the corresponding import module.

[0096] It should be noted that, in this embodiment, for the import module pandas of the import operation, check whether the module's belonging path is in the path list of the default module library of the Python interpreter. If so, it means that it does not belong to the user's calculation script and is an unmodifiable module, because the default module of the single-process Python interpreter will not change. The current module pandas belongs to an unmodifiable module, and the name is recorded in the corresponding class attribute of the Module-Context class as a module to be retained. In this import operation, due to the general logic of the Python import process, pandas.core, pandas.api, etc., which belong to the pandas function library, will be recursively imported, and will also be recorded in the module to be retained. Among them, identifying the corresponding belonging path specifically checks whether the "__path__ and __file__ attributes" of the imported module are in the path list of the default module library of the Python interpreter.

[0097] It should be noted that, in this embodiment, the type of the imported module is described in the above embodiment examples, corresponding to a computing library, a standard library or a computing script. Specifically, the type includes, for example, the computing library pandas, and the computing script utils. Furthermore, the feature data of the imported module corresponds to the path to which the imported module belongs, so that it is possible to determine whether the current path is in the path list of the default module library of the Python interpreter based on the path to which it belongs. If so, the first feature is identified, that is, it corresponds to an unmodifiable module and needs to be retained; if not, the second feature is identified, that is, it corresponds to a modifiable module and needs to be removed.

[0098] Furthermore, the removal module 74 is used to remove the corresponding import module when the second feature is identified based on the feature data of the import module, wherein the removed import modules include utils. During actual execution, after the second feature is identified based on the belonging path, the current import module is judged to be the module to be removed, thereby recording the name of the current import module in the class attribute of the Module-Context class, and traversing and checking the module cache of the current Python interpreter to remove the module to be removed.

[0099] It should be noted that, in this embodiment, for the imported module utils of the import operation, its path is checked. Specifically, as in the above embodiment, whether the "__path__ and __file__ attributes" of the imported module are in the path list of the default module library of the Python interpreter, wherein the path to which utils belongs does not belong to the path list of the default module library of the Python interpreter, and therefore recording the name as the module to be removed can avoid using the unupdated script content the next time the process executes the business script. After the execution of the script content of demo.py is completed, the module cache of the current Python interpreter, that is, sys.modules, is traversed and checked. During the traversal process, the name of each module is obtained, compared with the information recorded by the Module-Context class during the import process, and corresponding retention and removal actions are performed.

[0100] Furthermore, also due to the general logic of Python import, the modules involved in the business script utils.py will be recorded as modules to be retained or removed according to the classification standards in the above embodiment, that is, the corresponding non-modifiable modules need to be retained, and the modifiable modules need to be removed.

[0101] See also Figure 8 This embodiment provides an electronic device. Specifically, the electronic device includes at least a memory and a processor connected via a bus, wherein the memory is used to store a computer program, and the processor is used to execute the computer program stored in the memory to execute all or part of the steps in the aforementioned method embodiment.

[0102] In summary, the Python code hot reloading method, system and medium of the present invention avoid the additional performance overhead caused by repeatedly starting the Python interpreter. For systems that need to execute a large number of scripts with short execution time at high frequency, the performance of the system can be greatly improved, the import overhead of the pandas module can be saved, and the timeliness of business script utils is guaranteed.

[0103] In the several embodiments provided in the present application, it should be understood that the disclosed system, device or method can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of modules / units is only a logical function division. There may be other division methods in actual implementation, such as multiple modules or units can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or modules or units, which can be electrical, mechanical or other forms.

[0104] The modules / units described as separate components may or may not be physically separated, and the components displayed as modules / units may or may not be physical modules, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules / units may be selected according to actual needs to achieve the purpose of the embodiments of the present application. For example, the functional modules / units in the various embodiments of the present application may be integrated into one processing module, or each module / unit may exist physically separately, or two or more modules / units may be integrated into one module / unit.

[0105] Those of ordinary skill in the art should further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in the above description according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.

[0106] The present application embodiment also provides a computer-readable storage medium. A person of ordinary skill in the art can understand that all or part of the steps in the method for implementing the above embodiment can be completed by a program to instruct the processor, and the program can be stored in a computer-readable storage medium, and the storage medium is a non-transitory medium, such as a random access memory, a read-only memory, a flash memory, a hard disk, a solid-state hard disk, a magnetic tape, a floppy disk, an optical disc, and any combination thereof. The above storage medium can be any available medium that can be accessed by a computer or a data storage device such as a server or a data center that includes one or more available media integrated. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a digital video disc (DVD)), or a semiconductor medium (e.g., a solid-state hard disk (SSD)), etc.

[0107] The present application embodiment may also provide a computer program product, the computer program product including one or more computer instructions. When the computer instructions are loaded and executed on a computing device, the process or function described in the embodiment of the present application is generated in whole or in part. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium, for example, the computer instructions may be transmitted from a website site, computer or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer or data center.

[0108] When the computer program product is executed by a computer, the computer executes the method described in the above method embodiment. The computer program product may be a software installation package, and when the above method is required, the computer program product may be downloaded and executed on a computer.

[0109] The descriptions of the processes or structures corresponding to the above-mentioned figures have different emphases. For parts that are not described in detail in a certain process or structure, please refer to the relevant descriptions of other processes or structures.

[0110] The above embodiments are merely illustrative of the principles and effects of the present application and are not intended to limit the present application. Anyone familiar with the technology may modify or change the above embodiments without violating the spirit and scope of the present application. Therefore, all equivalent modifications or changes made by a person of ordinary skill in the art without departing from the spirit and technical ideas disclosed in the present application shall still be covered by the claims of the present application.

Claims

1. A Python code hot reloading method, characterized in that: The method is applied to start a main process in Python code and maintain a target process pool, wherein the target process pool includes processes of multiple Python interpreters, and the method includes: A hook function is created to track the import module of the Python interpreter, which specifically includes: during the execution of the Python script, the import behavior is rewritten to replace it with a class function to obtain the hook function; based on the hook function, the import module involved in each import behavior is recorded into the class attribute; and the module information of the recorded import module is used as the basis for maintaining the interpreter when the script execution ends; Classification is performed based on the feature data of the imported module to identify whether the current imported module is retained, the feature data includes a first feature and a second feature, and the classification based on the feature data of the imported module to identify whether the current imported module is retained specifically includes: identifying the type of the imported module, wherein the types include pandas and utils; checking the path to which the imported module belongs to obtain the feature data; based on the path to which it belongs, determining whether the current path is in the path list of the default module library of the Python interpreter, wherein if so, the first feature is identified; if not, the second feature is identified, wherein, When the first feature is identified based on the feature data of the imported module, the corresponding imported module is retained, specifically including: after the first feature is identified based on the belonging path, the current imported module is judged to be an unmodifiable module; the name of the current imported module is recorded in the class attribute of the Module-Context class, so as to retain the corresponding imported module, wherein the imported module specifically recursively imports pandas.core and pandas.api when being imported, wherein the retained imported modules include pandas; When the second feature is identified based on the feature data of the imported module, the corresponding imported module is removed, specifically including: after identifying the second feature based on the belonging path, judging that the current imported module is the module to be removed; recording the name of the current imported module in the class attribute of the Module-Context class, and traversing and checking the module cache of the current Python interpreter to remove the module to be removed, wherein the removed imported modules include utils.

2. The Python code hot reloading method according to claim 1, characterized in that: The main process is used to accept Python script execution requests and distribute the corresponding execution requests to idle Python interpreter working processes.

3. A Python code hot reload system, characterized in that: The system is applied to start a main process in Python code and maintain a target process pool, wherein the target process pool includes processes of multiple Python interpreters, and the system includes: A tracing module is used to create a hook function to track the import module of the Python interpreter, which specifically includes: during the execution of the Python script, the import behavior is rewritten to replace it with a class function to obtain the hook function; based on the hook function, the import module involved in each import behavior is recorded into the class attribute; and the module information of the recorded import module is used as the basis for maintaining the interpreter at the end of the script execution; An identification module, used for classifying based on the feature data of the imported module to identify whether the current imported module is retained, the feature data including a first feature and a second feature, the classification based on the feature data of the imported module to identify whether the current imported module is retained specifically includes: identifying the type of the imported module, wherein the type includes pandas and utils; checking the path to which the imported module belongs to obtain the feature data; judging whether the current path is in the path list of the default module library of the Python interpreter based on the path to which it belongs, wherein if so, the first feature is identified; if not, the second feature is identified; A retention module, used for retaining the corresponding import module when the first feature is identified based on the feature data of the import module, specifically comprising: after the first feature is identified based on the belonging path, judging that the current import module is an unmodifiable module; recording the name of the current import module into the class attribute of the Module-Context class, thereby retaining the corresponding import module, wherein the import module specifically recursively imports pandas.core and pandas.api when importing, wherein the retained import modules include pandas; A removal module is used to remove the corresponding import module when the second feature is identified based on the feature data of the import module, specifically comprising: after identifying the second feature based on the belonging path, judging that the current import module is the module to be removed; recording the name of the current import module into the class attribute of the Module-Context class, and traversing and checking the module cache of the current Python interpreter to remove the module to be removed, wherein the removed import modules include utils.

4. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the Python code hot reloading method described in any one of claims 1 to 2 is implemented.

5. An electronic device, characterized in that: The electronic device comprises: a processor and a memory; wherein the memory is used to store a computer program, and the processor is used to execute the computer program stored in the memory, so that the electronic device executes the Python code hot reloading method as described in any one of claims 1 to 2.

Citation Information

Patent Citations

  • Data processing method and device, computer equipment, storage medium and program product

    CN116974583A