“This is the 25th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

Official Python column # 74, stop! Don’t miss this zero-based article!

In previous articles, we looked at submitting a task using ThreadPoolExecutor’s Submit method, then retrieving the Future object and parsing its state.

We’ll cover process pools later, but before we share process pools, let’s look at processes and child processes.

Process concept

This article is intended as a supplement, so the committee will not reconceptualize the entire concept of process. Here is an excerpt from the encyclopedia:

Process is a running activity of a program on a data set in a computer. It is the basic unit of resource allocation and scheduling in the system and the basis of operating system structure. In the early process-oriented computer architecture, the process is the basic execution entity of the program. In modern thread-oriented computer architectures, processes are containers for threads. A program is a description of instructions, data and their organizational form, while a process is an entity of the program.

Baike.baidu.com/item/%E8%BF…

So what exactly is a process?

The sensible way to think about it is that when we run a Python program, we have a process running.

You can then obtain process information using the following code:

Pid = os.getpid(

This process can be found using a system tool such as Photoshop (or using taskList on Windows).

The following screenshot shows the process ID (PID) when the committee runs a Python program using the ps command:

Here is the sample code shown for the above process:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2022/2/26 12:10 am
# @Author : LeiXueWei
# @csDN /Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : process.py
# @Project : DeepDivePython

import os
import time
from datetime import datetime
pid = os.getpid() 
i = 0
while i < 5:
    i += 1
    time.sleep(3)
    print("Current process ID:", pid, ', execution time ', datetime.now())
Copy the code

The above code prints the process ID every three seconds, repeating five times.

The committee has previously shared threads at length.

What is the relationship between threads and processes?

Those of you who have studied operating systems know this, so you can recall it again.

The following code presentation has been prepared by the committee:

#! /usr/bin/env python # -*- coding: utf-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: Process. DeepDivePython import os import threading import time from datetime import datetime pid = os.getpid() i = 0 for i in range(3): def demo(slogan): Time.sleep (4) print(" I am a Thread in a process of the slogan:", slogan) threading.thread (target=demo, kwargs={"slogan": }).start() I =0 while I < 3: I += 1 time.sleep(3) print(" ID:", pid, ', execute time ', datetime.now())Copy the code

A process is a container in which threads run.

If a thread is an active individual, a student, then a process is like a class.

Different processes are independent of each other, just as different classes maintain boundaries with each other.

Of course, threads and processes are not strictly related to classes of students, but are used figuratively to describe their paired relationships.

Here is the result:

We showed earlier that every program has a main thread, and then that main thread can create other threads.

Is there a primary or secondary process, and is there a similar way to create other processes?

Yes. But we usually talk about parent and child processes.

Creation of a child process

The OS built-in library gives us a fork method:

Import OS pid = os.fork() # fork a child process from the parent, pid = ID of the child processCopy the code

The return value of the fork method is divided into two cases:

  • Returns the PID of the child if called by the parent process

  • If it is a child process call, 0 is returned.

See a demo below to make it clear:

#! /usr/bin/env python # -*- coding: utf-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: Process1.py # @Project: DeepDivePython import OS from datetime import datetime pid = os.fork() print(" ID:", pid, ', execution time ', Datetime.now ()) if pid > 0: print(" parent pid :", os.getpid()) print(" parent pid :", pid) else: Print (" parent PID:", os.getpid()) print(" parent PID:", os.getppId ())Copy the code

The running results are as follows:

conclusion

The OS library in Python provides some convenient ways to get process information easily.

There’s more to learn about process than this article. However, for beginners, it is important to learn the code and concepts described in this article and understand the relationship and difference between threads and processes.

Readers interested in processes can browse the operating system for a more comprehensive knowledge and a solid foundation.

We’ll continue sharing process pools in the next article.

For those who like Python, please check out the Python Basics section or the Python Getting Started to Master Section

Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to wechat, like support collection!