“This is the 18th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

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

The previous article wrote a lot, are more basic.

In this article we’ll start with intermediate programming. Deal with more complicated things. For the thread of this article, let’s start with the basics!

What is a thread?

We know that the workers are all working in the factory at the same time, copying each other’s work. They are units that operate independently!

Threads are similar to a single running unit, multithreading is multiple independent running units doing the same thing at the same time.

It’s a simple way of thinking about it, and we’ll compare it later.

Threads have statuses, and it’s a good metaphor for a worker’s day, going to work in the morning, then working, sometimes stopping to rest, and finally leaving.

Copy and run the following code to see:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2021/11/21 12:02 am
# @Author : LeiXueWei
# @csDN /Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : __init__.py.py
# @Project : hello

import threading

mythread = threading.Thread()
print("mythread:", mythread)
print("is_alive:", mythread.is_alive())
mythread.start()
print("mythread:", mythread)
print("is_alive:", mythread.is_alive())
Copy the code

Here is the result:

Readers are advised to run it first.

Again, thread code

Above we used the threading library and then created an object instance of the Thread class and assigned it to the myThread variable.

It then prints whether the object and thread object’s one function, is_Alive (), is active.

False both times.

But the second time we see the thread object printed as’ stopped’.

The thread is in the stopped state after we have run the start function.

The top one is the thread, but it doesn’t seem to be doing anything, so let’s let it do something.

The thread triggers the business function and the thread calls the business function

For example, this business is: follow and like.

def dianzan_guanzhu() :
    now = datetime.datetime.now() Initialize the time variable
    name = "Python of new"
    print("%s name:%s" % (now, name)) # First print time and fan name
    time.sleep(1)
    result = "Great! + name + "Pay attention to thunder committee, white whoring a lot of knowledge and development experience!"
    print("%s result:%s" % (now, result)) # Second print time and fan activity
    return result
Copy the code

Let’s look at the following code:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2021/11/21 12:02 am
# @Author : LeiXueWei
# @csDN /Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : __init__.py.py
# @Project : hello

import threading
import datetime
import time

The student committee defines a function of concern.
def dianzan_guanzhu() :
    now = datetime.datetime.now()
    name = "Python of new"
    print("%s name:%s" % (now, name))
    time.sleep(1)
    result = "Great! + name + "Pay attention to thunder committee, white whoring a lot of knowledge and development experience!"
    print("%s result:%s" % (now, result))
    return result


mythread = threading.Thread(target=dianzan_guanzhu)
print("mythread:", mythread)
print("is_alive:", mythread.is_alive())
mythread.start()
print("mythread:", mythread)
print("is_alive:", mythread.is_alive())
dianzan_guanzhu()
print("is_alive:", mythread.is_alive())
Copy the code

Run the copy directly, here our dianzan_guanzhu function is called twice

The first is the mythread.start function.

The second time we call the dianzan_guanzhu function directly off thread.

Here is the result:

It doesn’t seem like anything.

Look at it again, notice the time of each print, the time of the input seems to be out of order, right? That’s right. It’s not dazzling. It’s running right.

Since the now variable is initialized after entering the Dianzan_guanzhu function, the time is fixed.

But the dianzan_guanzhu function is also called outside the thread, so here it is: two threads doing the same thing at the same time.

Which one is the extra thread?

As a side note, when we write Python scripts, we run the code in a main thread.

I just haven’t been able to dismiss the idea of threads, haven’t written multithreaded programs, haven’t been aware of it.

From now on, you need to be clear: every program runs a main thread.

Returning to the result, the two threads call through the function one after the other:

First, print the first line of output in sequence. Separate sleep for one second (sleep(1)). Finally, print the second line of output in sequence.

conclusion

Let’s start with the basics of threads. I’ll share more about threads in the next section.

By the way, if you like Python, please check out the Committee’s Python Basics column or Python Getting Started to Master column

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!