In Python, a task is implemented in a single way through a process branch. On Linux systems, the fork () method is used to implement the process branch.
- The fork () call creates a new child process that is a copy of the original parent process. The child process can run out of the parent process independently.
- Fork () is a very special method, one call, two returns.
- Fork () It returns 2 values, a value of 0, indicating that the child process is returned, and a value other than 0, which indicates that the child process ID is returned in the parent process.
The following can only be run in Linux and not under Windows.
Process Branch Fork ()
Examples are as follows:
#!/usr/bin/python#coding =utf-8ImportOs def Child():Print' Hello from child ', Os.getpid ()) Os._exit (0) def parent():PID = Os.fork ()ifPID = =0: Child ()Print ' fork child process error! '#如果打印该字符串, error calling child () Else: Print (' Hello from parent ', Os.getpid (), PID) parent ()
The results of the operation are as follows:
(‘hello from parent‘, 29888, 29889)(‘hello from child‘, 29889)
From the result it is not difficult to see that the print character after the child () is not printed, stating that the call to child () is not returned.
Combination of fork and exec
From the above example, the child () method is called and then exited directly. But in practical applications, we want the branching subprocess to be able to run another new program independently. The Exec method is required to replace the child process, and the PID of the replacement process does not change. The Exec method does not return.
Let's start by explaining the 8 group of Exec-related methods:
OS.EXECV (program, Cmdargs)
The basic "V" execution form requires passing in the executable program name, as well as a list or tuple that is used to run the program's command-line parameter numbers.
OS.EXECL (program, CMDARG1, Cmdarg2, ..., cmdargn)
The basic "L" execution form requires passing in the executable program name, as well as the command line multiple character arguments used to run the program.
OS.EXECVP (program, args)
In "P" mode, the basic "V" execution form requires passing in the executable program name, as well as a list or tuple that is used to run the program's command-line parameter numbers. The search path to run the new program is the search path for the current file.
OS.EXECLP (program, CMDARG1, Cmdarg2, ..., cmdargn)
In "P" mode, the basic "l" execution form requires passing in the executable program name, as well as the command line multiple character arguments used to run the program. The search path to run the new program is the search path for the current file.
Os.execve (program, args, env)
In "E" mode, the basic "V" execution form requires passing in the executable program name, as well as a list or tuple that is used to run the program's command-line parameter numbers. Finally, the environment variable that runs the new program is passed to the env dictionary parameter.
Os.execle (program, CMDARG1, Cmdarg2, ..., CMDARGN, env)
In "E" mode, the basic "l" execution form requires passing in the executable program name, as well as the command line multiple character arguments used to run the program. Finally, the environment variable that is required to run the new program is passed the env dictionary parameter.
OS.EXECVPE (program, args, env)
In the combined mode of "P" and "V", the basic "V" execution form requires passing in the executable program name and a list or tuple that is used to run the program's command-line parameter numbers. Finally, the environment variable that runs the new program is passed the env dictionary parameter. The search path to run the new program is the current file's search path.
Os.execlpe (program, CMDARG1, Cmdarg2, ..., CMDARGN, env)
In the combination mode of "P" and "V", the basic "l" execution form requires passing in the executable program name, as well as the command line multiple character arguments used to run the program. Finally, the environment variable that runs the new program is passed to the env dictionary parameter. The search path to run the new program is the search path for the current file.
The newprocess.py code is as follows:
#!/usr/bin/python#coding=utf-8import osdef child(): print(‘hello from child‘, os.getpid()) os._exit(0)child()
The main code is as follows:
#!/usr/bin/python#coding =utf-8ImportOs def Child():Print' Hello from child ', Os.getpid ()) Os._exit (0) def parent():PID = Os.fork ()ifPID = =0: OS.EXECLP (' python ',' python ',' newprocess.py ')assert False,' fork child process error! ' Else: Print (' Hello from parent ', Os.getpid (), PID) parent ()
The output is as follows:
$ TestFork.py (‘hello from parent‘3079130792)$ (‘hello from child‘30792)
Python process branching fork and exec detailed