“This is the 19th 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!

In front of the article to write the basic knowledge of threads, this time we look at multithreading!

What multithreading?

Multithreading, that is, multiple independent running units doing the same thing at the same time.

Let’s go back to the code we shared earlier: likes and likes.

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 the thunder committee, learned to develop knowledge!"
    print("%s result:%s" % (now, result))
    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


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 the thunder committee, learned to develop knowledge!"
    print("%s result:%s" % (now, result))
    return result


for i in range(5):
    mythread = threading.Thread(name="t-" + str(i), target=dianzan_guanzhu)
    print("mythread:", mythread)
    print("is_alive:", mythread.is_alive())
    mythread.start()
    print("is_alive:", mythread.is_alive())
Copy the code

The Thread class can pass in name to specify the Thread name.

Run the copy directly. Here we create 5 threads.

They, in turn, call the dianzan_guanzhu function

Here is the result:

The five threads were printed at different times, but the content was interspersed with confusion.

Gets the number of active threads

The threading.active_count function retrieves the number of active threads.

As mentioned earlier, plus the main thread, there are six threads.

Run the following code to see:

#! /usr/bin/env python # -*- coding: utF-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: __init__.py. hello import threading import datetime import time def dianzan_guanzhu(): Now = datetime.datetime.now() name = "python "print("%s name:%s" % (now, name)) time.sleep(1) result =" " + name + "pay attention to the thunder committee, learned to develop knowledge!" print("%s result:%s" % (now, result)) return result for i in range(5): mythread = threading.Thread(name="t-" + str(i), target=dianzan_guanzhu) print("mythread:", mythread) print("is_alive:", Thread.is_alive ()) mythread.start() ac = threading.active_count() print("active_count:", ac)Copy the code

If we print the number of active threads, then all five threads will be called by start.

Up to 6 active threads.

Today we’ll show you how to create a task with multiple threads.

Tomorrow we will share the details of multi-threaded coordination.

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!