Home  >  Article  >  Backend Development  >  What is the Python Threading module? Learn what a thread module is in 3 minutes

What is the Python Threading module? Learn what a thread module is in 3 minutes

Tomorin
TomorinOriginal
2018-08-16 17:37:191878browse

This article mainly talks aboutWhat is a thread:Thread is the smallest unit that the operating system can perform calculation scheduling. A process is contained within a process and is the actual unit of processing within a process. A thread is a collection of instructions.

A thread refers to a single sequential control flow in a process. Multiple threads can run concurrently in a process, and each thread performs different tasks in parallel.

Python provides support for threads through two standard libraries, thread and threading. thread provides low-level, primitive threads and a simple lock.

Other methods provided by the threading module:

1.threading.currentThread(): Returns the current thread variable.

2.threading.enumerate(): Returns a list containing running threads. Running refers to after the thread starts and before it ends, excluding threads before starting and after termination.

3.threading.activeCount(): Returns the number of running threads, which has the same result as len(threading.enumerate()).

In addition to usage methods, the thread module also provides the Thread class to handle threads. The Thread class provides the following methods:

1.run(): Method used to represent thread activity.

2.start(): Start thread activity.

3.join([time]): Wait until the thread terminates. This blocks the calling thread until the thread's join() method is called abort - exit normally or throw an unhandled exception - or an optional timeout occurs.

4.isAlive(): Returns whether the thread is active.

5.getName(): Returns the thread name.

6.setName(): Set the thread name.

Use the Threading module to create threads

Use the Threading module to create threads, inherit directly from threading.Thread, and then override the __init__ method and run method:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import threading
import time
exitFlag = 0
class myThread (threading.Thread): #继承父类threading.Thread
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self): #把要执行的代码写到run函数里面 线程在创建后会直接运行run函数 
print "Starting " + self.name
print_time(self.name, self.counter, 5)
print "Exiting " + self.name
def print_time(threadName, delay, counter):
while counter:
if exitFlag:
(threading.Thread).exit()
time.sleep(delay)
print "%s: %s" % (threadName, time.ctime(time.time()))
counter -= 1
# 创建新线程
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
# 开启线程
thread1.start()
thread2.start()
print "Exiting Main Thread"

The execution results of the above program are as follows;

Starting Thread-1
Starting Thread-2
Exiting Main Thread
Thread-1: Thu Mar 21 09:10:03 2013
Thread-1: Thu Mar 21 09:10:04 2013
Thread-2: Thu Mar 21 09:10:04 2013
Thread-1: Thu Mar 21 09:10:05 2013
Thread-1: Thu Mar 21 09:10:06 2013
Thread-2: Thu Mar 21 09:10:06 2013
Thread-1: Thu Mar 21 09:10:07 2013
Exiting Thread-1
Thread-2: Thu Mar 21 09:10:08 2013
Thread-2: Thu Mar 21 09:10:10 2013
Thread-2: Thu Mar 21 09:10:12 2013
Exiting Thread-2


The above is the detailed content of What is the Python Threading module? Learn what a thread module is in 3 minutes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn