A file execution method and device
By creating an execution container under the current thread, the target code object can be executed directly under the execution container, which solves the problems of repetitive steps and data transfer overhead in the multi-threaded concurrent execution of the CPython virtual machine, and improves execution efficiency and resource utilization.
Patent Information
- Application Number
- CN202111517456.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-13
- Publication Date
- 2026-01-23
- Estimated Expiration
- 2042-01-23
AI Technical Summary
When the CPython virtual machine is executed concurrently in a multi-threaded manner, it suffers from problems such as numerous repetitive steps and high performance overhead during data transfer, resulting in low operating efficiency.
An execution container is created in the current thread, and the target code object is executed directly in the execution container, avoiding the main thread from allocating tasks and reusing the target code object, thus reducing the number of data transfers.
It improves execution efficiency in multi-threaded concurrent scenarios, reduces computational resource consumption, and achieves better thread isolation.
Smart Images

Figure CN114201266B_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of computer technology, and more specifically, to a file execution method and apparatus. Background Technology
[0002] A virtual machine (VM) is a software-simulated complete computer system with full hardware system functionality, running in a completely isolated environment, capable of running a wide variety of programs. The CPython VM is the virtual machine used to execute CPython, a Python interpreter implemented in C, while Python is a computer programming language. CPython initializes the various data structures required for Python to run, then interprets the source code into intermediate code, and finally executes the intermediate code to run the program. Due to CPython's architectural limitations, its multi-threaded concurrent execution requires sending the file to the main interpreter, which then sends it to the child interpreters of other threads for parsing and execution. Different threads need to transfer data, and when different threads execute the same file, each thread needs to perform initialization steps to obtain the bytecode object, resulting in many repetitive steps and low execution efficiency. Summary of the Invention
[0003] This disclosure provides at least one file execution method and apparatus.
[0004] In a first aspect, embodiments of this disclosure provide a file execution method, including:
[0005] In response to an execution instruction for a target file, detect whether a target code object corresponding to the target file has been created;
[0006] If the target code object is created, obtain the target code object;
[0007] Create an execution container in the current thread, and use the execution container to execute the target code object.
[0008] In one alternative implementation, before creating an execution container and executing the target code object using the execution container, the method further includes:
[0009] If the target code object has not been created, a target code object corresponding to the target file is created based on the target file; wherein the created target code object can be obtained by other threads outside the current thread.
[0010] In one optional implementation, executing the target code object using the execution container includes:
[0011] When there is a need to create a target object, the target object is created through the execution container, and a level label corresponding to the execution container is configured for the target object.
[0012] The method further includes:
[0013] When the execution container detects that there is an object to be destroyed with a reference count of 0, it determines whether the level label carried by the object to be destroyed is consistent with the level label corresponding to the execution container.
[0014] If the level label carried by the object to be destroyed matches the level label corresponding to the execution container, the object to be destroyed is destroyed using the execution container.
[0015] In one optional implementation, creating a target code object corresponding to the target file based on the target file includes:
[0016] The target file is compiled into bytecode to obtain the target code object.
[0017] In one optional implementation, the target file is a Python file.
[0018] In one optional implementation, executing the target code object using the execution container includes:
[0019] The execution container is used to invoke the Python interpreter;
[0020] The target code object is parsed using the Python interpreter, and the parsing result of the target code object is executed.
[0021] In one optional implementation, the created target code object carries identification information corresponding to the current thread;
[0022] After executing the target code object using the execution container, the method further includes:
[0023] When the current thread detects that the reference count of the target code object is 0, it determines whether the identification information carried by the target code object corresponds to the current thread;
[0024] If the identification information carried by the target code object corresponds to the current thread, the target code object is destroyed.
[0025] Secondly, embodiments of this disclosure also provide a file execution device, comprising:
[0026] The detection module is used to respond to the execution instructions for the target file and detect whether the target code object corresponding to the target file has been created;
[0027] The acquisition module is used to acquire the target code object when the target code object is created;
[0028] The execution module is used to create an execution container in the current thread and use the execution container to execute the target code object.
[0029] In an optional implementation, the execution module is further configured to:
[0030] If the target code object has not been created, a target code object corresponding to the target file is created based on the target file; wherein the created target code object can be obtained by other threads outside the current thread.
[0031] In one optional implementation, when the execution module executes the target code object using the execution container, it is used to:
[0032] When there is a need to create a target object, the target object is created through the execution container, and a level label corresponding to the execution container is configured for the target object.
[0033] The device further includes a destruction module for:
[0034] When the execution container detects that there is an object to be destroyed with a reference count of 0, it determines whether the level label carried by the object to be destroyed is consistent with the level label corresponding to the execution container.
[0035] If the level label carried by the object to be destroyed matches the level label corresponding to the execution container, the object to be destroyed is destroyed using the execution container.
[0036] In one optional implementation, when the execution module creates the target code object corresponding to the target file based on the target file, it is used to:
[0037] The target file is compiled into bytecode to obtain the target code object.
[0038] In one optional implementation, the target file is a Python file.
[0039] In one optional implementation, when the execution module executes the target code object using the execution container, it includes:
[0040] The execution container is used to invoke the Python interpreter;
[0041] The target code object is parsed using the Python interpreter, and the parsing result of the target code object is executed.
[0042] In one optional implementation, the created target code object carries identification information corresponding to the current thread;
[0043] The destruction module is also used for:
[0044] When the current thread detects that the reference count of the target code object is 0, it determines whether the identification information carried by the target code object corresponds to the current thread;
[0045] If the identification information carried by the target code object corresponds to the current thread, the target code object is destroyed.
[0046] Thirdly, embodiments of this disclosure also provide an electronic device, including: a processor, a memory, and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor communicates with the memory via the bus, and when the machine-readable instructions are executed by the processor, the steps of the first aspect above, or any possible implementation of the first aspect, are performed.
[0047] Fourthly, embodiments of this disclosure also provide a computer-readable storage medium storing a computer program that, when executed by a processor, performs the steps of the first aspect or any possible implementation of the first aspect.
[0048] The file execution method and apparatus provided in this disclosure first respond to an execution instruction for a target file by detecting whether a target code object corresponding to the target file has been created; then, if the target code object has been created, the target code object is obtained; finally, an execution container is created in the current thread, and the target code object is executed using the execution container. This disclosure, by creating an execution container in the current thread, directly executes the target code object within the execution container of the current thread, eliminating the need for task allocation by the main thread. This reduces the number of data transfers in multi-threaded concurrent scenarios and reuses the target code object, as each thread does not need to perform file bytecode generation, effectively improving execution efficiency in multi-threaded concurrent scenarios.
[0049] To make the above-mentioned objects, features and advantages of this disclosure more apparent and understandable, preferred embodiments are described below in detail with reference to the accompanying drawings. Attached Figure Description
[0050] To more clearly illustrate the technical solutions of the embodiments of this disclosure, the accompanying drawings used in the embodiments will be briefly described below. These drawings are incorporated in and constitute a part of this specification. They illustrate embodiments conforming to this disclosure and, together with the specification, serve to explain the technical solutions of this disclosure. It should be understood that the following drawings only show some embodiments of this disclosure and should not be considered as limiting the scope. Those skilled in the art can obtain other related drawings based on these drawings without creative effort.
[0051] Figure 1 A flowchart of a file execution method provided by an embodiment of this disclosure is shown;
[0052] Figure 2 A schematic diagram of thread execution provided in an embodiment of this disclosure is shown;
[0053] Figure 3 A schematic diagram illustrating the object invocation relationship provided in an embodiment of this disclosure is shown;
[0054] Figure 4 A schematic diagram of a file execution apparatus provided in an embodiment of this disclosure is shown;
[0055] Figure 5 A schematic diagram of an electronic device provided in an embodiment of this disclosure is shown. Detailed Implementation
[0056] To make the objectives, technical solutions, and advantages of the embodiments of this disclosure clearer, the technical solutions of the embodiments of this disclosure will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of this disclosure, and not all of them. The components of the embodiments of this disclosure described and shown in the accompanying drawings can generally be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of this disclosure provided in the accompanying drawings is not intended to limit the scope of the claimed disclosure, but merely represents selected embodiments of this disclosure. All other embodiments obtained by those skilled in the art based on the embodiments of this disclosure without inventive effort are within the scope of protection of this disclosure.
[0057] It should be noted that similar labels and letters in the following figures indicate similar items. Therefore, once an item is defined in one figure, it does not need to be further defined and explained in subsequent figures.
[0058] In this document, the term "and / or" merely describes a relationship, indicating that three relationships can exist. For example, A and / or B can represent three cases: A alone, A and B simultaneously, and B alone. Furthermore, the term "at least one" in this document means any combination of at least two of any one or more elements. For example, including at least one of A, B, and C can mean including any one or more elements selected from the set consisting of A, B, and C.
[0059] Research has revealed that in existing CPython virtual machines, when performing multi-threaded concurrent tasks, the main interpreter is bound to the thread. Subsequent tasks must be submitted on the main interpreter's thread, meaning that subsequent tasks must go through the main interpreter before being allocated to other child interpreters, resulting in poor performance. Furthermore, during the process of passing tasks from the main interpreter to the child interpreters, since the main interpreter and child interpreters are on different threads, parameter passing requires copying operations to ensure safety between different threads, further increasing the performance overhead during task execution. In addition, the private data of different threads needs to be isolated, and accessing the isolated data also consumes more performance.
[0060] Based on the above research, this disclosure provides a file execution method that creates an execution container under the current thread and executes the target code object directly under the execution container of the current thread. This eliminates the need for the main thread to allocate tasks, reduces the number of data transfers in multi-threaded concurrent scenarios, and reuses the target code object, effectively improving the execution efficiency in multi-threaded concurrent scenarios.
[0061] To facilitate understanding of this embodiment, a file execution method disclosed in this disclosure will first be described in detail. The execution subject of the file execution method provided in this disclosure is generally a computer device with certain computing capabilities, such as a terminal device, a server, or other processing devices. In some possible implementations, the file execution method can be implemented by a processor calling computer-readable instructions stored in memory.
[0062] See Figure 1 The diagram shows a flowchart of a file execution method provided in an embodiment of this disclosure. The method includes steps S101 to S103, wherein:
[0063] S101. In response to the execution instruction for the target file, detect whether the target code object corresponding to the target file has been created.
[0064] In this step, the computer device that serves as the execution subject of this method can run a virtual machine. This virtual machine can be used to execute applications written in the Python language, i.e., the aforementioned target file. The virtual machine can load and initialize the target file, compile the target file into bytecode, i.e., the aforementioned target code object. The virtual machine can run multiple threads. When a thread, i.e. the current thread, detects the execution instructions of the target file, it can detect whether the target code object corresponding to the target file has been created.
[0065] The target code object is the executable file obtained by parsing the target file. The target file can be a Python file, and the target code object corresponding to the target file can be obtained by compiling and parsing.
[0066] In this step, after the current thread accepts the task of running the target file, it can first determine whether the target code object corresponding to the target file has been created by another thread. If it has been created, the target code object can be obtained directly, thus saving the step of parsing the target file.
[0067] After a target code object is created, it can be stored in an area accessible to various threads, such as shared memory. The current thread can search for the target code object from shared memory. When querying a target code object, it can be queried based on the mapping relationship between the target code object and the target file, or the identification information of the two.
[0068] S102. If the target code object is created, obtain the target code object.
[0069] In this step, if the target code object is found, it can be obtained, and the methods of obtaining it include, but are not limited to, reading and copying.
[0070] Furthermore, if the target code object cannot be found, or the target code object has not been created, the target code object corresponding to the target file can be created based on the target file. The created target code can be obtained by other threads outside the current thread. For example, the target file can be parsed and compiled into executable bytecode to obtain the target code object, and the target code object can be stored in shared memory.
[0071] In this way, other threads can directly obtain the target code object when executing the target file, eliminating the need for parsing.
[0072] S103. Create an execution container in the current thread and use the execution container to execute the target code object.
[0073] In this step, an execution container can be created under the current thread. This execution container can provide an execution environment for the target code object and can perform tasks such as creating, releasing, and caching various objects required for the execution of the target code object. The creation and destruction of the execution container is lightweight and consumes less computing resources. After executing the target code object using the execution container, the execution container can be destroyed, thereby achieving mutual isolation between different execution tasks, ensuring the safety between multiple threads, and reducing the computing resources consumed by the execution of the target file.
[0074] For example, the execution container can provide the context data required by the target code object. The execution container can obtain objects at multiple levels, such as task level, code level, and global level. Task level variables are usually temporary variables required for execution operations and can be created and destroyed by the execution container.
[0075] For example, code-level variables typically refer to target code objects, which are variables compiled from object files; global-level variables, on the other hand, can be commonly used objects created during virtual machine initialization. Because they are frequently used, they are at the highest global level. Generally, global level is the highest level, task level is the lowest level, and code level is between global and task level.
[0076] Similar to code-level target code objects, global-level commonly used variables and task-level temporary variables can also be created by other threads and shared.
[0077] When executing a target code object using an execution container, the execution container can first obtain various commonly used objects needed at the global level, as well as the target code object at the code level, and create some necessary task-level variables. Using the obtained variables, it calls the Python interpreter to parse the target code object, obtain executable intermediate code, and finally execute the intermediate code, which is the parsing result of the target code object, to obtain the execution result of the target code object.
[0078] After obtaining the execution result of the target code object, the execution container can be destroyed. When destroying the execution container, each object can be released or destroyed according to the level of the execution container and the level of each object called by the execution container. For example, since the execution container is the lowest level and the most lightweight object, the level of the execution container can be set to the task level. Then, when the execution task is destroyed, the temporary variables created by the execution container that are also at the task level can be destroyed, and the target code object and commonly used global variables that are higher than the task level can be released, thereby ensuring that other threads can also use code-level and global-level objects.
[0079] See Figure 2 The diagram shown is a schematic representation of thread execution according to an embodiment of this disclosure. Figure 2 The process includes threads A and B. When thread A receives the task to execute the target file main.py, it checks whether the target code object CodeObject corresponding to main.py has been created. If it finds that this is the first time main.py is executed and CodeObject has not been created, it directly compiles main.py to obtain CodeObject, stores it in shared memory or a cache, then creates an execution container Context, executes CodeObject, and outputs the execution result. When thread B receives the task to execute main.py, it detects the previously created CodeObject, directly calls CodeObject, creates Context, executes CodeObject, and outputs the execution result.
[0080] As can be seen, since thread B can use the CodeObject compiled by thread A, it does not need to compile main.py again, saving computing resources. Furthermore, due to the existence of the execution container, the execution process and results of main.py between thread A and thread B are isolated, achieving a good isolation effect with less computing resources.
[0081] Cache and share target code files can reduce the computational resources consumed by multiple threads executing the same file. However, if target code files are continuously cached, it may cause problems such as memory shortage and slow program response. Therefore, it is necessary to manage the lifecycle of target code files and other levels of objects and destroy objects that are not needed.
[0082] First, regarding task-level objects, when executing target code objects using an execution container, it may be necessary to create some temporary objects. These temporary objects can be used as target objects. When there is a need to create target objects, these target objects can be created by the execution container, and the target objects can be configured with the level label corresponding to the execution container, such as the task-level label mentioned above. When the execution container detects that an object has a reference count of 0, it can be considered that the object no longer needs to exist, and the level label carried by these objects to be destroyed can be determined. If the level label is consistent with the level label of the execution container, that is, task-level, then it can be destroyed.
[0083] Secondly, for code-level target code objects, when the current thread or other threads create a target code object, they can assign the target code object with identification information corresponding to the current thread, such as the code-level identification information mentioned above. After the target code object is executed using the execution container, if it is detected that the reference count of the target code object is 0, it can be determined whether the identification information carried by the target code object corresponds to the current thread, such as whether they are both at the code level. If the identification information carried by the target code object corresponds to the current thread, the target code object can be destroyed.
[0084] In other words, threads and execution containers can only destroy objects when they are of the same or higher level than the other.
[0085] This prevents the execution container from destroying objects at higher levels, thus avoiding system errors caused by missing objects.
[0086] For example, such as Figure 3 The diagram shown is a schematic representation of the object calling relationship provided in an embodiment of this disclosure. Figure 3 In the process of thread A and thread B executing the target file, the code-level container Code Level Context can be called to parse the target file and obtain a CodeObject (rectangle). Then, a task-level execution container Task Level Context is created. The Task Level Context can call the code-level CodeObject and global-level objects (circles), and create some temporary objects (triangles). It executes the CodeObject according to the called object and outputs the execution result. Correspondingly, the Code Level Context can destroy the CodeObject, and the Task Level Context can destroy the temporary objects.
[0087] The file execution method provided in this disclosure first responds to an execution instruction for a target file by detecting whether a target code object corresponding to the target file has been created; then, if the target code object has been created, the target code object is obtained; finally, an execution container is created in the current thread, and the target code object is executed using the execution container. This disclosure, by creating an execution container in the current thread, directly executes the target code object within the execution container of the current thread, eliminating the need for task allocation by the main thread. This reduces the number of data transfers in multi-threaded concurrent scenarios and reuses the target code object. Since each thread does not need to bytecode the file, the execution efficiency in multi-threaded concurrent scenarios is effectively improved.
[0088] Those skilled in the art will understand that, in the above-described method of the specific implementation, the order in which each step is written does not imply a strict execution order and does not constitute any limitation on the implementation process. The specific execution order of each step should be determined by its function and possible internal logic.
[0089] Based on the same inventive concept, this disclosure also provides a file execution device corresponding to the file execution method. Since the principle of the device in this disclosure for solving the problem is similar to the file execution method described above in this disclosure, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be described again.
[0090] Reference Figure 4 The diagram shown is a schematic representation of a file execution device provided in an embodiment of this disclosure. The device includes:
[0091] The detection module 410 is used to respond to the execution instruction for the target file and detect whether the target code object corresponding to the target file has been created;
[0092] The acquisition module 420 is used to acquire the target code object when the target code object is created;
[0093] The execution module 430 is used to create an execution container in the current thread and use the execution container to execute the target code object.
[0094] In an optional implementation, the execution module 430 is further configured to:
[0095] If the target code object has not been created, a target code object corresponding to the target file is created based on the target file; wherein the created target code object can be obtained by other threads outside the current thread.
[0096] In one optional implementation, when the execution module 430 executes the target code object using the execution container, it is used to:
[0097] When there is a need to create a target object, the target object is created through the execution container, and a level label corresponding to the execution container is configured for the target object.
[0098] The device further includes a destruction module for:
[0099] When the execution container detects that there is an object to be destroyed with a reference count of 0, it determines whether the level label carried by the object to be destroyed is consistent with the level label corresponding to the execution container.
[0100] If the level label carried by the object to be destroyed matches the level label corresponding to the execution container, the object to be destroyed is destroyed using the execution container.
[0101] In an optional implementation, when the execution module 430 creates the target code object corresponding to the target file based on the target file, it is used to:
[0102] The target file is compiled into bytecode to obtain the target code object.
[0103] In one optional implementation, the target file is a Python file.
[0104] In one optional implementation, when the execution module 430 executes the target code object using the execution container, it includes:
[0105] The execution container is used to invoke the Python interpreter;
[0106] The target code object is parsed using the Python interpreter, and the parsing result of the target code object is executed.
[0107] In one optional implementation, the created target code object carries identification information corresponding to the current thread;
[0108] The destruction module is also used for:
[0109] When the current thread detects that the reference count of the target code object is 0, it determines whether the identification information carried by the target code object corresponds to the current thread;
[0110] If the identification information carried by the target code object corresponds to the current thread, the target code object is destroyed.
[0111] The processing flow of each module in the device and the interaction flow between each module can be referred to the relevant descriptions in the above method embodiments, and will not be detailed here.
[0112] Corresponding to Figure 1 In addition to the file execution method described in this disclosure, an electronic device 500 is also provided in this embodiment, such as... Figure 5 The diagram shown is a structural schematic of an electronic device 500 provided in an embodiment of this disclosure, including:
[0113] The system includes a processor 51, a memory 52, and a bus 53. The memory 52 stores execution instructions and includes main memory 521 and external memory 522. The main memory 521, also called internal memory, temporarily stores the computational data in the processor 51, as well as data exchanged with external memory such as a hard disk. The processor 51 exchanges data with the external memory 522 through the main memory 521. When the electronic device 500 is running, the processor 51 communicates with the memory 52 through the bus 53, causing the processor 51 to execute the following instructions:
[0114] In response to an execution instruction for a target file, detect whether a target code object corresponding to the target file has been created;
[0115] If the target code object is created, obtain the target code object;
[0116] Create an execution container in the current thread, and use the execution container to execute the target code object.
[0117] In one alternative implementation, before creating the execution container and executing the target code object using the execution container, the processor 51 is further configured to execute:
[0118] If the target code object has not been created, a target code object corresponding to the target file is created based on the target file; wherein the created target code object can be obtained by other threads outside the current thread.
[0119] In one optional implementation, the instructions executed by the processor 51, wherein executing the target code object using the execution container, includes:
[0120] When there is a need to create a target object, the target object is created through the execution container, and a level label corresponding to the execution container is configured for the target object.
[0121] The processor 51 is also used to perform:
[0122] When the execution container detects that there is an object to be destroyed with a reference count of 0, it determines whether the level label carried by the object to be destroyed is consistent with the level label corresponding to the execution container.
[0123] If the level label carried by the object to be destroyed matches the level label corresponding to the execution container, the object to be destroyed is destroyed using the execution container.
[0124] In one optional implementation, the instruction executed by the processor 51, wherein creating a target code object corresponding to the target file based on the target file, includes:
[0125] The target file is compiled into bytecode to obtain the target code object.
[0126] In one optional implementation, the target file is a Python file.
[0127] In one optional implementation, the instructions executed by the processor 51, wherein executing the target code object using the execution container, includes:
[0128] The execution container is used to invoke the Python interpreter;
[0129] The target code object is parsed using the Python interpreter, and the parsing result of the target code object is executed.
[0130] In one optional implementation, the created target code object carries identification information corresponding to the current thread;
[0131] After executing the target code object using the execution container, the processor 51 is further configured to execute:
[0132] When the current thread detects that the reference count of the target code object is 0, it determines whether the identification information carried by the target code object corresponds to the current thread;
[0133] If the identification information carried by the target code object corresponds to the current thread, the target code object is destroyed.
[0134] This disclosure also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, performs the steps of the file execution method described in the above method embodiments. The storage medium can be a volatile or non-volatile computer-readable storage medium.
[0135] This disclosure also provides a computer program product carrying program code. The program code includes instructions that can be used to execute the steps of the file execution method described in the above method embodiments. For details, please refer to the above method embodiments, which will not be repeated here.
[0136] The aforementioned computer program product can be implemented through hardware, software, or a combination thereof. In one optional embodiment, the computer program product is specifically embodied in a computer storage medium; in another optional embodiment, the computer program product is specifically embodied in a software product, such as a software development kit (SDK), etc.
[0137] Those skilled in the art will readily understand that, for the sake of convenience and brevity, the specific working process of the above description and apparatus can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here. In the several embodiments provided in this disclosure, it should be understood that the disclosed apparatus and method can be implemented in other ways. The apparatus embodiments described above are merely illustrative. For example, the division of units is only a logical functional division; in actual implementation, there may be other division methods. Furthermore, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Additionally, the displayed or discussed mutual coupling or direct coupling or communication connection may be through some communication interfaces; the indirect coupling or communication connection of apparatus or units may be electrical, mechanical, or other forms.
[0138] 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 this embodiment according to actual needs.
[0139] In addition, the functional units in the various embodiments of this disclosure 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.
[0140] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a processor-executable, non-volatile, computer-readable storage medium. Based on this understanding, the technical solution of this disclosure, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a 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 this disclosure. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0141] Finally, it should be noted that the above-described embodiments are merely specific implementations of this disclosure, used to illustrate the technical solutions of this disclosure, and not to limit it. The protection scope of this disclosure is not limited thereto. Although this disclosure has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that any person skilled in the art can still modify or easily conceive of changes to the technical solutions described in the foregoing embodiments, or make equivalent substitutions for some of the technical features, within the scope of the technology disclosed in this disclosure. Such modifications, changes, or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this disclosure, and should all be covered within the protection scope of this disclosure. Therefore, the protection scope of this disclosure should be determined by the protection scope of the claims.
Claims
1. A file execution method, characterized in that, include: In response to an execution instruction for a target file, detect whether a target code object corresponding to the target file has been created; If the target code object is created, obtain the target code object; Create an execution container in the current thread, and use the execution container to execute the target code object; Based on the level of the execution container and the level of each object invoked by the execution container, release or destroy each object. The step of executing the target code object using the execution container includes: When there is a need to create a target object, the target object is created through the execution container, and a level label corresponding to the execution container is configured for the target object. The level label is used to determine whether the execution container is allowed to destroy the object to be destroyed. The execution container provides the context data required to execute the target code object.
2. The method according to claim 1, characterized in that, Before creating an execution container and executing the target code object using the execution container, the method further includes: If the target code object has not been created, a target code object corresponding to the target file is created based on the target file; wherein the created target code object can be obtained by other threads other than the current thread.
3. The method according to claim 1, further comprising: When the execution container detects that there is an object to be destroyed with a reference count of 0, it determines whether the level label carried by the object to be destroyed is consistent with the level label corresponding to the execution container. If the level label carried by the object to be destroyed matches the level label corresponding to the execution container, the object to be destroyed is destroyed using the execution container.
4. The method according to claim 2, characterized in that, The step of creating a target code object corresponding to the target file based on the target file includes: The target file is compiled into bytecode to obtain the target code object.
5. The method according to claim 1, characterized in that, The target file is a Python file.
6. The method according to claim 5, characterized in that, The step of executing the target code object using the execution container includes: The execution container is used to invoke the Python interpreter; The target code object is parsed using the Python interpreter, and the parsing result of the target code object is executed.
7. The method according to claim 2, characterized in that, The created target code object carries identification information corresponding to the current thread; After executing the target code object using the execution container, the method further includes: When the current thread detects that the reference count of the target code object is 0, it determines whether the identification information carried by the target code object corresponds to the current thread; If the identification information carried by the target code object corresponds to the current thread, the target code object is destroyed.
8. A file execution device, characterized in that, include: The detection module is used to respond to the execution instructions for the target file and detect whether the target code object corresponding to the target file has been created; The acquisition module is used to acquire the target code object when the target code object is created; An execution module is used to create an execution container in the current thread and execute the target code object using the execution container; and to release or destroy each object according to the level of the execution container and the level of each object called by the execution container. The execution module is further configured to, when there is a need to create a target object, create the target object through the execution container, and configure a level label corresponding to the execution container for the target object. The level label is used to determine whether the execution container is allowed to destroy the object to be destroyed. The execution container provides the context data required to execute the target code object.
9. An electronic device, characterized in that, include: The device includes a processor, a memory, and a bus. The memory stores machine-readable instructions executable by the processor. When the electronic device is running, the processor communicates with the memory via the bus. When the machine-readable instructions are executed by the processor, the steps of the file execution method as described in any one of claims 1 to 7 are performed.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, performs the steps of the file execution method as described in any one of claims 1 to 7.
Citation Information
Patent Citations
Program running and virtual machine instance realization method and device, equipment and medium
CN110058859A
Statistical method and device for public function execution time consumption, storage medium and terminal
CN112099798A