Os.getpid in Python ()
Os.getpid () is the process number that gets the current process, and os.getppid () is the process number that gets the parent process of the current process
The role of __name__ = ' __main__ ' in Python
eg
def main ():print "We is in%s"%__name__if __name__ = = ' __main__ ':
Main ()
This function defines a main function, and we execute the PY file to find out that the result is a print "We is in __main__", stating that the contents of our if statement are executed, called main ()
If we import another module into the module
From module Import mainMain ()
The result of its execution is: we is in module
However, "We is in __main__" is not shown, which means that the function under module __name__ = ' __main__ ' is not executed.
This allows the "module" file to be run or introduced by other modules without executing the function 2 times
It means that the script module that you write can be imported into other modules, and the module can be executed by itself.
If we are directly executing a. py file, the file then "__name__ = = ' __main__ '" is true, but if we import the file through import from another. py file, then __name__ The value is the name of our py file, not the __main__.
This feature also has a use: When debugging code, in "if __name__ = = ' __main__ '" to add some of our debugging code, we can let the external module calls do not execute our debugging code, but if we want to troubleshoot the problem, directly execute the module file, Debug your code to work!
Pool
Process Pool
. Apply_async () The total number of processes that are executing is processes, and a new process is added when a process finishes executing.
. Close ()
. Join () calls the close function before calling join, otherwise an error occurs. No new processes are added to the Pool,join function to wait for all child processes to end after the close is done
Treadlocal
When we use threads, each thread uses its own local variables, but if we call a function inside the thread and we need to let the function handle our variables, we have a problem. The local variable must be passed into the function. If a large number of local variables will greatly increase the complexity of the function parameters.
One approach is to use the global dict and then use the thread itself as the key to get the corresponding local variable. This is possible, but the code may not look good, and the string of code obtained is too long. A better approach is to use threadlocal, which will help you do it all. Simplified code
This article from "Big Plum" blog, reproduced please contact the author!
Python learning process and threading problems and problems