Method and device for running Python application and computing equipment
By creating a program replacement processing thread within the Python application process, and using metadata to determine and replace updated program units, the inflexibility of updating Python applications in existing technologies is solved, achieving both flexibility and stability in feature updates.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-10-23
- Publication Date
- 2026-03-17
AI Technical Summary
In existing technologies, patching vulnerabilities or extending functionality in Python applications requires intrusive code modifications, resulting in inflexible update methods and potentially impacting application stability and causing management chaos.
By creating a program replacement processing thread within the Python application's process, using metadata to determine the program units that need updating, and importing the corresponding updated program units for replacement execution, intrusive modifications to the original code are avoided.
It achieves flexibility and stability in updating Python application functions, improves the controllability of program unit management, and avoids direct modification of the original code.
Smart Images

Figure CN121680901A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a method, apparatus, and computing device for running Python applications. Background Technology
[0002] Python is a common programming language that is widely used in fields such as artificial intelligence and high-performance computing due to its concise syntax and high compatibility.
[0003] Applications written in Python are called Python applications. During the runtime of a Python application, updates can be made to its functionality, including bug fixes and feature extensions.
[0004] Currently, both vulnerability patching and feature expansion for Python applications require intrusive modifications to the application's code, meaning modifications to the original Python code are necessary. This makes updating Python applications less flexible in terms of functionality. Summary of the Invention
[0005] This application provides a method, apparatus, and computing device for running Python applications, which improves the flexibility of patching or extending the functionality of Python applications. The corresponding technical solution is as follows: Firstly, a method for running a Python application is provided, comprising: after a first process of the Python application starts, the first process creates a first thread for performing program replacement processing. The program replacement processing includes: in response to the process triggering an import operation of a first program unit, obtaining metadata of the first program unit, wherein the first process is a process created to run the Python application, and the metadata includes a program identifier; determining whether a metadata set includes the metadata of the first program unit, the metadata set including the metadata of the program unit to be updated; in response to the metadata set including the metadata of the first program unit, obtaining a second program unit corresponding to the first program unit, the second program unit being an updated version of the first program unit used to replace the execution of the first program unit; and importing the second program unit so that after the process triggers the execution of the first program unit, the second program unit is executed.
[0006] Here, program units such as the first program unit and the second program unit can refer to code segments, functions, etc. The first program unit is the program unit that needs to be updated, and the second program unit can be the program unit after the first program unit has been updated.
[0007] The technical solution provided in this application includes a first thread for program replacement processing. After the first process triggers the import of the first program unit, the first thread can determine whether the first program unit is one that needs functional updates based on its metadata. If so, it can trigger the import and execution of the second program unit. Therefore, if a functional update of the first program unit is required, a second program unit can be added to the Python application to replace the execution of the first program unit. This achieves functional updates of the first program unit without intrusive modifications to its code, thereby improving the flexibility of updating Python applications.
[0008] In one feasible approach, obtaining the second program unit corresponding to the first program unit includes: obtaining a second program identifier corresponding to the first program identifier of the first program unit in a first correspondence relationship, wherein the first correspondence relationship stores the program identifier of the program unit to be updated and the program identifier of the updated program unit, and the second program identifier is the program identifier of the second program unit; obtaining a storage location corresponding to the second program identifier in a second correspondence relationship, wherein the second correspondence relationship stores the program identifier of the updated program unit and the storage location of the updated program unit; and obtaining the second program unit based on the storage location of the second program unit. Here, the program identifier can be a program name. Thus, by recording the first correspondence relationship between the program identifiers of the first program unit and the second program unit, the second program unit used to replace the first program unit can be determined without intrusive modification of the first program unit. By recording the second correspondence relationship between the program identifier and the program unit storage location, the updated second program unit can be obtained, and the second program unit can be executed.
[0009] In one feasible approach, retrieving the second program unit based on its storage location includes: retrieving the first source code file corresponding to the second program unit based on its storage location. Before importing the second program unit, the method further includes: modifying the index relationship between the first program unit and the second source code file to a relationship between the first program unit and the first source code file, where the second source code file is the source code file corresponding to the first program unit. This allows the execution of the second program unit to be achieved by replacing the source code file of the first program unit, avoiding intrusive modifications to the first program unit.
[0010] In one feasible approach, obtaining the second program unit based on its storage location includes: obtaining the first bytecode file corresponding to the second program unit based on its storage location. Before importing the second program unit, the method further includes: modifying the index relationship between the first program unit and the second bytecode file to a relationship between the first program unit and the first bytecode file, where the second bytecode file is the bytecode file corresponding to the first program unit. This allows the execution of the first program unit by replacing its bytecode file, avoiding intrusive modifications to the first program unit.
[0011] In one possible implementation, the second program unit includes a first subroutine unit and a second subroutine unit. After importing the second program unit, the method further includes generating a third subroutine unit. The third subroutine unit executes the functions of the first program unit, the first subroutine unit implements functions that extend the functions of the first program unit, and the second subroutine unit calls the third subroutine unit. In this way, the first subroutine unit can be used to extend the functions of the first program unit, and the second subroutine unit can be used to implement the functions of the first program unit, without requiring intrusive modifications to the first program unit.
[0012] In one possible implementation, the above program replacement processing is based on a third program unit. The method further includes: after the second process of the Python application starts, the second process imports the third program unit in response to reading the first global variable; a second thread is created based on the third program unit, where the second thread performs the program replacement processing within the second process. In this way, the functionality to update the first program unit can be applied to other processes.
[0013] In one possible implementation, the first process is the main process of the Python application, and the first global variable is set by the first process; alternatively, the application receives a startup command that includes an instruction to set the first global variable. In this way, by setting the global variable, program replacement processing can be executed across multiple processes.
[0014] In one possible implementation, before the first process of the Python application starts, the process further includes: obtaining the metadata of the first program unit in the Python application and a second program unit for replacing the first program unit; and adding the metadata of the first program unit to the metadata set. Thus, by simply adding the metadata of the first program unit to the metadata set and adding the second program unit to the package of the Python application, the execution of the first program unit can be achieved.
[0015] In one possible implementation, the method further includes: a first thread for recording execution information of the second program unit, including the number of executions. The metadata also includes the version number of the program unit.
[0016] Secondly, a device for running Python applications is provided, the device comprising: The creation module is used to create the first thread for performing program replacement processing after the first process of the Python application starts. The program replacement processing is implemented by the get module, determine module, and import module. The module is used to retrieve the metadata of the first program unit in response to the import operation triggered by the process in which it resides. The first process is the process created by the Python application, and the metadata includes the program identifier. The determination module is used to determine whether the metadata set includes the metadata of the first program unit, and the metadata set includes the metadata of the program unit to be updated; The acquisition module is used to acquire a second program unit corresponding to the first program unit in response to the metadata in the metadata set that includes the first program unit. The second program unit is a program unit that updates the first program unit and is used to replace the execution of the first program unit. The import module is used to import a second program unit so that the second program unit is executed after the process triggers the execution of the first program unit.
[0017] In one possible implementation, the acquisition module is used to acquire, in a first correspondence, a second program identifier corresponding to a first program identifier of a first program unit, wherein the first correspondence stores the program identifier of the program unit to be updated and the program identifier of the updated program unit, and the second program identifier is the program identifier of the second program unit; in a second correspondence, acquire the storage location corresponding to the second program identifier, wherein the second correspondence stores the program identifier of the updated program unit and the storage location of the updated program unit; and acquire the second program unit according to the storage location of the second program unit.
[0018] In one possible implementation, the acquisition module is used to acquire the first source code file corresponding to the second program unit based on the storage location of the second program unit. The apparatus further includes a modification module, used to modify the index relationship between the first program unit and the second source code file to an index relationship between the first program unit and the first source code file, wherein the second source code file is the source code file corresponding to the first program unit.
[0019] In one possible implementation, the acquisition module is used to acquire the first bytecode file corresponding to the second program unit based on the storage location of the second program unit. The apparatus further includes a modification module, used to modify the index relationship between the first program unit and the second bytecode file to an index relationship between the first program unit and the first bytecode file, where the second bytecode file is the bytecode file corresponding to the first program unit.
[0020] In one possible implementation, the above apparatus further includes a generation module for generating a third subroutine unit, wherein the third subroutine unit is used to execute the function of the first program unit, the first subroutine unit is used to implement the function of extending the first program unit, and the second subroutine unit is used to call the third subroutine unit.
[0021] In one possible implementation, the program replacement process is implemented based on a third program unit, and the creation module is also used to import the third program unit in response to reading the first global variable after the second process of the Python application starts; a second thread is created based on the third program unit, wherein the second thread performs the program replacement process in the second process.
[0022] In one possible implementation, the first process is the main process of the Python application, and the first global variable is set by the first process; alternatively, the startup command of the Python application is received, which includes instructions to set the first global variable.
[0023] In one possible implementation, the apparatus further includes an adding module for acquiring metadata of a first program unit in a Python application and a second program unit for replacing the first program unit, and adding the metadata of the first program unit to the metadata set.
[0024] In one possible implementation, the above-described apparatus further includes a recording module for the first thread to record execution information of the second program unit, the execution information including the number of executions.
[0025] In one feasible approach, the metadata also includes the version number of the program unit.
[0026] Thirdly, a computing device is provided, the computing device including a memory and a processor, the memory storing at least one instruction, the processor executing at least one instruction for performing the method provided in the first aspect and / or any implementable manner of the first aspect.
[0027] Fourthly, a computer program product comprising instructions is provided, characterized in that, when the computer program product is run on a computing device, it causes the computing device to perform the method provided in the first aspect and / or any implementable manner of the first aspect.
[0028] Fifthly, a computer-readable storage medium is provided that stores computer program code, which, when executed by a computing device, causes the computing device to perform the methods provided in the first aspect and / or any implementable manner of the first aspect.
[0029] It should be understood that the technical effects achieved by the technical solutions provided by the second to fifth aspects of this application and their corresponding possible implementations can be found in the above description of the technical effects achieved by the technical solutions provided by the first aspect and their corresponding possible implementations, and will not be repeated here. Attached Figure Description
[0030] Figure 1 This application provides a flowchart of a method for running a Python application. Figure 2 This is a schematic diagram illustrating the execution of a Python application according to an embodiment of this application; Figure 3 This is a schematic diagram illustrating the execution of a Python application according to an embodiment of this application; Figure 4 This application provides an embodiment of a device for running Python applications; Figure 5 This is a schematic diagram of the structure of a computing device provided in an embodiment of this application; Figure 6 This is a schematic diagram of the structure of a computing device cluster provided in an embodiment of this application; Figure 7 This is a schematic diagram of the structure of a computing device cluster provided in an embodiment of this application. Detailed Implementation
[0031] To make the objectives, technical solutions, and advantages of this application clearer, the embodiments of this application will be described in further detail below with reference to the accompanying drawings.
[0032] Python is a common programming language, widely used in fields such as artificial intelligence and high-performance computing due to its concise syntax and high compatibility. Applications written in Python are called Python applications. In one example, a Python application's package can consist of multiple packages containing program files (such as .py or .pyc files), and each .py file can contain multiple functions that implement different functionalities. Python applications can be applications running in a terminal or services running on a cloud server.
[0033] During the runtime of a Python application, features can be updated, including bug fixes and feature extensions. Currently, both bug fixes and feature extensions require intrusive modifications to the Python application's code, making feature updates inflexible.
[0034] Making intrusive modifications to Python application code not only makes the way Python applications can be updated less flexible, but also leads to chaotic code management. A large number of intrusive modifications may also affect the stability of Python applications.
[0035] In the method for running a Python application provided in this application embodiment, if it is necessary to update the functionality of a certain program unit of the Python application, a program file for the updated program unit can be prepared and added to the Python application's package to obtain the updated Python application. Here, a program unit can refer to a Python application code segment or function, etc. During the running of the updated Python application, an update management program is set up in the Python application, which can, based on the updated program unit's program file, implement the execution of the updated program unit instead of the corresponding original program unit. This avoids intrusive modifications to the original program unit's code, ensuring flexibility in updating the Python application's functionality, improving the controllability of program unit management within the Python application, and enhancing the stability of the Python application's operation. The following is combined with... Figure 1 The method for running Python applications provided in the embodiments of this application will be described. Figure 1 This application provides a flowchart of a method for running a Python application, as illustrated in the embodiments below. Figure 1 As shown, the method includes: Step 101: After the first process of the Python application starts, the first process creates a first thread for performing program replacement processing.
[0036] In step 101, the Python application can be the updated Python application, that is, the Python application with the updated program units added. The process of adding updated program units to the Python application will be introduced here first. The Python application may include an update management program that performs program replacement processing, which is used to determine the program units in the Python application that need to be updated and to implement the execution of the updated program units to replace the program units that need to be updated.
[0037] In one example, the first process can be the main process of the Python application. After starting, the first process can import the update manager and run the update manager through a first thread. The processing of the update manager execution can include steps 102 to 104 as follows.
[0038] Step 102: The update management program responds to the import operation of the first program unit triggered by the process it is in and obtains the metadata of the first program unit. The first process is the process created by running the Python application, and the metadata includes the program identifier.
[0039] In implementation, the update management program can register hook functions through the import interface to achieve its functionality. In the first process, before any program unit is executed, it needs to be imported through the import interface. In this way, the update management program can use hook functions to determine each program unit imported in the first process. Taking the first program unit as an example, after importing it into the first process, the update management program can obtain the metadata of the first program unit and determine whether it is a program unit that needs a functional update based on this metadata. For example, the metadata corresponding to the first program unit may include the program identifier of the first program unit, which may include the package name, file name, function name, etc., of the first program unit.
[0040] Step 103: The update management program determines whether the metadata set includes the metadata of the first program unit. The metadata set includes the metadata of the program unit to be updated.
[0041] The metadata collection stores the metadata of program units that need feature updates. For example, each time a Python application is updated, the metadata of the program units that need feature updates can be added to the metadata collection.
[0042] During implementation, after obtaining the metadata of the first program unit, the update management program determines whether the metadata of the first program unit is matched with the metadata set, that is, whether the metadata of the first program unit is stored in the metadata set. If the metadata set contains the metadata of the first program unit, it means that the first program unit needs a functional update, that is, it needs to be patched or its functionality extended. If the metadata set does not contain the metadata of the first program unit, it means that the first program unit does not need a functional update.
[0043] Step 104: In response to the metadata set including the metadata of the first program unit, the update management program obtains the second program unit corresponding to the first program unit. The second program unit is a program unit that updates the first program unit and is used to replace the execution of the first program unit.
[0044] The second program unit is a program unit that updates the functionality of the first program unit. It can be added to the Python application's package when the Python application is updated. In other words, in this embodiment, there is no need to make intrusive modifications to the Python application program. When it is necessary to update the functionality of the Python application unit, the updated program unit can be directly added to the Python application's package.
[0045] For example, the updated Python application's package can also record a first correspondence between the metadata of the first program unit and the metadata of the second program unit. After determining that the first program unit is the program unit that needs a functional update, the update management program can determine the metadata of the second program unit corresponding to the metadata of the first program unit from the first correspondence. Further, based on the metadata of the second program unit, the storage location of the second program unit can be obtained, and thus the second program unit can be retrieved. This storage location can be the storage location of the program file corresponding to the second program unit on the hard drive, and retrieving the second program unit can involve reading the corresponding program file. For example, the updated Python program can store a second correspondence between metadata and storage location. The first thread can find the storage location of the second program unit in the second correspondence based on the metadata of the second program unit, and then read the corresponding program file.
[0046] Step 105: Update the management program to import the second program unit so that the second program unit is executed after the process triggers the execution of the first program unit.
[0047] In implementation, after the update management program determines that the first program unit is the one requiring a functional update, it can trigger the import of the second program unit to replace the first program unit. The import in step 105 can refer to loading the program file corresponding to the second program unit into the memory of the first process. Thus, if a thread in the first process needs to execute the first program unit, it can execute the second program unit based on the program file of the second program unit stored in the memory of the first process. Since the second program unit is the updated version of the first program unit, the actual execution effect is the same as the updated version of the first program unit.
[0048] In Python applications, program files include source code files or bytecode files.
[0049] When the program file is a source code file (.py file), technicians can prepare the first source code file corresponding to the second program unit in advance, add the first source code file to the Python application's package, and obtain the updated Python application. In the above second correspondence, the program identifier of the second program unit and the first source code file can be stored accordingly.
[0050] In Python applications, an index relationship between a first program unit and a second source code file can be maintained, where the second source code file is the source code file corresponding to the first program unit. After writing the first source code file corresponding to the second program unit into the process memory of the first process, the index relationship between the first program unit and the second source code file can be modified to that between the first program unit and the first source code file. Thus, when the first program unit is executed, the execution of the first source code file in the process memory can be triggered based on the modified index relationship, thereby enabling the execution of the second program unit.
[0051] When the program file is a bytecode file (pyc file), technicians can prepare the first bytecode file corresponding to the second program unit in advance, add the first bytecode file to the Python application's package, and obtain the updated Python application. In the aforementioned second correspondence, the program identifier of the second program unit and the first bytecode file can be stored accordingly. The first bytecode file corresponding to the second program unit can be obtained by compiling the first source code file.
[0052] In Python applications, an index relationship between a first program unit and a second bytecode file can be maintained, where the second bytecode file is the bytecode file corresponding to the first program unit. After writing the first bytecode file corresponding to the second program unit into the process memory of the first process, the index relationship between the first program unit and the second bytecode file can be modified to that between the first program unit and the first bytecode file. Thus, when the first program unit is executed, the execution of the first bytecode file in the process memory can be triggered based on the modified index relationship, thereby enabling the execution of the second program unit.
[0053] Figure 2 This is a schematic diagram illustrating the execution of a Python application according to an embodiment of this application. Figure 2 As shown, taking the third thread in the first process as an example, when the third thread imports the first program unit through the import interface, the update management program of the first thread can obtain the metadata of the first program unit. If the metadata set determines that the first program unit needs a functional update, a second program unit to replace the first program unit can be determined in the first correspondence. Then, in the second correspondence, the storage location of the second program unit is determined, and the first source code file (or first bytecode file) of the second program unit stored on the hard disk is loaded into the process memory according to the storage location, and the association between the first program unit and the second source code file (or second bytecode file) is modified. In this way, after the third process triggers the execution of the first program unit, it actually executes the second program unit.
[0054] In this embodiment, when patching or extending the functionality of a program unit is required, the patched or extended program unit can be directly added to the Python application's package. Then, the Python application's program replacement mechanism allows the execution of the updated program unit to be triggered by the original program unit, without requiring intrusive modifications to the original program unit's code. This improves the flexibility of updating the Python application's functionality. It is understood that in this embodiment, the functionality update of the program unit is completed after its import is triggered. Compared to monkey patching in some embodiments, this does not affect the loading order of program units or prevent the omission of program unit replacements.
[0055] In one feasible approach, the Python application can be updated before the updated application is executed. This involves adding the first source code file or first bytecode file corresponding to the second program unit to the Python application's package. Metadata for the first program code can also be added to the metadata collection. This way, technicians only need to prepare the first source code file or first bytecode file for the second program unit based on the first program unit, without requiring intrusive modifications to the Python application. This improves the flexibility of updating the Python application and enhances its stability.
[0056] In one example, a patch plugin list can be set up within the Python application to manage updates uniformly. This patch plugin list can store metadata corresponding to the updated program units in the Python application, such as the metadata set described in the above embodiment. The patch plugin list can also include the correspondence between the metadata of the updated program units and their storage locations, such as the second correspondence described in the above embodiment. Figure 3 This is a schematic diagram illustrating the running of a Python application provided in an embodiment of this application, such as... Figure 3 As shown, a plugin patching component runs in the Python application, and the functions A.fun and B.fun have been updated. This plugin patching component includes the aforementioned update manager. When the Python application starts running, the update manager can register hook functions through the import interface provided by the Python interpreter and can load the metadata of the updated program units in the plugin patch list. During the Python application's execution, the Python process normally imports the functions A.fun and B.fun. When importing A.fun and B.fun, the hook functions of the Python interpreter's import interface can call the update manager, passing in the metadata of A.fun and B.fun, such as their function names. If the update manager finds the metadata of function A.fun or function B.fun in the metadata of the loaded updated program unit, it can replace the execution of the unupdated function A.fun or function B with the updated function A.fun or function B according to the location information of the updated function A.fun or function B recorded in the plugin patch list.
[0057] In one example, the metadata within a program unit can also include a version number. This way, the metadata set can record not only the program identifier of the program unit to be updated but also its version number, which could be the version number of the Python application. In another example, the metadata of the updated program unit can also include a version number. When the version number of the updated program unit matches the version number of an unupdated program unit in the Python application, the updated program unit can be used to replace the unupdated program unit. If the version number of the updated program unit does not match the version number of the unupdated program unit in the Python application, the replacement of the unupdated program unit can be avoided, and an error can be reported. Thus, by adding version numbers to program units, the flexibility of managing feature updates to the Python application can be further increased.
[0058] In one example, the update management program may also include recording execution information for updated program units, such as the number of times the updated program unit was executed in the Python application, the execution time, etc. It may also record related information about the updated program units, such as the current number of updated program units and the number of updated program units that took effect during the execution of the Python application. In implementation, technicians can use Python commands to query the execution information and related information of updated program units to facilitate their management.
[0059] In this application embodiment, a method for extending the function of a first program unit is also provided, that is, a first subroutine unit for extending the function of the first program unit is set in a second program unit, and a second subroutine unit for calling the function of the original first program unit is provided, so as to realize the function of the original first program unit.
[0060] In implementation, if the second program unit needs to extend the functionality of the first program unit, a first identifier indicating the functional extension can be added to the metadata of the second program unit. Thus, when the update management program determines that the metadata of the second program unit includes the first identifier, it can determine that the second program unit is used to extend the functionality of the first program unit. Therefore, the update management program can store the first program unit in memory as a third subroutine unit. Within the second program unit, the second subroutine unit can call the third subroutine unit, thereby executing the first program unit.
[0061] In one example, the number of first subroutine units is unlimited. These units can include program units that call both pre-functions (`pre`) and post-functions (`post`) in the Python application. The `pre` function is executed before the first program unit, and the `post` function is executed after the second program unit. The `pre` and `post` functions can be configured by technical personnel based on the actual business scenario.
[0062] For example, if a Python application is running a large language model in the cloud, to expand the operational capabilities of the large language model and record the query latency of each request, an updated function can be set for the function that executes the large language model's inference. This updated function includes a `pre` function, a `post` function, and a function that calls the function executing the large language model's inference. In this way, before the large language model performs inference on each query, it can start a timer using the `pre` function, and after inference, it can stop the timer using the `post` function and return the timeout. This allows for the recording of the query latency for each request.
[0063] For example, in a cloud scenario, a Python application might rely on components that create a Hypertext Transfer Protocol Secure (HTTPS) server. When these components start and create the HTTPS server, they need to load a certificate. If the certificate requires password encryption protection, an updated function can be set for the function that loads the certificate. This updated function includes a `post` function and a function that calls the function that loads the certificate. This way, after loading the certificate, the `post` function can be used to encrypt it.
[0064] This application provides a method for triggering an update manager to take effect. In one possible implementation, the main program of a Python application may include statements that import the update manager. This allows the update manager to be executed within the main process of the Python application. For secondary processes within the Python application, a global variable can be set to indicate the loading of the update manager. This global variable can be referred to as the first global variable. The first global variable can be set by the main process (the aforementioned first process) after the Python application performs a feature update. Alternatively, if the Python application is a service running on a server, it can also be set via the Python application's startup command.
[0065] For example, in a Python application's second process (a secondary process), the `user.custommize.py` file can be executed to query global variables. If the first global variable is found, the third program unit (the program unit corresponding to the update manager mentioned above) can be implicitly imported, thus importing the update manager into the second process. In this way, the second process can create a second thread to execute the update manager, achieving the same program replacement processing as in the first process. This allows program replacement processing to be implemented across various processes within a Python application.
[0066] Based on the same technical concept, embodiments of this application also provide an apparatus for running Python applications. Figure 4 This application provides an embodiment of a device for running Python applications, such as... Figure 4 As shown, the device includes: Create module 410, which is used to create a first thread for performing program replacement processing after the first process of the Python application starts. The program replacement processing is implemented by get module 420, determine module 430 and import module 440. The module 420 is used to obtain the metadata of the first program unit in response to the import operation of the first program unit triggered by the process in which it is located. The first process is the process created by running the Python application, and the metadata includes the program identifier. The determination module 430 is used to determine whether the metadata set includes the metadata of the first program unit, and the metadata set includes the metadata of the program unit to be updated; The acquisition module 420 is used to acquire a second program unit corresponding to the first program unit in response to the metadata in the metadata set that includes the first program unit. The second program unit is a program unit that updates the first program unit and is used to replace the execution of the first program unit. Import module 440 is used to import a second program unit so that the second program unit is executed after the process triggers the execution of the first program unit.
[0067] In one possible implementation, the acquisition module 420 is used to: acquire, in the first correspondence, a second program identifier corresponding to the first program identifier of the first program unit, wherein the first correspondence stores the program identifier of the program unit to be updated and the program identifier of the updated program unit, and the second program identifier is the program identifier of the second program unit; acquire, in the second correspondence, a storage location corresponding to the second program identifier, wherein the second correspondence stores the program identifier of the updated program unit and the storage location of the updated program unit; and acquire the second program unit according to the storage location of the second program unit.
[0068] In one possible implementation, the acquisition module 420 is used to acquire the first source code file corresponding to the second program unit based on the storage location of the second program unit. The apparatus further includes a modification module, used to modify the index relationship between the first program unit and the second source code file to an index relationship between the first program unit and the first source code file, wherein the second source code file is the source code file corresponding to the first program unit.
[0069] In one possible implementation, the acquisition module 420 is used to acquire the first bytecode file corresponding to the second program unit based on the storage location of the second program unit. The apparatus further includes a modification module, used to modify the index relationship between the first program unit and the second bytecode file to an index relationship between the first program unit and the first bytecode file, where the second bytecode file is the bytecode file corresponding to the first program unit.
[0070] In one possible implementation, the above apparatus further includes a generation module for generating a third subroutine unit, wherein the third subroutine unit is used to execute the function of the first program unit, the first subroutine unit is used to implement the function of extending the first program unit, and the second subroutine unit is used to call the third subroutine unit.
[0071] In one possible implementation, the program replacement process is implemented based on a third program unit, and the creation module 410 is also used to import the third program unit in response to reading the first global variable after the second process of the Python application starts; and to create a second thread based on the third program unit, wherein the second thread performs the program replacement process in the second process.
[0072] In one possible implementation, the first process is the main process of the Python application, and the first global variable is set by the first process; alternatively, the startup command of the Python application is received, which includes instructions to set the first global variable.
[0073] In one possible implementation, the above-described apparatus further includes an addition module for acquiring metadata of a fourth program unit in a Python application and a fifth program unit for replacing the fourth program unit. Add metadata for the fifth program unit to the metadata collection.
[0074] In one possible implementation, the above-described apparatus further includes a recording module for the first thread to record execution information of the second program unit, the execution information including the number of executions.
[0075] In one feasible approach, the metadata also includes the version number of the program unit.
[0076] The creation module 410, acquisition module 420, determination module 430, and import module 440 can be implemented in software or hardware. For example, the implementation of the creation module 410 will be described below. Similarly, the implementation of the acquisition module 420, determination module 430, and import module 440 can refer to the implementation of the creation module 410.
[0077] As an example of a software functional unit, module 410 can include code running on a computing instance. The computing instance can include at least one of a physical host (computing device), a virtual machine, or a container. Furthermore, the aforementioned computing instance can be one or more. For example, module 410 can include code running on multiple hosts / virtual machines / containers. It should be noted that the multiple hosts / virtual machines / containers used to run the code can be distributed within the same region or in different regions. Further, the multiple hosts / virtual machines / containers used to run the code can be distributed within the same availability zone (AZ) or in different AZs, each AZ including one or more geographically proximate data centers. Typically, a region can include multiple AZs.
[0078] Similarly, multiple hosts / virtual machines / containers used to run this code can be distributed within the same Virtual Private Cloud (VPC) or across multiple VPCs. Typically, a VPC is set up within a region. Communication between two VPCs within the same region, as well as between VPCs in different regions, requires a communication gateway to be set up within each VPC to enable interconnection between VPCs.
[0079] As an example of a hardware functional unit, module 410 can include at least one computing device, such as a server. Alternatively, module 410 can also be a device implemented using an application-specific integrated circuit (ASIC) or a programmable logic device (PLD). The PLD can be implemented using a complex programmable logical device (CPLD), a field-programmable gate array (FPGA), generic array logic (GAL), or any combination thereof.
[0080] The multiple computing devices included in creation module 410 can be distributed in the same region or in different regions. Similarly, the multiple computing devices included in creation module 410 can be distributed in the same Availability Zone (AZ) or in different AZs. Likewise, the multiple computing devices included in creation module 410 can be distributed in the same Virtual Private Cloud (VPC) or in multiple VPCs. These multiple computing devices can be any combination of computing devices such as servers, ASICs, PLDs, CPLDs, FPGAs, and GALs.
[0081] It should be noted that, in other embodiments, the creation module 410 can be used to execute any step in the method of running a Python application, the acquisition module 420 can be used to execute any step in the method of running a Python application, and the determination module 430 can be used to execute any step in the method of running a Python application. The steps implemented by the creation module 410, acquisition module 420, determination module 430, and import module 440 can be specified as needed. By implementing different steps in the method of running a Python application through the creation module 410, acquisition module 420, determination module 430, and import module 440, all functions of the device for running a Python application can be realized.
[0082] This application also provides a computing device 100. For example... Figure 5 As shown, the computing device 100 includes a bus 102, a processor 104, a memory 106, and a communication interface 108. The processor 104, the memory 106, and the communication interface 108 communicate with each other via the bus 102. The computing device 100 can be a server or a terminal device. It should be understood that this application does not limit the number of processors and memories in the computing device 100.
[0083] Bus 102 can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. Buses can be categorized as address buses, data buses, control buses, etc. For ease of representation, Figure 3 The bus 102 may be represented by a single line, but this does not mean that there is only one bus or one type of bus. The bus 102 may include a path for transmitting information between various components of the computing device 100 (e.g., memory 106, processor 104, communication interface 108).
[0084] The processor 104 may include any one or more processors such as a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), or a digital signal processor (DSP).
[0085] Memory 106 may include volatile memory, such as random access memory (RAM). Memory 106 may also include non-volatile memory, such as read-only memory (ROM), flash memory, hard disk drive (HDD), or solid state drive (SSD).
[0086] The memory 106 stores executable program code, which the processor 104 executes to implement the functions of the aforementioned creation module 410, acquisition module 420, determination module 430, and import module 440, thereby realizing the method of running the Python application. That is, the memory 106 stores instructions for executing the method of running the Python application.
[0087] The communication interface 108 uses transceiver modules such as, but not limited to, network interface cards and transceivers to enable communication between the computing device 100 and other devices or communication networks.
[0088] This application also provides a computing device cluster. The computing device cluster includes at least one computing device. The computing device can be a server, such as a central server, an edge server, or a local server in a local data center. In some embodiments, the computing device can also be a terminal device such as a desktop computer, a laptop computer, or a smartphone.
[0089] like Figure 6 As shown, the computing device cluster includes at least one computing device 100. The memory 106 of one or more computing devices 100 in the computing device cluster may store the same instructions for executing methods to run Python applications.
[0090] In some possible implementations, the memory 106 of one or more computing devices 100 in the computing device cluster may also store partial instructions for executing methods to run Python applications. In other words, a combination of one or more computing devices 100 can jointly execute instructions for executing methods to run Python applications.
[0091] It should be noted that the memory 106 in different computing devices 100 within the computing device cluster can store different instructions, each used to execute a portion of the functions of the device running the Python application. That is, the instructions stored in the memory 106 of different computing devices 100 can implement the functions of creating module 410, obtaining module 420, determining module 430, and importing module 440, or one or more of these modules.
[0092] In some possible implementations, one or more computing devices in a computing device cluster can be connected via a network. This network can be a wide area network (WAN) or a local area network (LAN), etc. Figure 7 One possible implementation is shown. For example... Figure 7 As shown, the two computing devices 100A and 100B are connected via a network. Specifically, they are connected to the network through communication interfaces in each computing device. In this possible implementation, the memory 106 in computing device 100A stores instructions for executing the functions of the creation module 410. Simultaneously, the memory 106 in computing device 100B stores instructions for executing the functions of the acquisition module 420, the determination module 430, and the import module 440.
[0093] It should be understood that Figure 7 The functions of the computing device 100A shown can also be performed by multiple computing devices 100. Similarly, the functions of the computing device 100B can also be performed by multiple computing devices 100.
[0094] This application also provides another computing device cluster. The connection relationships between the computing devices in this computing device cluster can be similarly referred to... Figure 6 and Figure 7 The connection method of the computing device cluster is different in that the memory 106 of one or more computing devices 100 in the computing device cluster can store the same instructions for executing methods to run Python applications.
[0095] In some possible implementations, the memory 106 of one or more computing devices 100 in the computing device cluster may also store partial instructions for executing methods to run Python applications. In other words, a combination of one or more computing devices 100 can jointly execute instructions for executing methods to run Python applications.
[0096] This application also provides a computer program product containing instructions. The computer program product may be a software or program product containing instructions capable of running on a computing device or stored on any usable medium. When the computer program product is run on at least one computing device, it causes the at least one computing device to execute a method for running a Python application.
[0097] This application also provides a computer-readable storage medium. The computer-readable storage medium can be any available medium that a computing device can store, or a data storage device such as a data center that includes one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid-state drive). The computer-readable storage medium includes instructions that instruct the computing device to execute a method for running a Python application.
[0098] In this application, the terms "first," "second," etc., are used to distinguish identical or similar items that have essentially the same function. It should be understood that there is no logical or temporal dependency between "first," "second," and "nth," nor does it limit the quantity or order of execution. It should also be understood that although the following description uses the terms "first," "second," etc., to describe various elements, these elements should not be limited by the terms. These terms are merely used to distinguish one element from another.
[0099] It should also be understood that, in the various embodiments of this application, the sequence number of each process does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application.
[0100] In this application, the term "at least one" means one or more, and the term "multiple" means two or more. For example, multiple second devices means two or more second devices. The terms "system" and "network" are often used interchangeably herein.
[0101] It should be understood that the terminology used in the description of the various examples herein is for the purpose of describing particular examples only and is not intended to be limiting. As used in the description of the various examples and the appended claims, the singular forms “a” and “the” are intended to include the plural forms as well, unless the context clearly indicates otherwise.
[0102] It should also be understood that the term "and / or" as used herein refers to and covers any and all possible combinations of one or more of the associated listed items. The term "and / or" describes an association between related objects, indicating that three relationships can exist; for example, A and / or B can represent: A alone, A and B simultaneously, or B alone. Additionally, the character " / " in this application generally indicates that the preceding and following related objects are in an "or" relationship.
[0103] It should also be understood that the terms “if” and “if” can be interpreted as meaning “when” or “upon” or “in response to determination” or “in response to detection”. Similarly, depending on the context, the phrases “if determination…” or “if detection [the stated condition or event]” can be interpreted as meaning “when determination…” or “in response to determination…” or “when detection [the stated condition or event]” or “in response to detection [the stated condition or event]”.
[0104] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the protection scope of the technical solutions of the embodiments of this application.
[0105] All information, data, and signals involved in this application are authorized by the user or by all parties, and the collection, use, and processing of such data must comply with the relevant laws, regulations, and standards of the relevant countries and regions.
Claims
1. A method of running a Python application, characterized by, The method comprises: After a first process of a Python application is started, the first process creates a first thread for performing program replacement processing, the program replacement processing comprising: In response to a process triggering an import operation of a first program unit, obtaining metadata of the first program unit, wherein the first process is a process created by running a Python application, and the metadata comprises program identification; Determining whether the metadata set comprises metadata of the first program unit, wherein the metadata set comprises metadata of program units to be updated; In response to the metadata set comprising metadata of the first program unit, obtaining a second program unit corresponding to the first program unit, wherein the second program unit is an updated program unit of the first program unit, and is used to replace execution of the first program unit; Importing the second program unit, so that after the process triggers execution of the first program unit, the second program unit is executed.
2. The method of claim 1, wherein, The obtaining of the second program unit corresponding to the first program unit comprises: In a first correspondence relationship, obtaining a second program identification corresponding to a first program identification of the first program unit, wherein the first correspondence relationship corresponds to a program identification of a program unit to be updated and a program identification of an updated program unit, and the second program identification is a program identification of the second program unit; In a second correspondence relationship, obtaining a storage location corresponding to the second program identification, wherein the second correspondence relationship corresponds to a program identification of an updated program unit and a storage location of the updated program unit; According to the storage location of the second program unit, the second program unit is obtained.
3. The method of claim 2, wherein, The obtaining of the second program unit according to the storage location of the second program unit comprises: According to the storage location of the second program unit, a first source code file corresponding to the second program unit is obtained. Before the importing of the second program unit, the method further comprises: Modifying an index relationship between the first program unit and a second source code file to an index relationship between the first program unit and a first source code file, wherein the second source code file is a source code file corresponding to the first program unit.
4. The method of claim 2, wherein, The obtaining of the second program unit according to the storage location of the second program unit comprises: According to the storage location of the second program unit, a first bytecode file corresponding to the second program unit is obtained. Before the importing of the second program unit, the method further comprises: Modifying an index relationship between the first program unit and a second bytecode file to an index relationship between the first program unit and a first bytecode file, wherein the second bytecode file is a bytecode file corresponding to the first program unit.
5. The method according to any one of claims 1 to 4, characterized in that, The second program unit comprises a first sub-program unit and a second sub-program unit, and after the importing of the second program unit, the method further comprises: A third sub-program unit is generated, wherein the third sub-program unit is used to execute the function of the first sub-program unit, the first sub-program unit is used to implement the function extended from the first sub-program unit, and the second sub-program unit is used to call the third sub-program unit.
6. The method according to any one of claims 1 to 5, characterized in that, The program replacement process is implemented based on a third sub-program unit, and the method further includes: After a second process of the Python application is started, the second process imports the third sub-program unit in response to reading the first global variable; A second thread is created based on the third sub-program unit, wherein the second thread executes the program replacement process in the second process.
7. The method according to any one of claims 1 to 6, characterized in that, The first process is a main process of the Python application, and the first global variable is set by the first process; or A start command of the Python application is received, and the start command includes an instruction for setting the first global variable.
8. The method according to any one of claims 1 to 7, characterized in that, Before the first process of the Python application is started, the method further includes: Metadata of the first sub-program unit in the Python application and a second sub-program unit used for replacing the first sub-program unit are obtained; Metadata of the first sub-program unit is added to the metadata set.
9. The method according to any one of claims 1 to 8, characterized in that, The method further includes: The first thread is used to record execution information of the second sub-program unit, and the execution information includes a number of executions.
10. The method according to any one of claims 1 to 9, characterized in that, The metadata further includes a version number of the sub-program unit.
11. An apparatus for running a Python application, the apparatus comprising: The apparatus includes at least one module used to execute the method for running the Python application according to any one of claims 1 to 10.
12. A computing device, comprising: The computing device includes a memory and a processor, the memory stores at least one instruction, and the processor executes the at least one instruction to execute the method according to any one of claims 1 to 10.
13. A computer program product comprising instructions, characterized in that, When the computer program product is run on the computing device, the computing device is caused to execute the method according to any one of claims 1 to 10.