Code execution method and device, electronic equipment and storage medium
By pre-creating and warming up a multi-language sandbox process pool during the sandbox system startup phase, and performing health checks and process reuse in high-concurrency scenarios, the startup delay, resource overhead, and security issues of traditional online programming platforms are resolved, achieving efficient and stable code execution.
Patent Information
- Application Number
- CN202511820909.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-04
- Publication Date
- 2026-04-10
AI Technical Summary
Traditional online programming platforms suffer from high startup latency, high resource consumption, insecure code transmission, and poor concurrency performance when executing multi-language code. The lack of unified process pool management and health checks leads to poor system stability.
During the sandbox system startup phase, a multi-language sandbox process pool is pre-created and preheated. Code security is ensured through encrypted transmission and asynchronous return mechanisms. In high-concurrency scenarios, health checks and process reuse are performed to improve response speed and stability.
By using a preheating process pool and health check mechanism, response speed and concurrency performance are significantly improved, code transmission security is ensured, and efficient reuse of process resources is achieved, ensuring stable operation of the system in high-concurrency scenarios.
Smart Images

Figure CN121834794A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer, in particular to a code execution method and device, electronic equipment and storage medium. BACKGROUND
[0002] With the evolution of software development technology, modern application development increasingly relies on heterogeneous technology stacks composed of multiple programming languages such as Python, JavaScript, Java, and Go. To adapt to this trend, online programming services on cloud computing platforms must provide native support for multi-language environments to meet the diverse programming and learning needs of developers.
[0003] To support dynamic execution of multi-language code, the core technology of the online programming platform is to create and manage a sandboxed execution environment (such as a process or container) for each language on the server side to ensure the isolation and security of user code. The traditional implementation is to temporarily create and initialize a brand-new language-specific sandbox process for each execution request.
[0004] However, the traditional solution has the following technical problems: relying on temporary process creation results in high startup delay and high resource overhead; lacking unified process pool management, unable to effectively reuse resources; plaintext code transmission poses security risks; execution result synchronous return affects concurrent performance; and lacking health checks, poor system stability. SUMMARY
[0005] The present application provides a code execution method, device, electronic equipment and storage medium for improving response speed, code transmission security, and concurrent performance and stability of the sandbox system, and effectively reusing process resources to reduce resource overhead.
[0006] In a first aspect, the present application provides a code execution method applied to a sandbox system, the method comprising: In the startup phase of the sandbox system, for the runtime environment of each programming language, a sandbox process pool is created and initialized for the runtime environment according to predefined process parameters; and in the initialization phase of the sandbox system, the sandbox process pool is preheated; After receiving each code execution request, an idle preheated target sandbox process is allocated from the corresponding sandbox process pool according to the target programming language type carried in the code execution request; The code execution request carries the to-be-executed code, which is encrypted and transmitted to the target sandbox process, decrypted, code executed, and execution result returned asynchronously in the isolated environment of the target sandbox process; perform a health check on the target sandbox process before returning the target sandbox process, and determine to destroy or recycle the target sandbox process into a corresponding sandbox process pool for reuse according to the health check result.
[0007] The technical solutions provided in the present application bring at least the following beneficial effects: Firstly, by pre-creating and initializing the sandbox process pool corresponding to the running environment of different programming languages according to the pre-defined process parameters at the startup stage of the sandbox system, and performing the key process warm-up operation at the initialization stage of the sandbox system, the process resource of the ready-to-use and immediately executable code is prepared before the sandbox system formally processes the request; then, after receiving each code execution request, an idle and pre-warmed target sandbox process is allocated from the corresponding sandbox process pool according to the target programming language type carried in the code execution request, the process resource of the ready-to-use and immediately executable code can be allocated for the massive requests, thereby improving the response speed of the massive requests; next, the to-be-executed code carried in the code execution request is encrypted and transmitted to the target sandbox process, and the decryption, code execution and asynchronous return of the execution result are performed in the isolated environment of the target sandbox process; in this way, the data security in the code transmission process can be effectively guaranteed by encrypting the transmission of the code, and the concurrent performance of the sandbox system can be greatly improved by asynchronously returning the execution result; finally, the health check is performed on the target sandbox process before returning the target sandbox process, and the target sandbox process is determined to be destroyed or recycled into the corresponding sandbox process pool for reuse according to the health check result, the process can be destroyed or recycled for reuse through the health check, and the stability of the sandbox system in the high-concurrency scenario can be ensured. Moreover, a controlled sandbox process pool is maintained for the running environment of each programming language, and a unified dynamic allocation, destruction, recycling and other management strategies are implemented, so that the limited process resources can be recycled in the high-concurrency scenario, and the huge overhead of creating and destroying the process for each request can be completely avoided.
[0008] In a possible implementation, after receiving each code execution request, the method further includes: If there is no idle process in the sandbox process pool, the code execution request is added to a first-in-first-out queue for waiting; If the waiting time of the code execution request in the first-in-first-out queue exceeds a preset timeout threshold, the invalid requests in the first-in-first-out queue are cleaned up.
[0009] In a possible implementation, the encrypted transmission of the to-be-executed code carried in the code execution request to the target sandbox process includes: The to-be-executed code carried in the code execution request is encrypted by a random key to obtain binary data; encoding the binary data into a secure code package conforming to a text transmission standard; transmitting the secure code package to the target sandbox process through a standard input channel.
[0010] In a possible implementation, the decrypting, code executing and execution result asynchronous returning in the isolated environment of the target sandbox process include: decrypting the secure code package in a sub-process in the isolated environment of the target sandbox process to obtain the to-be-executed code, and performing an execution operation on the to-be-executed code to obtain an execution result; asynchronously returning the execution result through an independent output channel, and notifying an execution termination state through a completion channel.
[0011] In a possible implementation, the determining, according to the health check result, whether to destroy the target sandbox process or recycle the target sandbox process into a corresponding sandbox process pool for reuse includes: if the health check result is that the number of uses of the target sandbox process is greater than a preset maximum use number threshold or the maximum survival time of the target sandbox process is greater than a preset maximum survival time threshold, destroying the target sandbox process; otherwise, recycling the target sandbox process into a corresponding sandbox process pool for reuse.
[0012] In a possible implementation, the method further includes: monitoring the number of sandbox processes in the sandbox process pool, and if the number of sandbox processes is less than a preset minimum process number, creating a new sandbox process so that the number of sandbox processes in the sandbox process pool reaches the preset minimum process number.
[0013] In a possible implementation, the method further includes: re-performing process creation in the case of process creation failure, wherein the number of times of re-performing process creation does not exceed a preset maximum retry number.
[0014] In a possible implementation, the method further includes: when the sandbox system is closed or abnormally terminated, performing an exit flow, orderly terminating all active processes in each of the sandbox process pools, and cleaning up remaining requests in the first-in-first-out queue and releasing related resources.
[0015] In a second aspect, the present application provides a code execution apparatus applied to a sandbox system, the apparatus including: A preheating module is configured to create and initialize a sandbox process pool for each runtime environment of a programming language according to predefined process parameters during a startup phase of the sandbox system, and to preheat the sandbox process pool during an initialization phase of the sandbox system. An allocation module is configured to allocate an idle preheated target sandbox process from the corresponding sandbox process pool according to a target programming language type carried in the code execution request each time a code execution request is received. An execution module is configured to encrypt and transmit the to-be-executed code carried in the code execution request to the target sandbox process, and to decrypt, execute the code, and return the execution result asynchronously in the isolated environment of the target sandbox process. An inspection module is configured to perform a health inspection on the target sandbox process before returning the target sandbox process, and to determine whether to destroy the target sandbox process or recycle the target sandbox process into the corresponding sandbox process pool for reuse according to the health inspection result.
[0016] In a third aspect, the present application provides an electronic device, comprising a processor and a memory; the memory stores instructions executable by the processor; the processor is configured to execute the instructions, so that the electronic device implements the method of the first aspect.
[0017] In a fourth aspect, the present application provides a computer-readable storage medium, comprising computer software instructions; when the computer software instructions run in an electronic device, the electronic device implements the method of the first aspect.
[0018] In a fifth aspect, the present application provides a computer program product, comprising a computer program; when the computer program runs in an electronic device, the electronic device implements the method of the first aspect.
[0019] The beneficial effects of the second aspect to the fifth aspect are described with reference to the corresponding description of the first aspect, and will not be repeated. BRIEF DESCRIPTION OF DRAWINGS
[0020] Figure 1 An application scenario diagram of a code execution method provided by the prior art; Figure 2 A flowchart of a code execution method provided by an embodiment of the present application; Figure 3 A specific flowchart of a code execution method provided by an embodiment of the present application; Figure 4 A composition diagram of a code execution device provided by an embodiment of the present application; Figure 5This is a schematic diagram of the composition of an electronic device provided in an embodiment of this application. Detailed Implementation
[0021] The following is a detailed description of a call detail record (CDR) data recording method provided in this application, with reference to the accompanying drawings.
[0022] In this article, the term "and / or" is merely a description of the relationship between related objects, indicating that there can be three relationships. For example, A and / or B can represent three situations: A exists alone, A and B exist simultaneously, and B exists alone.
[0023] The terms "first" and "second," etc., used in the specification and drawings of this application are used to distinguish different objects or to distinguish different treatments of the same object, rather than to describe a specific order of objects.
[0024] Furthermore, the terms "comprising" and "having," and any variations thereof, used in the description of this application are intended to cover non-exclusive inclusion. For example, a process, method, system, product, or apparatus that includes a series of steps or units is not limited to the steps or units listed, but may optionally include other steps or units not listed, or may optionally include other steps or units inherent to such process, method, product, or apparatus.
[0025] It should be noted that in the embodiments of this application, the words "exemplary" or "for example" are used to indicate examples, illustrations, or explanations. Any embodiment or design scheme described as "exemplary" or "for example" in the embodiments of this application should not be construed as being more preferred or advantageous than other embodiments or design schemes. Specifically, the use of the words "exemplary" or "for example" is intended to present the relevant concepts in a specific manner.
[0026] To facilitate a clear description of the technical solutions of the embodiments of this application, the terms "first" and "second" are used in the embodiments of this application to distinguish the same or similar items with essentially the same function and effect. Those skilled in the art can understand that the terms "first" and "second" are not intended to limit the quantity or execution order.
[0027] In the description of this application, unless otherwise stated, "a plurality of" means two or more.
[0028] like Figure 1As shown, the traditional approach first uses a request dispatcher to dynamically create a brand-new sandbox process for each incoming code execution request (e.g., a Python process for a Python request), which is then immediately destroyed after executing the code. This "use-and-discard" model leads to significant performance bottlenecks: on the one hand, each request needs to wait for a cold start, causing high latency; on the other hand, the frequent creation and destruction of processes continuously consumes a large amount of CPU and memory resources, resulting in high overhead. Furthermore, the traditional approach also suffers from the following technical problems: lack of unified process pool management, making it impossible to effectively reuse resources; plaintext code transmission poses security risks; synchronous return of execution results affects concurrent performance; and the lack of health checks leads to poor system stability.
[0029] To address the aforementioned technical problems, this application provides a code execution method, apparatus, electronic device, and storage medium. The approach is as follows: by pre-creating and warming up a multi-language sandbox process pool, resource preparation is completed during the system startup phase, enabling high-concurrency requests to be immediately allocated to readily available processes, thereby completely eliminating cold start latency and significantly improving response speed; encrypted transmission and asynchronous return mechanisms ensure code security and system concurrency performance; finally, health checks and process reuse strategies achieve resource recycling and stable system operation, fundamentally avoiding the huge overhead caused by frequent process creation and destruction in traditional solutions.
[0030] The embodiments provided in this application will now be described in detail with reference to the accompanying drawings. See Figure 2 This is a flowchart illustrating a code execution method provided in an embodiment of this application. Figure 2 As shown, the code execution method provided in this application can be implemented through a sandbox system, specifically including the following steps S201~S204.
[0031] S201. During the startup phase of the sandbox system, a sandbox process pool is created and initialized for each programming language runtime environment according to predefined process parameters; and during the initialization phase of the sandbox system, the sandbox process pool is preheated.
[0032] In some embodiments, the programming languages can encompass a wide range of mainstream and domain-specific languages to meet the diverse user needs of online programming platforms. Specifically, its process pool can be pre-configured with runtime environments for, but not limited to, interpreted languages (such as Python, JavaScript (Node.js), Ruby, PHP), compiled languages (such as Java, Go, Rust, C++), and domain-specific languages (such as R for data processing, Julia for scientific computing, etc.).
[0033] For example, during the startup phase of the sandbox system, the global process pool manager is first initialized through configuration parameters, and process parameters for different programming language runtime environments are predefined according to the system configuration: process pool capacity. Maximum capacity of process pool Minimum number of processes and the amount of preheating Subsequently, during the initialization phase of the sandbox system, a process warm-up mechanism is initiated, creating a specified number of processes in parallel. The initial process instances are generated and these processes are placed into the corresponding free buffers, so that the service can provide efficient response capabilities in the early stage of startup and effectively alleviate the performance bottleneck caused by cold start.
[0034] S202. Upon receiving a code execution request, allocate an idle, pre-warmed target sandbox process from the corresponding sandbox process pool based on the target programming language type carried in the code execution request.
[0035] Specifically, after each code execution request is received, the target programming language type carried in the code execution request is extracted, and it is determined whether there is an idle process in the sandbox process pool corresponding to the target programming language type. If so, an idle and preheated target sandbox process is immediately allocated from it.
[0036] In some embodiments, after each code execution request is received, the method further includes: if there are no idle processes in the sandbox process pool, the code execution request is added to a first-in-first-out queue for waiting; if the waiting time of the code execution request in the first-in-first-out queue exceeds a preset timeout threshold, invalid requests in the first-in-first-out queue are cleaned up.
[0037] For example, upon receiving each code execution request, if there are no idle processes in the sandbox process pool, the code execution request is added to the waiting queue according to the first-in-first-out (FIFO) principle to avoid blocking subsequent requests and improve scheduling fairness. Simultaneously, the sandbox system supports context timeout control and request cancellation mechanisms, with a timeout threshold set to... If in For code execution requests that have not been processed for a long time or have been actively canceled, they are automatically removed from the queue after the timeout to prevent invalid requests from consuming resources. This can improve the fairness and response efficiency of task scheduling in high-concurrency scenarios of the sandbox system.
[0038] S203. Encrypt the code to be executed carried in the code execution request and transmit it to the target sandbox process. Decryption, code execution, and asynchronous return of execution results are performed in the isolated environment of the target sandbox process.
[0039] In some embodiments, step S203, which involves encrypting and transmitting the executable code carried in the code execution request to the target sandbox process, includes: encrypting the executable code carried in the code execution request with a random key to obtain binary data; encoding the binary data into a secure code packet conforming to the text transmission standard; and transmitting the secure code packet to the target sandbox process through the standard input channel.
[0040] For example, first, a random key is generated, and the code to be executed is encrypted using an XOR encryption algorithm to obtain binary data. Then, Base64 encoding is used to encode the binary data into a secure code packet in plain text format to ensure that the data is transmitted intact during transmission. This secure code packet is transmitted to the target sandbox process through the standard input channel.
[0041] In some embodiments, step S203, which involves decrypting, executing code, and asynchronously transmitting execution results within the isolated environment of the target sandbox process, includes: within the isolated environment of the target sandbox process, decrypting the secure code package in a child process to obtain the code to be executed, performing execution operations on the code to be executed, and obtaining the execution result; asynchronously transmitting the execution result through an independent output channel, and notifying the execution termination status through a completion channel.
[0042] For example, within the isolated environment of the target sandbox process, a child process performs the decryption and execution of secure code. Execution results are returned asynchronously via an independent output channel, distinguishing between standard output and error messages. The execution termination status (success or failure) is notified via a completion channel. This ensures reliable and isolated communication, preventing sensitive code from being stolen or tampered with during transmission.
[0043] S204. Before returning the target sandbox process, perform a health check on the target sandbox process and determine whether to destroy or recycle the target sandbox process to the corresponding sandbox process pool for reuse based on the health check results.
[0044] In some embodiments, step S204, which determines whether to destroy or recycle the target sandbox process to the corresponding sandbox process pool for reuse based on the health check results, includes: if the health check results indicate that the number of times the target sandbox process is used is greater than a preset maximum usage threshold or the maximum survival time of the target sandbox process is greater than a preset maximum survival time threshold, then the target sandbox process is destroyed; otherwise, the target sandbox process is recycled to the corresponding sandbox process pool for reuse.
[0045] For example, after the code execution is complete, a health check is performed on the target sandbox process before returning it to the sandbox process pool. The number of times the target sandbox process is used is... Survival time is Let the maximum number of uses threshold be... The maximum survival time threshold is .if > or > If the process fails, the target sandbox process will be destroyed to prevent abnormal processes from affecting the performance of the sandbox system; otherwise, a healthy and unexpired target sandbox process will be put back into the sandbox process pool for reuse.
[0046] In some embodiments, the method further includes: monitoring the number of sandbox processes in the sandbox process pool; if the number of sandbox processes is less than a preset minimum number of processes, creating a new sandbox process so that the number of sandbox processes in the sandbox process pool reaches the preset minimum number of processes.
[0047] For example, at time t, the number of processes in the sandbox process pool is... ,when At that time, it is necessary to create A new process is added to supplement the process pool resources, maintain the minimum service capacity of the process pool, and reduce instantaneous latency under high concurrency.
[0048] In some embodiments, the method further includes: recreating the process if the process creation fails; wherein the number of times the process creation is recreated does not exceed a preset maximum number of retries.
[0049] For example, a retry mechanism with a backoff strategy is implemented to handle process creation failures, i.e., the process is recreated if creation fails, with a maximum number of retries limited to [number missing]. This avoids infinite loop attempts that can burden the system.
[0050] In some embodiments, the method further includes: when the sandbox system is shut down or terminated abnormally, executing an exit procedure to sequentially terminate all active processes in each sandbox process pool, clearing remaining requests in the first-in-first-out queue, and releasing related resources. This ensures data consistency and system security, preventing task loss or resource leakage due to forced interruption.
[0051] The technical solutions provided by the above embodiments bring at least the following beneficial effects. The code execution method provided in this application firstly creates and initializes sandbox process pools corresponding to the runtime environments of different programming languages according to predefined process parameters during the sandbox system startup phase, and performs key process warm-up operations during the initialization phase of the sandbox system, so that the sandbox system has fully ready and immediately executable code process resources before formally processing requests; then, after each code execution request is received, an idle and pre-warmed target sandbox process is allocated from the corresponding sandbox process pool according to the target programming language type carried in the code execution request, which can allocate fully ready and immediately executable code process resources for massive requests. The system first obtains the source code, thereby improving the response speed of massive requests. Then, the code to be executed, carried in the code execution request, is encrypted and transmitted to the target sandbox process. Within the isolated environment of the target sandbox process, decryption, code execution, and asynchronous return of execution results are performed. This effectively ensures data security during code transmission through encrypted transmission and significantly improves the concurrency performance of the sandbox system through asynchronous return of execution results. Finally, before returning the target sandbox process, a health check is performed, and based on the health check results, the target sandbox process is either destroyed or recycled to the corresponding sandbox process pool for reuse. This health check-based process destruction or recycling ensures the long-term stability of the sandbox system under high-concurrency scenarios. Furthermore, maintaining a controlled sandbox process pool for each programming language's runtime environment and implementing unified dynamic allocation, destruction, and recycling management strategies allows limited process resources to be reused cyclically in high-concurrency scenarios, completely avoiding the huge overhead of creating and destroying processes individually for each request.
[0052] The code execution method of this application embodiment is described below with reference to a specific example. The specific implementation process of this method is as follows: Figure 3 As shown.
[0053] S301. Initialize the global process pool manager; S302, Startup process warm-up mechanism; S303, Process pool is ready; S304, Receive code execution request; S305. Determine if there are any idle processes in the process pool corresponding to the target language. If yes, proceed to step S309; otherwise, proceed to step S306. S306, Request to join the FIFO waiting queue; S307. Determine whether a timeout has occurred or the request has been cancelled. If yes, proceed to step S321; otherwise, proceed to step S308. S308. After an available process is allocated, continue execution and proceed to step S310. S309. Immediately allocate an idle process and proceed to step S310; S310. Encrypt the code to be executed using a random key XOR encryption. S311, Secure encapsulation is achieved by combining Base64 encoding; S312. Transmit the encrypted code to the target process; S313, asynchronous execution of code by a process; S314. Task completed, process returned; S315. Health check: Operating status, number of uses, and survival time; S316. Determine whether the process exceeds the threshold or is abnormal. If yes, proceed to step S317; otherwise, proceed to step S320. S317, marked as requiring recycling, no longer to be reused; S318, Trigger adaptive supplementation of new processes; S319. New process created successfully, added to process pool, proceed to step S303; S320: Reset the state, re-enter the process pool, and end the process; S321. Discard the request, cancel the response after timeout, and end the process.
[0054] In this embodiment, the cold start delay caused by dynamically created processes in traditional sandbox systems is completely eliminated through process pool pre-start and pre-warming mechanisms; precise resource allocation in multi-language environments is achieved through an intelligent scheduling system, ensuring rapid response and fair processing of requests in high-concurrency scenarios; dual security mechanisms are used to add encoding protection on top of encrypted transmission, effectively preventing code leakage and tampering; asynchronous execution and queue management significantly improve system throughput; intelligent resource reclamation and dynamic replenishment are achieved by combining full lifecycle monitoring, reducing resource overhead while ensuring long-term stable system operation; finally, abnormal requests are automatically cleaned up through a timeout circuit breaker mechanism, building a highly available and high-concurrency code execution environment.
[0055] As can be seen, the above mainly describes the solutions provided by the embodiments of this application from a methodological perspective. To achieve the above functions, the embodiments of this application provide corresponding hardware structures and / or software modules for executing each function. Those skilled in the art should readily recognize that, in conjunction with the modules and algorithm steps of the various examples described in the embodiments disclosed herein, the embodiments of this application can be implemented in hardware or a combination of hardware and computer software. Whether a function is executed by hardware or by computer software driving hardware 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.
[0056] This application embodiment can divide the code execution device into functional modules according to the above method example. For example, each function can be divided into its own functional module, or two or more functions can be integrated into one processing module. The integrated module can be implemented in hardware or as a software functional module. Optionally, the module division in this application embodiment is illustrative and only represents one logical functional division; other division methods may be used in actual implementation.
[0057] In some embodiments, this application also provides a code execution apparatus. The code execution apparatus may include one or more functional modules for implementing the code execution method of the above method embodiments.
[0058] For example, Figure 4 This is a schematic diagram illustrating the composition of a code execution device provided in an embodiment of this application. Figure 4 As shown, the code execution device 400 includes: The preheating module 401 is used to create and initialize a sandbox process pool for each programming language runtime environment according to predefined process parameters during the startup phase of the sandbox system; and to preheat the sandbox process pool during the initialization phase of the sandbox system. The allocation module 402 is used to allocate an idle, preheated target sandbox process from the corresponding sandbox process pool according to the target programming language type carried in the code execution request after each code execution request is received. The execution module 403 is used to encrypt and transmit the code to be executed carried in the code execution request to the target sandbox process, and perform decryption, code execution and asynchronous return of execution results in the isolated environment of the target sandbox process; The inspection module 404 is used to perform a health check on the target sandbox process before returning it, and to determine whether to destroy or recycle the target sandbox process to the corresponding sandbox process pool for reuse based on the health check results.
[0059] In some embodiments, the code execution device 400 further includes a timeout cleanup module, specifically used for: Upon receiving a code execution request, if there are no idle processes in the sandbox process pool, the code execution request is added to a first-in-first-out queue to wait. If the waiting time for a code execution request in the first-in-first-out queue exceeds the preset timeout threshold, invalid requests in the first-in-first-out queue will be cleaned up.
[0060] In some embodiments, the execution module 403 includes: The encryption unit is used to encrypt the code to be executed carried in the code execution request with a random key to obtain binary data; Encoding unit, used to encode binary data into secure code packets that conform to text transmission standards; The transmission unit is used to transmit security code packets to the target sandbox process via the standard input channel.
[0061] In some embodiments, the execution module 403 further includes: The decoding unit is used to decrypt the security code package within the isolated environment of the target sandbox process and within a child process to obtain the code to be executed. An execution unit is used to perform execution operations on the code to be executed and obtain the execution result. The feedback unit is used to asynchronously feedback the execution results through an independent output channel; The notification unit is used to notify the execution termination status via the completion channel.
[0062] In some embodiments, the inspection module 404 is specifically used for: If the health check results show that the number of times the target sandbox process is used exceeds the preset maximum number of uses threshold or the maximum survival time of the target sandbox process exceeds the preset maximum survival time threshold, then the target sandbox process will be destroyed. Otherwise, the target sandbox process will be recycled to the corresponding sandbox process pool for reuse.
[0063] In some embodiments, the code execution device 400 further includes: The monitoring module is used to monitor the number of sandbox processes in the sandbox process pool; A module is created to generate new sandbox processes if the number of sandbox processes is less than the preset minimum number of processes, so that the number of sandbox processes in the sandbox process pool reaches the preset minimum number of processes.
[0064] In some embodiments, the code execution device 400 further includes: The retry module is used to recreate the process if the process creation fails; the number of times the process creation is recreated shall not exceed the preset maximum number of retries.
[0065] In some embodiments, the code execution device 400 further includes: The exit module is used to execute the exit process when the sandbox system is shut down or terminated abnormally, to terminate all active processes in each sandbox process pool in an orderly manner, and to clean up the remaining requests in the first-in-first-out queue and release related resources.
[0066] When implementing the functions of the integrated modules described above in hardware, this embodiment of the invention provides a possible schematic diagram of the electronic device involved in the above embodiments. For example... Figure 5As shown, the electronic device 500 includes: a processor 502, a communication interface 503, and a bus 504. Optionally, the electronic device 500 may also include a memory 501.
[0067] Processor 502 may implement or execute various exemplary logic blocks, modules, and circuits described in conjunction with the disclosure of this application. Processor 502 may be a central processing unit, a general-purpose processor, a digital signal processor, an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, transistor logic devices, hardware components, or any combination thereof. It may implement or execute various exemplary logic blocks, modules, and circuits described in conjunction with the disclosure of this application. Processor 502 may also be a combination that implements computing functions, such as including one or more microprocessor combinations, a combination of a DSP and a microprocessor, etc.
[0068] Communication interface 503 is used to connect to other devices via a communication network. This communication network can be Ethernet, wireless access network, wireless local area network (WLAN), etc.
[0069] The memory 501 may be a read-only memory (ROM) or other type of static storage device capable of storing static information and instructions, random access memory (RAM) or other type of dynamic storage device capable of storing information and instructions, or electrically erasable programmable read-only memory (EEPROM), disk storage medium or other magnetic storage device, or any other medium capable of carrying or storing desired program code in the form of instructions or data structures and accessible by a computer, but is not limited thereto.
[0070] In one possible implementation, the memory 501 can exist independently of the processor 502. The memory 501 can be connected to the processor 502 via a bus 504 and is used to store instructions or program code. When the processor 502 calls and executes the instructions or program code stored in the memory 501, it can implement the code execution method provided in this embodiment of the invention.
[0071] In another possible implementation, the memory 501 can also be integrated with the processor 502.
[0072] Bus 504 can be an extended industry standard architecture (EISA) bus, etc. Bus 504 can be divided into address bus, data bus, control bus, etc. For ease of representation, Figure 5 The bus is represented by a single thick line, but this does not mean that there is only one bus or one type of bus.
[0073] Through the above description of the implementation methods, those skilled in the art can clearly understand that, for the sake of convenience and brevity, only the division of the above functional modules is used as an example. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the service calling device can be divided into different functional modules to complete all or part of the functions described above.
[0074] This application also provides a computer-readable storage medium. All or part of the processes in the above method embodiments can be executed by computer instructions instructing related hardware. The program can be stored in the aforementioned computer-readable storage medium, and when executed, it can include the processes of the above method embodiments. The computer-readable storage medium can be any of the foregoing embodiments or memory. The aforementioned computer-readable storage medium can also be an external storage device of the aforementioned service invocation device, such as a plug-in hard drive, smart media card (SMC), secure digital (SD) card, flash card, etc., equipped on the aforementioned service invocation device. Further, the aforementioned computer-readable storage medium can include both internal storage units of the aforementioned service invocation device and external storage devices. The aforementioned computer-readable storage medium is used to store the aforementioned computer program and other programs and data required by the aforementioned service invocation device. The aforementioned computer-readable storage medium can also be used to temporarily store data that has been output or will be output.
[0075] This application also provides a computer program product comprising a computer program that, when run on a computer, causes the computer to execute any of the code execution methods provided in the above embodiments.
[0076] The above are merely specific embodiments of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A code execution method, characterized in that, Applied to sandbox systems, the method includes: During the startup phase of the sandbox system, a sandbox process pool is created and initialized for each programming language runtime environment according to predefined process parameters; and during the initialization phase of the sandbox system, the sandbox process pool is preheated. Upon receiving a code execution request, an idle, pre-warmed target sandbox process is allocated from the corresponding sandbox process pool based on the target programming language type carried in the code execution request. The code to be executed carried in the code execution request is encrypted and transmitted to the target sandbox process. Decryption, code execution, and execution result are asynchronously returned within the isolated environment of the target sandbox process. Before returning the target sandbox process, a health check is performed on the target sandbox process, and based on the health check results, it is determined whether the target sandbox process will be destroyed or recycled to the corresponding sandbox process pool for reuse.
2. The code execution method according to claim 1, characterized in that, After each code execution request is received, the method further includes: If there are no idle processes in the sandbox process pool, the code execution request is added to a first-in-first-out queue for waiting. If the waiting time for the code execution request in the first-in-first-out queue exceeds a preset timeout threshold, then invalid requests in the first-in-first-out queue will be cleaned up.
3. The code execution method according to claim 1, characterized in that, The step of encrypting and transmitting the executable code carried in the code execution request to the target sandbox process includes: The code to be executed carried in the code execution request is encrypted with a random key to obtain binary data; The binary data is encoded into a secure code packet conforming to text transmission standards; The security code packet is transmitted to the target sandbox process via the standard input channel.
4. The code execution method according to claim 3, characterized in that, The process of decryption, code execution, and asynchronous return of execution results within the isolated environment of the target sandbox process includes: Within the isolated environment of the target sandbox process, the secure code package is decrypted in the child process to obtain the executable code, and the executable code is executed to obtain the execution result. The execution results are asynchronously returned through an independent output channel, and the execution termination status is notified through a completion channel.
5. The code execution method according to claim 1, characterized in that, The step of determining whether to destroy or recycle the target sandbox process to the corresponding sandbox process pool for reuse based on the health check results includes: If the health check result indicates that the number of times the target sandbox process is used exceeds a preset maximum usage threshold or the maximum survival time of the target sandbox process exceeds a preset maximum survival time threshold, then the target sandbox process is destroyed. Otherwise, the target sandbox process will be recycled to the corresponding sandbox process pool for reuse.
6. The code execution method according to claim 5, characterized in that, The method further includes: Monitor the number of sandbox processes in the sandbox process pool. If the number of sandbox processes is less than the preset minimum number of processes, create a new sandbox process so that the number of sandbox processes in the sandbox process pool reaches the preset minimum number of processes.
7. The code execution method according to claim 6, characterized in that, The method further includes: If process creation fails, the process will be recreated; however, the number of re-creation attempts will not exceed the preset maximum number of retries.
8. The code execution method according to claim 2, characterized in that, The method further includes: When the sandbox system is shut down or terminated abnormally, an exit process is executed to terminate all active processes in each sandbox process pool in an orderly manner, and to clear the remaining requests in the first-in-first-out queue and release related resources.
9. A code execution device, characterized in that, The device, used in sandbox systems, includes: The preheating module is used to create and initialize a sandbox process pool for each programming language runtime environment according to predefined process parameters during the startup phase of the sandbox system; and to preheat the sandbox process pool during the initialization phase of the sandbox system. The allocation module is used to allocate an idle, preheated target sandbox process from the corresponding sandbox process pool according to the target programming language type carried in the code execution request after each code execution request is received. The execution module is used to encrypt and transmit the code to be executed carried in the code execution request to the target sandbox process, and perform decryption, code execution and asynchronous return of execution results in the isolated environment of the target sandbox process; The inspection module is used to perform a health check on the target sandbox process before returning it, and to determine whether to destroy or recycle the target sandbox process to the corresponding sandbox process pool for reuse based on the health check results.
10. An electronic device, characterized in that, include: Processor and memory; The memory stores instructions executable by the processor; when the processor is configured to execute instructions, the electronic device implements the code execution method as described in any one of claims 1-8.
11. A computer-readable storage medium, characterized in that, include: Computer software instructions; when the computer software instructions are executed in an electronic device, the electronic device causes the electronic device to implement the code execution method as described in any one of claims 1-8.
Citation Information
Cited By
A sandbox snapshot recovery collaboration method, system, device and medium
CN122527079A
