Python method, apparatus, and device for freezing application runtime hot update
By employing methods such as obtaining remote manifests, incremental downloading, and atomic merging, the problem of Python applications generated by PyInstaller failing to update at runtime is resolved, achieving millisecond-level seamless updates and system stability, making it suitable for industrial automation testing and data acquisition systems.
Patent Information
- Application Number
- CN202611360653.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-03
- Publication Date
- 2026-11-17
AI Technical Summary
In existing technologies, frozen Python applications generated by tools such as PyInstaller cannot achieve dynamic code updates at runtime, resulting in problems such as forced disconnection of hardware connections, low update efficiency, version inconsistency, and poor system availability in industrial sites or automated testing scenarios.
By obtaining a remote list of remote update servers and using hash value comparison to determine the target update file, incremental downloads and atomic merging are achieved, bypassing the frozen importer restriction, loading new module code, and maintaining continuous hardware connection status.
It enables seamless millisecond-level updates of Python applications without restarting the process, reducing bandwidth consumption, ensuring system stability and reliability, and avoiding version inconsistencies and hardware connection interruptions.
Smart Images

Figure CN123114512A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer hot update technology, and in particular to a method, apparatus and device for hot updating Python applications while freezing runtime. Background Technology
[0002] Python, as a widely used programming language, is extensively used in developing desktop applications in fields such as industrial control, automated testing, and instrumentation. To facilitate cross-platform distribution and deployment, developers typically use packaging tools like PyInstaller to compile and freeze the Python source code and its dependencies into a single executable file or directory package. When the application is frozen at runtime, its module loading path is redirected to within the packaged resources. The Python interpreter loads the module bytecode from the embedded resources via the built-in FrozenImporter, rather than directly importing it from the .py source files on the external file system.
[0003] In continuous software maintenance, hot update technology allows applications to dynamically replace code logic while running, with updates taking effect without restarting the process. For regular, non-frozen deployments of Python applications, developers can utilize module overloading mechanisms provided by standard libraries such as importlib to re-import modified module source code at runtime, thereby achieving hot code replacement. Similarly, in some interpreted scripting language applications (such as Lua and JavaScript), hot updates are typically accomplished by replacing external script files and triggering an interpreter reload.
[0004] However, for frozen applications generated by tools like PyInstaller, the conventional importlib.reload mechanism and file replacement methods are ineffective because its FrozenImporter has higher priority in the import chain than regular file system importers and does not support recognizing and loading new .py source files from external file system paths. Therefore, the current mainstream update method for frozen Python applications is still a full replacement: that is, stopping the application, downloading the complete executable file or installation package again, overwriting the original files, and then restarting the process. This update method has the following technical drawbacks in practical applications: First, the PyInstaller-frozen executable embeds Python source code modules as packaged resources, and the runtime's sys.modules and import paths both point to the packaged internals. When new .py source files appear on the external file system, the Python interpreter cannot recognize these external modules through the normal import mechanism. This results in the frozen application lacking the ability to dynamically update its code during runtime; any code changes must be implemented by stopping the process, replacing the executable, and restarting the application to take effect.
[0005] Secondly, in industrial settings or automated testing scenarios, Python applications typically need to maintain long-term, stable connections with hardware devices, such as serial communication, CAN bus communication, and camera video stream acquisition. Full update solutions require restarting the application, forcibly disconnecting these hardware connections. Re-initializing and re-establishing these connections often takes tens of seconds or more, causing prolonged business interruptions and potentially resulting in the loss of critical process data and device status.
[0006] Third, the existing full replacement method is prone to application files being in a mixed state of old and new versions if an abnormal interruption occurs during the update process (such as power outage, process crash, network transmission failure, or insufficient disk space). Applications that are interrupted often cannot run normally and are difficult to automatically restore to a usable state before or after the update, thus compromising the continuous availability of the system.
[0007] Fourth, the size of the frozen executable file or installation package is typically hundreds of MB. Even if the actual code changes only involve a few .py source files, existing solutions must transfer the entire executable file, resulting in significant unnecessary bandwidth consumption. In remote deployments or industrial sites with limited network conditions, this full-volume transfer method is inefficient and costly to maintain.
[0008] Fifth, when a module is imported for the first time, the Python interpreter compiles the source code into bytecode and caches it as a .pyc file in the __pycache__ directory. If the external source code is updated and the old .pyc bytecode file is not promptly removed, the interpreter may prioritize loading the outdated bytecode instead of the latest source code during subsequent imports, causing the code update to fail or resulting in unpredictable runtime behavior.
[0009] Sixth, if the hot update process is abnormally interrupted during the file merging phase, some new files already written to the hot update directory may become expired or orphaned semi-finished caches. Current technology lacks a mechanism to automatically detect and clean up these abnormal caches during application startup, which may result in the application loading incomplete or inconsistent code upon the next startup, severely impacting the integrity and stability of system startup. Summary of the Invention
[0010] The problem solved by this invention is to provide a method, apparatus, and device for freezing application runtime hot updates in Python.
[0011] In a first aspect, embodiments of the present invention provide a Python method for freezing application runtime hot updates, applied to a hot update system, the hot update system comprising a remote update server and a client hot update engine interconnected, the method comprising: Obtain the remote list of the remote update server, which includes the identifier and hash value of each source file; The remote list is compared with the local cache to determine the target update file; Download the target update file to a temporary directory; Acquire an update mutex lock, merge the target update file from the temporary directory into the local hot update directory, and create a backup of the overwritten file; Remove the target module and its associated sub-modules from the system module cache, create a module specification based on the local hot update directory and load the new module code to bypass the frozen importer restriction of the frozen application; The business facade instance is reconstructed based on the new module code, and the hardware connection object reference of the old business facade instance is passed to the new business facade instance to maintain the continuity of the hardware connection state. Save the remote list to update the local cache, release the update mutex lock, and notify that the update is complete.
[0012] Secondly, embodiments of the present invention also provide a Python application runtime hot update apparatus for executing the hot update method described in the first aspect, the apparatus comprising: The manifest acquisition unit is used to acquire the remote manifest of the remote update server, wherein the remote manifest contains the identifier and hash value of each source code file; The difference detection unit is used to compare the remote list with the local cache to determine the target update file; An incremental download unit is used to download the target update file to a temporary directory; An atomic merging unit is used to acquire an update mutex lock, merge the target update file from the temporary directory to the local hot update directory, and create a backup for the overwritten file; The module reloading unit is used to remove the target module and its associated sub-modules from the system module cache, create a module specification based on the local hot update directory, and load the new module code to bypass the frozen importer restriction of the frozen application. The state inheritance unit is used to reconstruct the business facade instance according to the new module code and pass the hardware connection object reference of the old business facade instance to the new business facade instance to maintain the continuity of the hardware connection state. An update confirmation unit is used to save the remote list to update the local cache, release the update mutex lock, and notify that the update is complete.
[0013] Thirdly, embodiments of the present invention also provide a computer device, the device including a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus; Memory, used to store computer programs; When a processor executes a program stored in memory, it implements the steps of the Python application runtime hot update method described in the first aspect.
[0014] Fourthly, embodiments of the present invention also provide a computer-readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the steps of the Python freeze application runtime hot update method as described in the first aspect.
[0015] This invention provides a method, apparatus, and device for freezing application runtime hot updates in Python. The method includes: obtaining a remote list from a remote update server, the remote list containing the identifiers and hash values of each source code file; comparing the remote list with a local cache to determine the target update file; downloading the target update file to a temporary directory to achieve incremental updates based on accurate hash value identification, requiring only the download of a small number of actually changed source code files each time, avoiding the unnecessary transmission of hundreds of megabytes of complete executable files in traditional full replacement methods; acquiring an update mutex lock, merging the target update file from the temporary directory to the local hot update directory, and creating a backup for the overwritten file, avoiding the mixed state of old and new version files and ensuring that the application is always runnable under any abnormal circumstances; removing the target module and its associated sub-modules from the system module cache, creating a module specification based on the local hot update directory, and loading the new module code to bypass the application freezing process. Ingress restrictions allow frozen applications generated by tools like PyInstaller to complete code replacement at runtime without restarting the process, reducing the average time of the entire process from triggering an update to the new code taking effect. This ensures that module objects in the system module cache are completely replaced by the new version, preventing the old version's bytecode from obscuring the new source code. The business facade instance is restructured based on the new module code, and the hardware connection object references of the old business facade instance are passed to the new business facade instance to maintain continuous hardware connection status. This avoids the re-initialization of hardware connections such as serial communication, bus communication, and video capture, achieving millisecond-level seamless switching from the old version's business logic to the new version's business logic. Saving a remote inventory to update the local cache, releasing the update mutex lock, and notifying when the update is complete strictly synchronizes the file system state with the version record, effectively avoiding the risk of version inconsistency caused by abnormal interruptions. This improves the reliability and maintainability of frozen applications in harsh environments such as industrial sites. Attached Figure Description
[0016] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the following description of the embodiments will be briefly introduced. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0017] Figure 1 A flowchart of a Python method for freezing application runtime hot update provided in an embodiment of the present invention; Figure 2 A schematic diagram of a hot update system provided in an embodiment of the present invention; Figure 3 A sub-flowchart of the Python application runtime hot update method provided in an embodiment of the present invention; Figure 4 A sub-flowchart of the Python application runtime hot update method provided in an embodiment of the present invention; Figure 5 A sub-flowchart of the Python application runtime hot update method provided in an embodiment of the present invention; Figure 6 A sub-flowchart of the Python application runtime hot update method provided in an embodiment of the present invention; Figure 7 A sub-flowchart of the Python application runtime hot update method provided in an embodiment of the present invention; Figure 8 A flowchart of a Python method for freezing application runtime hot update provided in an embodiment of the present invention; Figure 9 A schematic block diagram of a Python application runtime hot update device provided in an embodiment of the present invention; Figure 10 A schematic block diagram of a computer device provided for an embodiment of the present invention. Detailed Implementation
[0018] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0019] It should be understood that, when used in this specification and the appended claims, the terms "comprising" and "including" indicate the presence of the described features, integrals, steps, operations, elements and / or components, but do not exclude the presence or addition of one or more other features, integrals, steps, operations, elements, components and / or collections thereof.
[0020] It should also be understood that the terminology used in this specification is for the purpose of describing particular embodiments only and is not intended to limit the invention. As used in this specification and the appended claims, the singular forms “a,” “an,” and “the” are intended to include the plural forms unless the context clearly indicates otherwise.
[0021] It should also be further understood that the term "and / or" as used in this specification and the appended claims refers to any combination of one or more of the associated listed items and all possible combinations, and includes such combinations.
[0022] It should also be noted that, unless otherwise explicitly specified and limited, terms such as "installation," "connection," "linking," "fixing," and "setting" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral part; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; they can refer to the internal communication of two components or the interaction between two components. When an component is referred to as being "on" or "below" another component, the component can be located "directly" or "indirectly" on the other component, or there may be one or more intermediary components. The terms "first," "second," "third," etc., are only for the convenience of describing this technical solution and should not be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. Therefore, features defined with "first," "second," "third," etc., may explicitly or implicitly include one or more of that feature. For those skilled in the art, the specific meaning of the above terms in this invention can be understood according to the specific circumstances.
[0023] Please see Figure 1 and Figure 2 This invention provides a method for freezing hot updates during Python application runtime. This invention is applicable to runtime hot update scenarios for all Python desktop applications distributed using PyInstaller (or similar Python freeze packaging tools, such as cx_Freeze, Nuitka), and is especially suitable for the following scenarios that require continuous 24 / 7 operation: Industrial automation testing framework: testing systems that require long-term continuous operation and cannot be frequently restarted or shut down, such as automotive display testing, production line automation testing, and reliability and durability testing.
[0024] Data acquisition and monitoring systems: Applications that require maintaining hardware connectivity and data continuity, such as laboratory data acquisition, environmental monitoring, and equipment status monitoring.
[0025] The Python method of freezing application runtime hot update is applied to a hot update system, which includes a remote update server and a client hot update engine that are interconnected.
[0026] In the industrial automation testing framework scenario, the client is an industrial control computer (ICC) or test station host deployed at the test site, running a Python test application packaged and frozen using tools such as PyInstaller. The client establishes connections with lower-level controllers or test fixtures via a serial communication interface, with the vehicle-mounted electronic control unit or production line equipment under test via a controller area network (LAN) bus interface, and with industrial cameras or webcams via a video acquisition interface to execute vehicle display image acquisition, production line automation process control, or reliability and durability test sequences. The remote update server is a version management server deployed in the enterprise's computer room or test center. It establishes a network connection with the client via a LAN or WAN, managing the version release of the Python source code files used for testing and responding to client requests for manifest retrieval and file downloads.
[0027] In data acquisition and monitoring system scenarios, the client is an edge computing gateway or data acquisition workstation deployed in laboratories, monitoring sites, or equipment rooms. Internally, it runs Python acquisition and monitoring applications packaged and frozen using tools such as PyInstaller. The client establishes connections with laboratory instruments or environmental monitoring sensors via a serial communication interface, with industrial field equipment via a controller area network (CAN) bus interface, and with surveillance cameras via a video acquisition interface to perform real-time acquisition of experimental data, monitoring of environmental parameters, or visual monitoring of equipment status. The remote update server is an application management server deployed in the monitoring center or cloud. It establishes connections with clients distributed across various sites via wired or wireless networks and is responsible for uniformly distributing updated acquisition algorithms, protocol parsing logic, or monitoring interface code.
[0028] In both scenarios, communication between the client and the remote update server is based on the Hypertext Transfer Protocol. The client's hot update engine is embedded within the frozen application and independently completes interaction with the remote update server, version comparison, file download, and module reloading while the application continues to run, without interrupting the test tasks or data collection tasks being executed by the client.
[0029] Specifically, the Python method for freezing application runtime hot update includes steps S1 to S7.
[0030] S1. Obtain the remote list of the remote update server, which includes the identifier and hash value of each source file.
[0031] In this embodiment, the remote list of the remote update server is obtained. The remote list is a collection of metadata maintained by the remote update server to describe the current version status of each source code file it manages. The file identifier is used to uniquely identify a source code file, which can be the relative path of the file in the source code directory tree (such as "module / communication.py") or the filename; the hash value is a verification value calculated by a cryptographic hash algorithm on the complete content of the file, used to accurately determine whether the file content has changed.
[0032] When initiating the update process, the client-side hot update engine first sends a manifest retrieval request to the remote update server via Hypertext Transfer Protocol (HTTP). In one embodiment, the client-side hot update engine sends a GET request for the manifest interface to the network port being listened to by the remote update server. Upon receiving the request, the remote update server returns the currently constructed remote manifest to the client-side hot update engine. After receiving the remote manifest, the client-side hot update engine temporarily stores it in its local memory as baseline data for subsequent difference detection.
[0033] Instead of directly downloading all source code files, the client first obtains a remote list containing only file identifiers and hash values. This allows the client to grasp the version status of all source code files on the server in a very short time (less than 1 second), providing a basis for subsequently downloading only the files that have actually changed. Compared to the traditional full update method that requires downloading hundreds of megabytes of complete executable files, this step significantly reduces network transmission overhead before the update.
[0034] Referring also to 3, in some embodiments, obtaining the remote list of the remote update server includes: S11. The remote update server scans the source code directory, reads the contents of each source code file, and calculates the corresponding hash value.
[0035] In this embodiment, the remote update server scans the source code directory, reads the contents of each source code file, and calculates the corresponding hash value. The source code directory is a pre-configured directory on the remote update server used to store source code files to be distributed. The remote update server recursively traverses the source code directory and all its subdirectories through the file system interface to identify all source code files that meet the preset filtering conditions, such as Python source code files with the .py extension.
[0036] For each identified source code file, the remote update server obtains its complete content through file read operations. It reads the file as a byte stream to ensure consistency with the actual stored content and then uses a preset hash algorithm to calculate the hash value corresponding to that file. In one embodiment, the hash algorithm uses a 256-bit version of the Secure Hash Algorithm, which maps input data of arbitrary length to a fixed-length 256-bit (32-byte) hash digest. Even if the file content undergoes only a minor change (such as a modification of a single character), the corresponding hash value will present a completely different result, thus enabling precise detection of any changes to the file content.
[0037] S12. Construct the remote list based on the file identifier and corresponding hash value of each source file.
[0038] In this embodiment, a remote list is constructed based on the file identifier and corresponding hash value of each source code file. The remote update server establishes a one-to-one mapping relationship between the file identifier of each source code file obtained in the aforementioned steps and its calculated hash value, and serializes and organizes it according to a preset data format to generate a structured remote list.
[0039] In one embodiment, the remote manifest is encoded in JSON (JavaScript Object Notation) format, where each key-value pair has a file identifier (such as a relative path string) as the key and a hash value string corresponding to that file as the value. Once the remote manifest is built, it is stored in the remote update server's memory cache or a temporary file, ready to be returned immediately upon client request. Simultaneously, the remote update server maintains the original content of each source file so that when a client subsequently requests to download a single file, the corresponding source file can be distributed on demand via an HTTP interface.
[0040] In this way, the remote update server can dynamically and in real time reflect the latest status of each file in the source code directory, ensuring that the remote list obtained by the client is always consistent with the actual source code on the server, providing a reliable data foundation for the client to accurately identify the files that need to be updated.
[0041] S2. Compare the remote list with the local cache to determine the target update file.
[0042] In this embodiment, the remote manifest is compared with the local cache to determine the target update file. The local manifest cache is a version record file (such as a manifest cache file) persistently stored locally by the client's hot update engine. It records the file identifiers and corresponding hash values of each source code file in the local hot update directory after the last successful update. When the application starts for the first time or performs the first update, the local manifest cache may not have been generated or may be empty. At this time, all source code files recorded in the remote manifest are considered as new files that need to be downloaded.
[0043] The client-side hot update engine uses file identifiers as index keys to compare the remote manifest obtained by S1 with the local manifest cache item by item. Specifically, for each file identifier recorded in the remote manifest, it checks if the same file identifier exists in the local manifest cache; if it does, it further compares whether the hash values corresponding to that file identifier in the remote manifest and the local cache are completely consistent. Through precise comparison of hash values, any changes to the file content can be accurately detected, and even if the file modification time changes but the content remains the same, it will not be mistakenly judged as needing an update.
[0044] After the comparison is complete, the client's hot update engine generates a list of difference files. This list includes at least two types of target update files: newly added files and changed files. Newly added files are those that exist in the remote manifest but not in the local manifest cache; changed files are those that exist in both the remote and local manifest caches, but whose corresponding hash values are inconsistent. Through this difference detection mechanism, the client can accurately locate the source code files whose content has actually changed without downloading unchanged files. This reduces the network transmission volume required for updates from the traditional full executable file (hundreds of megabytes) to a small number of source code files containing only actual changes (only one to five files, with a total size of less than fifty kilobytes), significantly saving bandwidth and shortening update preparation time.
[0045] Referring also to 4, in some embodiments, comparing the remote list with the local cache to determine the target update file includes: S21. Identify files that exist in the remote list but not in the local cache, and add them as new files.
[0046] In this embodiment, files that exist in the remote manifest but not in the local cache are identified as new files. The client hot update engine traverses all file identifiers contained in the remote manifest, and for each file identifier, searches in the key set of the local manifest cache. If the search result does not exist, the source code file corresponding to that file identifier is determined to be a new file.
[0047] For example, if the remote manifest contains the file identifier "module / can_bus.py" but this record is not found in the local manifest cache, then "module / can_bus.py" will be marked as a new file. When deploying an update feature for the first time or when an application requests an update for the first time, because the local manifest cache has not yet been established, all file identifiers in the remote manifest will be recognized as new files. In this case, the update will download all remote source code files to establish the initial local hot update directory and local manifest cache.
[0048] The same identification logic applies to cases where the local manifest cache already exists but some records are missing. Missing records may stem from the failure to download some files during the last update, resulting in the update process being abnormally terminated. In this case, by re-identifying them as newly added files, the missing content can be filled in during the next update, ensuring that the local hot update directory ultimately remains consistent with the remote manifest.
[0049] S22. Identify files in the remote list that have the same file identifier but different hash values as the files in the local cache, and designate them as change files.
[0050] In this embodiment, files with the same file identifier in the remote list and the local cache but different hash values are identified as changed files. For file identifiers that are not identified as new files in S21 (i.e., files that exist in both the remote list and the local cache), the client hot update engine further compares the hash values corresponding to the file identifier in the two lists respectively.
[0051] Hash value comparison uses an exact string matching method. If the hash value string corresponding to the file identifier in the remote manifest is exactly the same as the corresponding hash value string in the local manifest cache, it indicates that the content of the source file has not changed since the last update and does not need to be included in the scope of this update; if the two are not the same, it indicates that the content of the source file has been modified and it is marked as a changed file.
[0052] For example, if the hash value of the file identifier "main.py" in the remote manifest is "abc123...", while its hash value in the local manifest cache is "xyz789...", then "main.py" is determined to be a modified file. Using hash-based content comparison is more accurate than comparisons based on file modification time or file size. It avoids false positives caused by unexpected changes in file timestamps or padding bytes, ensuring that only files with truly changed content are downloaded and updated.
[0053] S23. The newly added file and the changed file are identified as the target update file.
[0054] In this embodiment, newly added files and modified files are identified as target update files. The client hot update engine merges all newly added files identified in S21 with all modified files identified in S22 to generate a unified list of target update files. Each item in the target update file list contains at least a file identifier and the hash value corresponding to that file in the remote manifest, serving as input for subsequent incremental download steps.
[0055] Furthermore, to maintain consistency between the local hot update directory and the remote source code directory, the client hot update engine can also identify files that exist in the local manifest cache but not in the remote manifest, as remotely deleted files. Remotely deleted files indicate that the file has been removed from the remote source code directory and is no longer a valid component of the current version. In subsequent steps, these remotely deleted files can be removed from the local hot update directory to prevent old version source code files from remaining in the new version and potentially being loaded incorrectly, thus eliminating the risk of version inconsistency. This cleanup operation can be performed synchronously after acquiring the update mutex lock and during atomic merging to ensure the atomicity of file addition and deletion operations.
[0056] Therefore, through the difference detection process from S21 to S23, the client-side hot update engine can quickly complete version difference analysis locally, accurately determining the update scope without downloading any source code files. This not only minimizes network transmission overhead but also provides a clear list of file operations for subsequent atomic update operations, making the entire update process predictable and traceable.
[0057] S3. Download the target update file to a temporary directory.
[0058] In this embodiment, the target update file is downloaded to a temporary directory. The temporary directory is a directory pre-created or dynamically created by the client's hot update engine in the local file system, such as a hot update temporary directory, used to temporarily store the new version of the source code file downloaded from the remote update server. The temporary directory is independent of the local hot update directory; they are physically separated to ensure that the download phase does not directly affect the currently running business code.
[0059] Based on the target update file list determined in S2, the client-side hot update engine initiates file-by-file download requests to the remote update server. In one embodiment, the client-side hot update engine sends a retrieval request to the file distribution interface (e.g., / api / file / {file identifier}) of the remote update server via the Hypertext Transfer Protocol, carrying the file identifier of the file to be downloaded in the request message; the remote update server locates the corresponding source code file based on the file identifier and returns the file content as a response body to the client-side hot update engine; after receiving the response, the client-side hot update engine writes the file content to a temporary directory, and the relative path structure of the file in the temporary directory is consistent with the path structure indicated by the file identifier.
[0060] During the download process, the client-side hot update engine does not acquire the S4 update mutex lock, allowing business threads to execute normally. Real-time services such as hardware communication and data acquisition are unaffected by the update download operation. This method enables the download phase (which may last for several seconds or even longer due to network conditions) to run in complete parallel with the business operation phase, fundamentally avoiding business interruptions caused by waiting for updates.
[0061] For each file in the target update file list, the client-side hot update engine performs a download operation independently. If the download of a file fails due to network timeout, abnormal server response, or local disk write failure, the client-side hot update engine records the failure status of that file, such as recording the file identifier and the reason for failure in the download failure log, and continues to execute the download of the next file in the list until all target update files have been traversed. The failure to download a single file will not interrupt the normal download process of other files, thus maximizing the acquisition of updatable file content even in unstable network scenarios.
[0062] For files that fail to download, the client-side hot update engine can retry the download of the failed file once or multiple times after all files have been downloaded. If the download still fails after retrying, the file is marked as not ready and skipped in the subsequent atomic merge step. At the same time, the hash value of the file is not recorded in the local manifest cache to ensure that the local manifest cache is consistent with the status of the files that were actually successfully downloaded.
[0063] Once all target update files (or other files except for those that failed) have been downloaded, the client-side hot update engine summarizes the download results, confirms the set of files ready to be merged in the temporary directory, and reports a download completion event to the update coordinator, triggering the subsequent S4 atomic merge operation. By separating the download and merge operations and avoiding holding mutex locks during the download phase, this step ensures business continuity while preparing complete file materials for subsequent atomic updates, enabling uninterrupted business updates.
[0064] S4. Acquire an update mutex lock, merge the target update file from the temporary directory into the local hot update directory, and create a backup for the overwritten file.
[0065] In this embodiment, an update mutex is acquired, the target update file is merged from the temporary directory into the local hot update directory, and a backup is created for the overwritten file. The update mutex is a thread-level mutex maintained by the client hot update engine, used to allow only one update process to enter the critical section at any given time, preventing multiple update requests from executing concurrently, which could lead to file state corruption or abnormal module loading.
[0066] Before attempting to acquire the update mutex, the client-side hot update engine first checks if any tasks are currently blocking the update. If a task is running and should not be interrupted, such as a hardware test sequence or data acquisition process, the update request is rejected, and a busy status message is returned to the front end. The update will be initiated again after the task is completed. If no tasks are blocking the update, the engine attempts to acquire the update mutex. If the acquisition is successful, the atomic update phase begins. If the acquisition fails, and another thread already holds the lock, the engine waits or returns a message to retry later.
[0067] After entering the atomic update phase, the client hot update engine traverses all target update files downloaded to the temporary directory in S3, performing a merge operation file by file. For each target update file, the client hot update engine first determines its target path in the local hot update directory; if a file with the same name (i.e., an older version file) already exists at the target path, it first copies the older version file, names it with a preset backup extension (e.g., .bak), and stores it in the same directory or a dedicated backup subdirectory as a backup file; then, it copies or moves the new version file from the temporary directory to the target path, overwriting the older version file. If a file with the same name does not exist at the target path (i.e., a newly added file), it directly copies or moves the new version file from the temporary directory to the target path without creating a backup.
[0068] The merge operation is performed file by file until all target update files have been merged. Throughout the merge process, because the update mutex lock is held, other threads cannot simultaneously trigger a new update process or modify files in the local hot update directory, thus ensuring the atomicity of the file system-level operation. That is, for all files involved in this update, the state transition is logically presented as an indivisible whole operation.
[0069] By acquiring a mutex lock and performing file merging under lock protection, this step compresses the impact window of the update operation on business operations to the shortest possible time (only a few seconds). Within this time window, a backup mechanism is used to reserve a recovery path for possible failures, effectively solving the problems of mutual interference between file replacement and business operations and the difficulty in recovery after abnormal interruption in traditional update methods.
[0070] Referring also to 5, in some embodiments, after acquiring the updated mutex lock, the method further includes: S41. If an error occurs during the merging process, the overwritten original file is restored based on the backup file, and the merged new file is deleted.
[0071] In this embodiment, if an anomaly occurs during the merging process, the overwritten original files are restored based on the backup files, and the merged new files are deleted. Anomalies include, but are not limited to, write failures due to insufficient disk space, copy failures due to insufficient file system permissions, and read / write errors due to physical failures of the storage medium.
[0072] When the client's hot update engine detects an anomaly while merging any file, it immediately triggers a rollback process. The rollback process is executed by the rollback manager, which performs the following steps: It iterates through all files merged into the local hot update directory in this update. For files with corresponding backup files (i.e., files that overwrote older versions), the backup files are copied back to the original target path, restoring the overwritten original files. For files without corresponding backup files (i.e., newly added files), the new files are directly deleted from the local hot update directory. Simultaneously, temporary files in the temporary directory that have not yet been merged or remain from the merging process are cleaned up.
[0073] Through the rollback process described above, even if an unexpected exception occurs during the merging phase, the file collection in the local hot update directory can still be restored to its complete state before the update, avoiding a situation where old and new versions of files are mixed together, and ensuring that the application still has a stable code foundation under abnormal scenarios.
[0074] S42. Release the update mutex and terminate the current update process.
[0075] In this embodiment, the update mutex is released and the current update process is terminated. After file recovery and cleanup are completed, the client hot update engine releases the update mutex it has acquired, allowing other waiting update requests or business threads to enter the critical section normally. Simultaneously, the client hot update engine sends an update failure notification to the front end. The notification may include a summary of the exception reason, such as insufficient disk space or file write failure, so that maintenance personnel or users are aware that the update has not been successfully completed and can troubleshoot environmental issues and re-initiate the update based on the prompts.
[0076] In summary, through the exception handling branches of S41 and S42, this embodiment ensures that the update process will not leave dirty data or deadlock state under any abnormal exit path by detecting exceptions, restoring backups, releasing locks, and notifying status during the file merging stage, thereby improving the update reliability of the frozen application in harsh environments such as industrial sites.
[0077] S5. Remove the target module and its associated sub-modules from the system module cache, create a module specification based on the local hot update directory and load the new module code to bypass the frozen importer restriction of the frozen application.
[0078] In this embodiment, the target module and its associated submodules are removed from the system module cache, a module specification is created based on the local hot update directory, and the new module code is loaded to bypass the frozen importer restriction of the frozen application. In the Python runtime environment, the system module cache is a global dictionary maintained by the interpreter to store module objects of imported modules. When an import statement is executed in the code, the interpreter first checks if a module with the same name already exists in the system module cache; if it does, it directly returns the cached module object without rereading the source code file. Furthermore, for applications that use tools such as PyInstaller for frozen packaging, the Python interpreter registers a frozen importer in the import chain; this frozen importer takes precedence over regular file system importers, loads modules only from the embedded resources, and cannot recognize new source code files on external file system paths.
[0079] Based on the above mechanism, if a regular reload operation is performed directly on a module in the system module cache, the interpreter will still read the old version bytecode from the embedded resources due to the path restriction of the frozen importer, and will be unable to load the new source code in the local hot update directory. Therefore, the client hot update engine first traverses the system module cache, locates the target module corresponding to the target update file, and removes the target module and its associated submodules (submodules directly or indirectly imported by the target module and still residing in the system module cache). By thoroughly clearing the old module cache, it ensures that subsequent import operations will not return expired module objects.
[0080] Subsequently, the client-side hot-update engine creates a module specification based on the new source code files in the local hot-update directory. The module specification is a data structure in the Python import system that describes how modules are loaded, containing key information such as the module name, module location, and module loader. By explicitly specifying the module location as the local hot-update directory and configuring the submodule search path, the interpreter prioritizes searching in the local hot-update directory on the external file system when loading the module and its submodules, rather than falling back to the frozen importer. Finally, by executing the loader entry function in the module specification, the new module code is executed and loaded into the system module cache, completing the runtime code replacement.
[0081] This step enables the application freezer to load and execute new source code from an external file system for the first time without restarting the process, thus overcoming the limitations of the freeze importer on runtime code updates and solving the problem of hot updates for Python frozen binary applications.
[0082] Referring also to 6, in some embodiments, the step of creating a module specification based on the local hot update directory and loading new module code includes: S51. Use the file location specification creation function to generate a module specification pointing to the local hot update directory, and configure the submodule search path to the local hot update directory.
[0083] In this embodiment, a module specification pointing to the local hot-update directory is generated using a file location specification creation function, and the submodule search path is configured to the local hot-update directory. The file location specification creation function is a specification creation interface provided in the Python standard library import utility module. It takes the module name and module file path as input parameters and generates a module specification object describing how to load the module from the specified file.
[0084] For each target module to be overloaded, the client-side hot update engine calls the file location specification creation function, passing in the module name (e.g., "communication.serial_handler") and the absolute path of the corresponding source file in the local hot update directory (e.g., " / app / hot_update / communication / serial_handler.py"). During the call, the client-side hot update engine explicitly configures the submodule search path parameter, setting it to a list containing paths to the local hot update directory. The submodule search path tells the Python interpreter that when relative imports or submodule imports are performed within the module, it should first look for dependent modules in the corresponding subdirectories under the local hot update directory.
[0085] If the submodule search path is not explicitly configured, the interpreter may still attempt to search for imported submodules from the bundled resources by freezing the importer after loading the main module. This could result in the submodules loading older versions, leading to an inconsistent state where old and new code run together. Therefore, explicit configuration ensures that the entire module tree is loaded from the local hot-update directory, maintaining version consistency.
[0086] S52. Execute the module loader entry function in the module specification to load the new module code into the system module cache.
[0087] In this embodiment, the module loader entry function in the module specification is executed to load the new module code into the system module cache. The module loader entry function is an execution interface built into the module specification object. It receives an empty module object as a container, reads the contents of the source code file specified in the module specification, and executes the source code in the namespace of the empty module object, thereby binding all classes, functions, variables, etc. defined in the file to the module object.
[0088] The client-side hot update engine extracts the module loader from the module specification, creates a new empty module object, and calls the module loader's entry function to execute the new source code file from the local hot update directory within this empty module object. After execution, the module object, now populated with new code, is re-injected into the system module cache, ensuring it can be correctly referenced by business code in subsequent import operations. For multiple target modules with import dependencies, the client-side hot update engine executes S51 and S52 sequentially according to the dependency topology (child modules first, then parent modules, or the order determined by the import relationship graph), ensuring that the reference relationships between modules are correctly resolved after reloading.
[0089] With S51 and S52, the application can complete precise hot replacement at the module level at runtime. The average time from triggering the update to the new code taking effect can be controlled within 15 seconds, without interrupting the hardware connection or restarting the application process, thus achieving true runtime hot update.
[0090] S6. Reconstruct the business facade instance based on the new module code, and pass the hardware connection object reference of the old business facade instance to the new business facade instance to maintain the continuity of the hardware connection state.
[0091] In this embodiment, the business facade instance is reconstructed based on the new module code, and the hardware connection object reference of the old business facade instance is passed to the new business facade instance to maintain the continuity of the hardware connection state. The business facade instance is a unified business interface object exposed by the client hot update engine, which encapsulates specific business logic such as hardware control, data processing, and protocol conversion. In the software architecture, the business facade pattern provides a high-level unified interface, shielding the complex interactions of multiple underlying subsystems, enabling upper-layer callers to drive hardware to complete business operations in a concise manner.
[0092] After S5 module reloading, the system module cache already contains the new version of the module object. However, the old business facade instance still exists in memory, created based on the old version of the module code. Its internal class definitions, method objects, and static variables still point to the old version of the module. If the old business facade instance continues to be used, even if the underlying module has been updated, the behavior logic at the instance level will remain in the old state, causing the update to fail. Therefore, the client hot update engine re-instantiates a new business facade instance based on the new module code loaded by S5, i.e., the new business facade instance.
[0093] However, in industrial control and automation testing scenarios, business facade instances typically hold long-lived connection objects established with physical hardware, such as serial communication objects, bus communication objects, and video acquisition objects. The creation of these hardware connection objects involves complex initialization processes such as port scanning, driver loading, handshake protocols, and configuration distribution. Completely rebuilding a connection often takes tens of seconds or even longer, and may interrupt ongoing communication sessions, data acquisition tasks, or video recordings, resulting in business data loss or abnormal on-site conditions.
[0094] To avoid the aforementioned issues, the client-side hot update engine does not recreate the hardware connection object when reconstructing a new business facade instance. Instead, it extracts a direct reference to the established hardware connection object from the old business facade instance and assigns this reference to the corresponding attribute of the new business facade instance. A direct reference is a pointer or handle to the same hardware connection object instance in memory, not a copy or deep copy of the object. Through reference passing, the new business facade instance and the old business facade instance share the same set of underlying hardware connection resources, including open operating system file descriptors, underlying driver handles, device configuration parameters, and running status flags.
[0095] To prevent the hardware connection from being accidentally closed due to the resource release logic triggered when the old instance ends its lifecycle, the client hot update engine explicitly sets the hardware connection object property in the old business facade instance to a null reference after the reference passing is completed, or adopts a weak reference mechanism to ensure that the destruction of the old instance will not cascade and close the passed hardware connection resources.
[0096] This step allows the application to maintain a continuous connection with external physical hardware while completing hot code updates, enabling millisecond-level seamless switching from the old version of business logic to the new version. This eliminates the hardware re-initialization time and business interruption issues caused by restarting the application in traditional update methods.
[0097] Referring also to 7, in some embodiments, passing the hardware connection object reference of the old business facade instance to the new business facade instance includes: S61. Pass the serial port connection object reference of the old business facade instance to the new business facade instance.
[0098] In this embodiment, the serial port connection object reference of the old business facade instance is passed to the new business facade instance. The serial port connection object is a hardware and software abstraction object used to implement the serial communication protocol. It encapsulates the open communication port identifier, such as the COM port number, baud rate configuration, data bits, stop bits, parity bits, and other communication parameters, as well as the operating system-level serial port file descriptor or handle.
[0099] The client-side hot update engine obtains a direct reference to the serial port connection object from the serial port attributes, such as the `serial_port` attribute, of the old business facade instance and assigns it to the attribute with the same name in the new business facade instance. Through this reference passing, the new business facade instance can directly send control commands to the physical serial port or receive sensor data through this serial port connection object without having to re-perform initialization operations such as opening the port and setting the baud rate, ensuring that serial communication is uninterrupted before and after the update.
[0100] S62. Pass the bus connection object reference of the old business facade instance to the new business facade instance.
[0101] In this embodiment, the bus connection object reference of the old business facade instance is passed to the new business facade instance. The bus connection object (i.e., the CAN bus object) is an abstract object used to implement the controller area network communication protocol. It encapsulates the loaded database file parsing rules, channel configuration information (such as channel number and baud rate), message filtering rules, and the underlying bus adapter handle.
[0102] The client-side hot update engine obtains a direct reference to the bus connection object from the bus attributes (such as the `can_bus` attribute) of the old business facade instance and assigns it to the attribute with the same name in the new business facade instance. Through this reference passing, the new business facade instance can continue to listen for or send messages that conform to the database file definition, maintaining real-time communication with CAN nodes such as vehicle-mounted devices and industrial sensors, and avoiding message loss or node offline due to bus channel reconstruction.
[0103] S63. Pass the video capture object reference of the old business facade instance to the new business facade instance.
[0104] In this embodiment, the video capture object reference of the old business facade instance is passed to the new business facade instance. The video capture object is an abstract object used to control the image capture device, which can be an industrial camera, a USB camera, or a network camera. The video capture object encapsulates the current recording status of the device (e.g., whether it is recording), video encoding parameters, frame rate configuration, path handle of the output video file, and pointer to the video stream buffer.
[0105] The client-side hot update engine obtains a direct reference to the video capture object from the video attributes (such as the camera attribute) of the old business facade instance and assigns it to the attribute with the same name in the new business facade instance. Through this reference passing, if the video capture was in a continuous recording state before the update, the new business facade instance can directly control the same video capture object to continue writing to the same video file after the update, ensuring the continuity of video recording and avoiding file segmentation or recording interruption.
[0106] Through the hardware connection object reference passing in this embodiment, the client hot update engine can achieve seamless inheritance of three key hardware resources—serial communication, bus communication, and video acquisition—while the code logic is fully updated. This allows the frozen application to immediately drive the old hardware connection to continue running with the new logic after the hot update is completed, thereby improving the availability and maintenance efficiency of industrial field applications.
[0107] S7. Save the remote list to update the local cache, release the update mutex lock and notify that the update is complete.
[0108] In this embodiment, the remote inventory is saved to update the local cache, the update mutex is released, and the update is notified as complete. After the business facade instance reconstruction and hardware state inheritance are completed in S6, the client hot update engine enters the update finalization stage, performing a series of state persistence and resource release operations to declare the formal end of this hot update process and ensure the cleanliness of the subsequent operating environment.
[0109] First, the client-side hot update engine performs bytecode cache cleanup. When importing modules, the Python interpreter automatically compiles the source code into bytecode and caches it as bytecode files with the .pyc extension, stored in a bytecode cache directory named __pycache__. These cached files are intended to speed up subsequent imports. However, if the old bytecode cache is not cleared after the source code files have been hot-swapped via S5, the interpreter may directly load outdated bytecode during the next import due to cache hit mechanisms. This results in the new source code's modifications not taking effect, leading to bytecode pollution—updated source code but unchanged runtime behavior. Therefore, the client-side hot update engine recursively traverses the local hot update directory and all its subdirectories, searching and deleting all .pyc bytecode files in the __pycache__ directory and within it. This cleanup operation ensures that the Python interpreter can only recompile bytecode from the latest .py source files during subsequent imports, fundamentally eliminating the shadowing effect of old bytecode on new source code.
[0110] Subsequently, the client-side hot update engine writes the remote manifest obtained in S1 to local persistent storage to update the local manifest cache. In one embodiment, the client-side hot update engine serializes the remote manifest into an object spectrum format (JSON) and writes it to a preset manifest cache file. The file identifiers and their hash values recorded in the local manifest cache are consistent with the actual file status in the current local hot update directory, thus providing accurate local baseline data for difference detection in S2 when the next update process starts. By updating the local manifest cache in a timely manner, duplicate downloads or version misjudgments caused by cache lag are avoided.
[0111] Next, the client-side hot update engine releases the update mutex lock it acquired at the start of S4. After the lock is released, other waiting update requests can re-compete for the lock and initiate a new update process; business threads can also safely access module files in the local hot update directory when needed. The release of the update mutex lock marks the formal end of this atomic update phase, and the system returns to normal operation from the update critical section.
[0112] Finally, the client-side hot update engine sends an update completion notification to the front-end. The front-end can be the application's built-in graphical user interface, a web management interface, or an external monitoring terminal that has established a communication connection with the client-side hot update engine. The update completion notification may include summary information about the update, such as the number of updated files, update time, and version identifier, so that operations personnel or users can confirm that the update has been successfully implemented. If the update process involves the passing of references to hardware connection objects, the notification may also include confirmation that the hardware status remains normal, indicating to users that their business has not been interrupted due to the update.
[0113] Through finalization operations, this hot update process achieved a consistent and clean state at the file system, module cache, and persistent record levels: only the latest version of the source code was retained in the local hot update directory, with no expired bytecode interference; the local manifest cache accurately reflected the current version status; the update mutex returned to a usable state, and the system was capable of receiving the next update. The entire update process, from triggering to notification completion, took an average of less than fifteen seconds, and required no application process restart or hardware connection interruption, achieving runtime hot updates while freezing the application.
[0114] Referring also to 8, in some embodiments, prior to obtaining the remote list of the remote update server, the method further includes: S01. When the application starts, check if the local hot update directory exists.
[0115] In this embodiment, when the application starts, it checks whether the local hot update directory exists. Before any business module is loaded, the client hot update engine, during the application startup initialization phase, checks the local file system for the existence of the local hot update directory. The local hot update directory is a dedicated directory for storing the new version of the source code files downloaded and merged through the hot update process; its path is typically configured as a fixed subdirectory under the application's working directory.
[0116] The detection operation is completed through the directory existence query interface provided by the operating system. If the local hot update directory does not exist, it indicates that the application has not performed a hot update before, or that the directory was not generated after the last hot update. In this case, the subsequent cleanup logic is skipped, and the normal business initialization process is directly entered. If the local hot update directory exists, it indicates that a hot update process may have been performed before. The validity of the files in the directory needs to be further verified to exclude expired caches left over from the last abnormal interruption of the update.
[0117] S02. If it exists, scan all source code files in the local hot update directory and verify whether each source code file has a corresponding module in the original frozen package to determine whether there are any isolated files.
[0118] In this embodiment, if an isolated file exists, all source code files in the local hot update directory are scanned, and each source code file is checked to see if a corresponding module exists in the original frozen package to determine if an isolated file exists. The client hot update engine recursively traverses the local hot update directory and all its subdirectories, collecting all source code files with the .py extension to form a list of files to be verified.
[0119] For each source file in the list of files to be verified, the client hot update engine performs mapping path verification. Mapping path verification means: based on the relative path of the source file in the local hot update directory, deriving its corresponding module import path (e.g., mapping the local path "hot_update / communication / serial.py" to the module name "communication.serial"), and then querying whether the original frozen package (i.e., the embedded resource package generated by packaging tools such as PyInstaller) contains the original module corresponding to the module import path. In one embodiment, the query is completed by checking the module index table maintained by the frozen importer: the frozen importer has registered the import paths of all embedded modules in the system import mechanism when the application starts. If the module import path exists in the index table, it is determined that the corresponding module exists in the original frozen package; if it does not exist, the source file is determined to be an orphan file.
[0120] For applications packaged using PyInstaller, module information in the original frozen package can also be obtained through the `sys.frozen` property and the archive information interface provided by the PyInstaller runtime module. If a module record matching the source file mapping path can be found through either of these interfaces, the corresponding module is considered to exist.
[0121] S03. If there are orphaned files, clear the local hot update directory to the automatic clearing operation of expired cache.
[0122] In this embodiment, if orphaned files exist, the local hot update directory is cleared. Orphaned files refer to source code files that exist in the local hot update directory but whose corresponding modules are no longer present in the original frozen package. The causes of orphaned files are usually as follows: during the last hot update, a file was recorded in the remote manifest, downloaded, and merged into the local hot update directory, but the update process was subsequently interrupted due to an anomaly (such as a power outage or process crash), failing to complete the merging of all files or module reloading; or, a module was subsequently deleted from the remote source code directory, but this module file had already been merged into the local directory during the last update, and no new update process was triggered to clean it up before this startup.
[0123] When any isolated file is detected, the client-side hot update engine determines that the local hot update directory is in an untrusted, incomplete state, and the consistency and integrity of the versions of the files within it cannot be guaranteed. In this case, the client-side hot update engine directly deletes the entire local hot update directory (including all its subdirectories and files) instead of attempting to retain them one by one. This overall cleanup strategy completely eliminates the risk of version mixing caused by some files being expired and others being valid, ensuring that no source code from unknown sources is loaded when the application starts.
[0124] After the cleanup is complete, the client's hot update engine outputs log information, such as detecting expired cache and automatically clearing it, and alerting operations personnel that an anomaly occurred during the last update. Subsequently, the application starts normally based on the complete original code in the original frozen package, and all modules are loaded from embedded resources through the frozen importer, ensuring the determinism and stability of the startup state.
[0125] S04. If no orphaned files exist, retain the local hot update directory and continue to obtain the remote list of the remote update server.
[0126] In this embodiment, if no isolated files exist, the local hot update directory is retained and S1 continues. If, after verification in S02, all source code files in the local hot update directory can be found in the original frozen package, it indicates that the cached content in the directory is complete and matches the original package structure, belonging to a valid cache left over from the last successful hot update.
[0127] At this point, the client-side hot update engine retains the local hot update directory and all its files, and configures module import priorities. Through this priority configuration, during subsequent application runtime, for modules that exist simultaneously in both the local hot update directory and the original frozen package, the application will automatically load the new version of the source code from the local hot update directory. This ensures that the application is in a hot update-enabled state immediately upon startup, without waiting for the next update trigger.
[0128] After retaining the local hot update directory, the application continues to execute the normal business initialization process and executes the S1 to S7 hot update process as needed during operation to obtain the updated version that may exist on the remote server.
[0129] By cleaning up and correcting expired cache during startup using S01 to S04, the frozen application has the ability to self-heal in abnormal scenarios: no matter what caused the last update to be interrupted, the application can automatically detect and repair the cache state when it starts up again, or retain the complete cache for continued use, or completely clear the expired cache and return to the original state, thereby avoiding startup failure or abnormal operation caused by unfinished cache, and improving the reliability and maintainability of the application in industrial field environments.
[0130] This invention also provides a Python application runtime hot update device, which can be configured in a Python application runtime hot update system and is used to execute any of the aforementioned Python application runtime hot update methods. Specifically, please refer to... Figure 9 , Figure 9 This is a schematic block diagram of a Python application runtime hot update device provided in an embodiment of the present invention.
[0131] like Figure 9 As shown, the Python application runtime hot update device 100 includes: The list acquisition unit 110 is used to acquire the remote list of the remote update server, the remote list containing the identifier and hash value of each source code file; The difference detection unit 120 is used to compare the remote list with the local cache to determine the target update file; Incremental download unit 130 is used to download the target update file to a temporary directory; Atomic merging unit 140 is used to acquire an update mutex lock, merge the target update file from the temporary directory to the local hot update directory, and create a backup for the overwritten file; The module reloading unit 150 is used to remove the target module and its associated sub-modules from the system module cache, create a module specification based on the local hot update directory and load the new module code to bypass the frozen importer restriction of the frozen application. The state inheritance unit 160 is used to reconstruct the business facade instance according to the new module code and pass the hardware connection object reference of the old business facade instance to the new business facade instance to maintain the continuity of the hardware connection state. The update confirmation unit 170 is used to save the remote list to update the local cache, release the update mutex lock, and notify that the update is complete.
[0132] In some embodiments, the Python application runtime hot update device 100 further includes: The initial detection unit is used to detect whether the local hot update directory exists when the application starts. The scanning and verification unit is used to scan all source code files in the local hot update directory if they exist, and to verify whether each source code file has a corresponding module in the original frozen package in order to determine whether there are any isolated files. The clearing unit is used to clear the local hot update directory to the automatic clearing operation of expired cache if orphaned files exist; A retention unit is configured to retain the local hot update directory and continue the process of obtaining the remote list of the remote update server if no orphaned files exist. The aforementioned Python application runtime hot update device can be implemented as a computer program, which can, for example... Figure 10 It runs on the computer device shown.
[0133] Please see Figure 10 , Figure 10 This is a schematic block diagram of a computer device provided in an embodiment of the present invention.
[0134] The computer device 500 includes a processor 502, a memory, and a network interface 505 connected via a communication bus 501. The memory may include a storage medium 503 and internal memory 504.
[0135] The storage medium 503 may store an operating system 5031 and a computer program 5032. When the computer program 5032 is executed, it causes the processor 502 to execute a Python method for freezing application runtime hot updates. The storage medium 503 may be a volatile storage medium or a non-volatile storage medium.
[0136] The processor 502 provides computing and control capabilities to support the operation of the entire computer device 500.
[0137] The internal memory 504 provides an environment for the computer program 5032 in the storage medium 503 to run. When the computer program 5032 is executed by the processor 502, the processor 502 can execute the Python freeze application runtime hot update method.
[0138] This network interface 505 is used for network communication, such as providing data transmission. Those skilled in the art will understand that... Figure 10The structure shown is merely a block diagram of a portion of the structure related to the present invention and does not constitute a limitation on the computer device 500 to which the present invention is applied. The specific computer device 500 may include more or fewer components than shown in the figure, or combine certain components, or have different component arrangements.
[0139] The processor 502 is used to run the computer program 5032 stored in the memory to implement the corresponding functions in the above-mentioned Python application runtime hot update method.
[0140] Those skilled in the art will understand that Figure 10 The embodiments of the computer device shown do not constitute a limitation on the specific configuration of the computer device. In other embodiments, the computer device may include more or fewer components than illustrated, or combine certain components, or have different component arrangements. For example, in some embodiments, the computer device may include only memory and a processor. In such embodiments, the structure and function of the memory and processor are different from those shown. Figure 10 The embodiments shown are consistent and will not be repeated here.
[0141] It should be understood that, in this embodiment of the invention, the processor 502 may be a Central Processing Unit (CPU), or it may be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor.
[0142] In another embodiment of the invention, a computer-readable storage medium is provided. This computer-readable storage medium may be volatile or non-volatile. The computer-readable storage medium stores a computer program, wherein the computer program, when executed by a processor, implements the steps included in the above-described Python freeze application runtime hot update method.
[0143] Those skilled in the art will readily understand that, for the sake of convenience and brevity, the specific working processes of the devices, apparatuses, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here. Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in terms of function in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this invention.
[0144] In the embodiments provided by this invention, it should be understood that the disclosed devices, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative. For instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. Units with the same function may be grouped into one unit. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. In addition, the coupling or direct coupling or communication connection shown or discussed may be an indirect coupling or communication connection through some interfaces, devices, or units, or it may be an electrical, mechanical, or other form of connection.
[0145] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of the embodiments of the present invention, depending on actual needs.
[0146] Furthermore, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0147] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a computer-readable storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned computer-readable storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), magnetic disks, or optical disks.
[0148] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any person skilled in the art can easily conceive of various equivalent modifications or substitutions within the technical scope disclosed in the present invention, and these modifications or substitutions should all be covered within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
Claims
1. A Python method for freezing application runtime hot updates, characterized in that, Applied to a hot update system, the hot update system including a remote update server and a client hot update engine that are interconnected, the method includes: Obtain the remote list of the remote update server, which includes the identifier and hash value of each source file; The remote list is compared with the local cache to determine the target update file; Download the target update file to a temporary directory; Acquire an update mutex lock, merge the target update file from the temporary directory into the local hot update directory, and create a backup of the overwritten file; Remove the target module and its associated sub-modules from the system module cache, create a module specification based on the local hot update directory and load the new module code to bypass the frozen importer restriction of the frozen application; The business facade instance is reconstructed based on the new module code, and the hardware connection object reference of the old business facade instance is passed to the new business facade instance to maintain the continuity of the hardware connection state. Save the remote list to update the local cache, release the update mutex lock, and notify that the update is complete.
2. The Python application runtime hot update method according to claim 1, characterized in that, The step of obtaining the remote list of the remote update server includes: The remote update server scans the source code directory, reads the contents of each source code file, and calculates the corresponding hash value. The remote list is constructed based on the file identifier and corresponding hash value of each source file.
3. The Python application runtime hot update method according to claim 1, characterized in that, The step of comparing the remote list with the local cache to determine the target update file includes: Identify files that exist in the remote list but not in the local cache, and add them as new files; Identify files in the remote list that have the same file identifier as those in the local cache but different hash values, and designate them as changed files; The newly added file and the modified file are identified as the target update file.
4. The Python application runtime hot update method according to claim 1, characterized in that, After acquiring the update mutex lock, the process also includes: If an error occurs during the merging process, the overwritten original files are restored based on the backup files, and the merged new files are deleted. Release the update mutex and terminate the current update process.
5. The Python application runtime hot update method according to claim 1, characterized in that, The process of creating a module specification based on the local hot update directory and loading new module code includes: The module specification pointing to the local hot update directory is generated using the file location specification creation function, and the submodule search path is configured to the local hot update directory; The module loader entry function in the module specification is executed to load the new module code into the system module cache.
6. The Python application runtime hot update method according to claim 1, characterized in that, The step of passing the hardware connection object reference of the old business facade instance to the new business facade instance includes: Pass the serial port connection object reference of the old business facade instance to the new business facade instance; Pass the bus connection object reference of the old business facade instance to the new business facade instance; Pass the video capture object reference of the old business facade instance to the new business facade instance.
7. The Python application runtime hot update method according to claim 1, characterized in that, Before obtaining the remote list of the remote update server, the method further includes: When the application starts, it checks if the local hot update directory exists; If it exists, scan all source code files in the local hot update directory and verify whether each source code file has a corresponding module in the original frozen package to determine whether there are any isolated files. If orphaned files exist, the local hot update directory will be cleared to the automatic cache clearing operation for expired files; If no orphaned files exist, the local hot update directory is preserved and the process of obtaining the remote list of the remote update server continues.
8. A Python application runtime hot update device, characterized in that, Configured in a hot update system for performing the method according to any one of claims 1-7, the hot update system comprising a remote update server and a client hot update engine interconnected; the apparatus comprising: The manifest acquisition unit is used to acquire the remote manifest of the remote update server, wherein the remote manifest contains the identifier and hash value of each source code file; The difference detection unit is used to compare the remote list with the local cache to determine the target update file; An incremental download unit is used to download the target update file to a temporary directory; An atomic merging unit is used to acquire an update mutex lock, merge the target update file from the temporary directory to the local hot update directory, and create a backup for the overwritten file. The module reloading unit is used to remove the target module and its associated sub-modules from the system module cache, create a module specification based on the local hot update directory, and load the new module code to bypass the frozen importer restriction of the frozen application. The state inheritance unit is used to reconstruct the business facade instance according to the new module code and pass the hardware connection object reference of the old business facade instance to the new business facade instance to maintain the continuity of the hardware connection state. An update confirmation unit is used to save the remote list to update the local cache, release the update mutex lock, and notify that the update is complete.
9. A computer device, characterized in that, The device includes a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus; Memory, used to store computer programs; When a processor executes a program stored in memory, it implements the steps of the Python method for freezing application runtime hot updates as described in any one of claims 1-7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the steps of the Python method for freezing application runtime hot update as described in any one of claims 1-7.
