directory

  • Introduction to the Python Process Process
  • The Python Process module
  • Introduction to the Python Process function
  • The Python Process is used by Process
  • 5.Python Process Tips
  • Six. Guess you like it

Recommended path for learning Python: Python Learning Directory >> Python Basics

Introduction to the Python Process Process

Process is a collection of resource management, including the invocation of various resources, memory management, network interface invocation; A Process can contain multiple subprocesses. When a Process is started, a thread is automatically created. The first thread in the Process is the main thread (Python __name__ == ‘__main__’).

The Python Process module

Python also provides the Process module for creating processes. To create a Process, you need to import the Process module. The syntax is as follows:

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python Process module. py @time :2021/05/06 07:37 @Motto: A thousand miles without a single step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! "" # import Process module from multiprocessing import Process" Target - represents the calling object, the task (function name) to be performed by the child process; Args - arguments to the function of the child process and are of type tuple; Kwargs - Arguments to the function of the child process, and the type is dict, such as kwargs = {' name ':Jack,' age ':18}; Name - Child process name; Return value: returns the process instance object; P = Process(group=None, target=None, name=None, args=(), kwargs={})Copy the code

Introduction to the Python Process function

The functions of Process are similar to those of the ** threading** thread.

  • 1. Start – Starts the process.
  • 2. Terminate – Forcibly terminates the process without any cleanup. If a child process is created before the process terminates, the child process becomes a zombie process after it is forcibly terminated. If the process also holds a lock, it will not be released, resulting in a deadlock.
  • Is_alive – Checks whether a process is alive, returns True if it is alive, False otherwise;
  • 4. Join ([timeout]) – The main thread waits for the child thread to terminate. Timeout indicates the optional timeout period. P.jin can only join processes started by start and cannot join processes started by run.
  • Daemon – The default value is False. If the value is True, it indicates that the process is a background daemon. The process terminates when its parent terminates. If the value is set to True, the process cannot create child processes. This attribute must be set before start.
  • 6. Name – Process name;
  • 7. Pid – The process ID, pid, defaults to None if the pid is obtained before the start function because the process has not been created.
  • 8. Exitcode — The process runs at None, if -n, it is terminated by signal N;
  • 9. Authkey – Process authentication, default is a random 32-character string generated by OS.urandom. The purpose of this key is to provide security for communication between underlying processes that are designed to involve network connections that can only succeed if they have the same authentication;

The Python Process is used by Process

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python Process module. py @time :2021/05/06 07:37 @Motto: A thousand miles without a single step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! """ from multiprocessing import Process def people_info(*args,**kwargs): print(args,kwargs) def main(): # information list list_info = [{" name ":" zhangsan ", "height" : "175 cm"}, {" name ":" lisi ", "height" : "155 cm"}, {" name ": "wangwu", "height": "195cm"}, {"name": "liqi", "height": "166cm"}, {"name": "wangba", "height": },] # create process for I in range(5): P = Process(target=people_info,args=(I,),kwargs=list_info[I]) If set to True, the primary process will wait for the child process to complete. # p.daemon = True # p.start() # pid If the pid is obtained before the start function, the default is None, Print (" process name = ",p.name) # print(" process name = ",p.name) # print(" process exitCode ",p.name) # print(" process name = ",p.name) # print(" process exitCode ",p.name = ",p.exitcode) if __name__ == "__main__": Main () "" output: Process PID = 2600 Process name = process-1 Process exitCode = None Process PID = 4372 Process name = Process-2 Process exitCode = None Process PID = 14124 Process name = process-3 Process exitCode = None Process Pid = 10920 Process name = process-4 Process exitCode = None Process PID = 4892 Process name = process-5 Process exitCode = None (0,) {'name': 'zhangsan', 'height': '175cm'} (1,) {'name': 'lisi', 'height': '155cm'} (2,) {'name': 'wangwu', 'height': '195cm'} (3,) {'name': 'liqi', 'height': '166cm'} (4,) {'name': 'wangba', 'height': '125cm'} '''Copy the code

5.Python Process Tips

The Python OS module encapsulates common system calls, including:

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python Process module. py @time :2021/05/06 07:37 @Motto: A thousand miles without a single step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! "" os.fork() creates a child process os.getpid() to obtain its own ID. Os.getppid () to obtain the ID of the parent processCopy the code

That’s it. I don’t know if you’ve noticed that Process creation is very similar to threading. So what’s the difference between a Python Process and a thread? For details on the difference between a Python Process and a thread, see the Python Process Process and thread threading

Six.Guess you like

  1. Python conditional derivations
  2. Python list derivations
  3. Python dictionary derivations
  4. Python function declarations and calls
  5. Python variable argument *argc/**kargcs
  6. Python anonymous function lambda
  7. Python return logic determines expressions
  8. Python string/list/tuple/dictionary conversions
  9. Python local and global variables
  10. The Python type function is different from the isinstance function
  11. Python is differs from ==
  12. Python mutable and immutable data types
  13. Shallow and deep copies of Python
  14. Read and write Python files
  15. Python exception Handling
  16. Python module import
  17. Python __name__ == ‘__main__’ explained in detail
  18. Python thread creation and parameter passing
  19. Python thread mutex Lock
  20. Python thread time Event
  21. The Python thread Condition variable Condition
  22. Python thread Timer Timer
  23. Python thread Semaphore
  24. Python thread Barrier object Barrier
  25. Python thread Queue Queue – FIFO
  26. Python thread queue LifoQueue – LIFO
  27. Python thread PriorityQueue PriorityQueue
  28. Python thread Pool ThreadPoolExecutor
  29. Python thread Pool ThreadPoolExecutor
  30. The Python Process module
  31. The Python Process Process is different from threading
  32. Python interprocess communication Queue/Pipe

Python Process Process module

This article is published by the blog – Ape Say Programming Ape Say programming!