In Python, a task concurrency is implemented through a process branch. In Linux, the process branch is implemented through the fork () method.
A new child process is created after the 1.fork () call, which is a copy of the original parent process. A subprocess can run independently of the parent process.
2.fork () is a very special method, called once, returns two times.
3.fork () It returns 2 values, a value of 0, that is returned in the subprocess, 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 cannot be run under Windows.
Process Branch Fork ()
Examples are as follows:
Copy Code code as follows:
#!/usr/bin/python
#coding =utf-8
Import OS
def child ():
Print (' Hello from Child ', Os.getpid ())
Os._exit (0)
def parent ():
PID = Os.fork ()
If PID = = 0:
Child ()
print ' fork child process error! ' #如果打印该字符串, indicating an error invoking child ()
Else
Print (' Hello from parent ', Os.getpid (), PID)
Parent ()
The results of the operation are as follows:
Copy Code code as follows:
(' Hello from parent ', 29888, 29889)
(' Hello from Child ', 29889)
It is not difficult to see from the results that the print character after the child () has not been printed, indicating that the call child () is not returned.
The combination of fork and exec
Judging from the example above, when the child () method is invoked, it exits directly. But in practical applications, we want the branching process to run independently of another new program. The Exec method is used to replace the child process, and the PID of the process is not changed. The Exec method does not return.
First of all, explain Exec's 8 method groups:
OS.EXECV (program, Cmdargs)
The basic "V" form of execution requires passing in the executable name of the program and the list or tuple of command-line parameter characters used to run the program.
OS.EXECL (program, CMDARG1, Cmdarg2, ..., cmdargn)
The basic "L" form of execution requires passing in an executable program name, as well as multiple character arguments for the command line that is used to run the program.
OS.EXECVP (program, args)
In "P" mode, the basic "V" execution requires passing in an executable program name and a list or tuple of command-line parameter characters used to run the program. The search path for running the new program is the search path for the current file.
OS.EXECLP (program, CMDARG1, Cmdarg2, ..., cmdargn)
In "P" mode, the basic "l" form of execution requires passing in the executable program name, as well as multiple character parameters for the command line running 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 takes the form of an executable program name and a list or tuple of command-line parameter characters used to run the program. Finally, pass in the required environment variable env dictionary parameters for running the new program.
Os.execle (program, CMDARG1, Cmdarg2, ..., CMDARGN, env)
In "E" mode, the basic "l" form of execution requires passing in the executable program name, as well as multiple character arguments for the command line that is used to run the program. Finally, pass in the required environment variable env dictionary parameters for running the new program.
OS.EXECVPE (program, args, env)
In the combination mode of "P" and "E", the basic "V" execution form, you need to pass in the executable name of the program and the list or tuple of the command-line parameter characters used to run the program. Finally, you want to pass in the environment variable env dictionary parameters needed to run the new program. The search path to run the new program is the search path for the current file.
Os.execlpe (program, CMDARG1, Cmdarg2, ..., CMDARGN, env)
In the combination mode of "P" and "E", the basic "l" form of execution requires passing in the executable program name, as well as multiple character arguments for the command line that is used to run the program. Finally, you want to pass in the environment variable env dictionary parameter that runs the new program. The search path to run the new program is the current file's search path.
The newprocess.py code is as follows:
Copy Code code as follows:
#!/usr/bin/python
#coding =utf-8
Import OS
def child ():
Print (' Hello from Child ', Os.getpid ())
Os._exit (0)
Child ()
The main code is as follows:
Copy Code code as follows:
#!/usr/bin/python
#coding =utf-8
Import OS
def child ():
Print (' Hello from Child ', Os.getpid ())
Os._exit (0)
def parent ():
PID = Os.fork ()
If PID = = 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:
Copy Code code as follows:
$ python testfork.py
(' Hello from parent ', 30791, 30792)
$ (' Hello from Child ', 30792)