SoFunction
Updated on 2024-11-20

Python Tutorial on Processes and Threads

Difference and connection between processes and threads

Finally starting to deepen the difficulty, coming to the point of knowledge of processes and threads~

These two concepts alone have been difficult for many beginners - learning the concepts today and forgetting them tomorrow; learning the examples tomorrow and forgetting the concepts.

To understand the connection and difference between processes and threads, let me give you a particularly simple example:

Your computer has two browsers, a Google Chrome and a qq browser.

A browser is a process.

Then, you opened Google Chrome, Baidu searched for Test Chitam, and opened a new tab to open Uncle Tam's article, as shown below:

image-20210404220106555

You can read it like this--Two web pages opened in the same browser are two threads. If I close the browser, both threads are gone as well.

Well, when you have the concept, then I can talk about the difference and connection between the two.

step

  • vantage
    • High stabilityWhen the program crashes, it does not affect the use of other processes. That is, after Google Chrome crashes, qq browser can still be used normally.
  • drawbacks
    • Costly to create processes.. I.e., when you have a variety of browsers open, your computer gets particularly laggy because there is an upper limit on computer memory and CPU.

threading

  • vantage
    • Multi-threading is usually a little faster than multi-processing, but the advantage is small
  • drawbacks
    • Any thread that hangs will cause the entire process to crash because threads share the process memory (the browser example no longer applies and can be understood as grasshoppers tied to a boat)

multiprocess

Since most of you guys use Windows OS, the fork() call for Unix/Linux is left aside.

In python, multiprocessing is generally implemented using multiprocessing.

import os
from multiprocessing import Process
def run\_proc(name):
    print('Starting the execution of subprocesses %s (%s)…' % (name, ()))
# Code to be executed by the child process
if __name__ == '\_\_main\_\_':
    print('Parent process %s.' % ())
    p = Process(target=run_proc, args=('test',)) # Create a Process instance
    print('The subprocess is about to start...')
    ()  # Start the instance with the start() method
    ()   # The join() method waits for a child process to finish before moving on, and is often used for inter-process synchronization.
    print('End of child process...')

Want to go into details?

In fact, my notes are so fully written that you only need to COPY the code down and execute it once, or DEBUG it once, to understand it.

thread pool

How is it understood?

That is, the number of processes the code can run, there is a place to control it.

from multiprocessing import Pool
import os
import time
import random
def long\_time\_task(name):
    print('commencement task %s (%s)...' % (name, ()))
    start = ()
    (() * 3)
    end = ()
    print('Task %s execution took %0.2f seconds.' % (name, (end - start)))
if __name__ == '\_\_main\_\_':
    print('Father process %s.' % ())
    p = Pool(3)
    # Because the default size of the Pool is 4, so task 0, 1, 2, 3 is immediately executed, while task 4 to wait for the completion of a task before the execution of the execution, but the maximum number of simultaneous execution of four processes
    # Since the default size of the Pool is the number of cores in the CPU, if you have an 8-core CPU, you have to submit at least 9 child processes to see the waiting effect
    for i in range(5):
        p.apply_async(long_time_task, args=(i,))
    print('Waiting for the child process to finish...')
    ()
    # Calling join() on a Pool object waits for all child processes to finish executing.
    # You must call close() before you call join(), and you can't continue to add new Processes after you call close()
    ()
    print('All child processes have been executed...')

Above all, it is a must know.

Of course, the best time to learn is when you are going to use it: when you are going to use it, then come back to review and learn it for the best results.

If you learn it and don't have a use case, I'd recommend holding off on learning it or using it as a knowledge base.

multi-threaded

There are three things about multithreading that must be made clear in advance:

  • Multitasking requirements can be accomplished by multiple processes or by multiple threads within a single process
  • A process is composed of several threads
  • A process has at least one thread

Python's standard library provides two modules: _thread and threading. _thread is the low-level module, and we generally use the high-level module threading (which encapsulates _thread).

Starting a thread is a matter of passing in a function and creating a Thread instance, then calling start() to begin execution, let's look at a simple example:

import time
import threading
# Code to be executed in the new thread
def loop():
    # Since any process starts a thread by default, we call that thread the main thread, which in turn can start new threads.
    # Python's threading module has a current\_thread() function, which always returns the current instance of the thread
    print('Thread ss %s running...' % threading.current_thread().name)
    n = 0
    # The name of the main thread instance is MainThread, the name of the subthread is specified at creation time, we use LoopThread to name the subthread, and display the name in the printout
    while n < 5:
    n = n + 1
    print('Thread ss %s >>> %s' % (threading.current_thread().name, n))
    (1)
print('Thread ss %s has ended.' % threading.current_thread().name)
print('Thread %s is running...' % threading.current_thread().name)
t = (target=loop, name='LoopThread')
()
()
'''
Another important aspect of the join() method is that it doesn't really need to be called at all.
Once threads are started, they keep executing until the given function finishes and exits.
If the main thread has other things to do than wait for these threads to finish (such as other processing or waiting for a new client request), it is possible to not call join().
you can leave join() alone. join() methods are only useful if you need to wait for threads to complete.
'''
print('Thread %s has ended.' % threading.current_thread().name)

Again, the above, is a must know.

And, working scenarios, we will generally use multiple threads to handle problems, not multiple processes. (Note: it's general)

As for the knowledge of inter-process communication, thread locks, GIL locks, multi-threaded variables, inter-thread communication, and asynchronous co-processing, it's a bit more complicated to talk about and is not the core of the minimalist tutorials, so you can either learn it on your own first, or read it and learn it when you're actually going to use it.

summarize

01 Multi-threading and multi-processing are a must-know;

02 Study at your own pace, neither by dead reckoning nor by simply skipping;

03 Small partners are more concerned about whether they will be asked during the interview. A: The general company will not, so?

That's all for this post, I hope it was helpful and I hope you'll check back for more from me!