search

Python multithreading

Nov 23, 2016 pm 01:59 PM
python

Multi-threading is similar to executing multiple different programs at the same time. Multi-threading has the following advantages:

Using threads can put tasks in long-term programs into the background for processing.

The user interface can be more attractive, so that if the user clicks a button to trigger the processing of certain events, a progress bar can pop up to show the progress of the processing

The running speed of the program may be accelerated

In some waiting tasks, For example, threads are more useful for user input, file reading and writing, and network sending and receiving data. In this case we can release some precious resources such as memory usage and so on.

Threads are still different from processes during execution. Each independent thread has an entry point for program execution, a sequential execution sequence, and an exit point for the program. However, threads cannot execute independently and must exist in the application program, and the application program provides multiple thread execution control.

Each thread has his own set of CPU registers, called the thread's context, which reflects the state of the CPU registers the last time the thread ran.

The instruction pointer and stack pointer register are the two most important registers in the thread context. The thread always runs in the process context. These addresses are used to mark the memory in the address space of the process that owns the thread.

Threads can be preempted (interrupted).

Threads can be put on hold (also called sleeping) while other threads are running - this is called thread backing off.

Start learning Python threads

There are two ways to use threads in Python: functions or classes to wrap thread objects.

Functional: Call the start_new_thread() function in the thread module to generate a new thread. The syntax is as follows:

thread.start_new_thread (function, args[, kwargs])

Parameter description:

function - thread function.

args - the parameters passed to the thread function, it must be a tuple type.

kwargs - optional parameters.

Example:

#!/usr/bin/python

import thread

import time

#Define a function for thread

def print_time(threadName, delay):

count = 0

while count

time.sleep(delay)

count += 1

print "%s: %s" % ( threadName, time.ctime(time.time()) )

# Create two threads

try:

thread.start_new_thread(print_time, ("Thread-1", 2,))

thread.start_new_thread(print_time, ("Thread-2", 4,) )

except:

print "Error: unable to start thread"

while 1:

pass

Execute the above program and the output result is as follows:

Thread-1: Thu Jan 22 15:42:17 2009

Thread-1: Thu Jan 22 15:42:19 2009

Thread-2: Thu Jan 22 15:42:19 2009

Thread-1: Thu Jan 22 15:42 :21 2009

Thread-2: Thu Jan 22 15:42:23 2009

Thread-1: Thu Jan 22 15:42:23 2009

Thread-1: Thu Jan 22 15:42:25 2009

Thread-2: Thu Jan 22 15:42:27 2009

Thread-2: Thu Jan 22 15:42:31 2009

Thread-2: Thu Jan 22 15:42:35 2009

Thread The end generally relies on the natural end of the thread function; you can also call thread.exit() in the thread function, which throws SystemExit exception to achieve the purpose of exiting the thread.

Threading module

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 thread module:

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

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.

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:

run(): Method used to represent thread activity.

start(): Start thread activity.

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.

isAlive(): Returns whether the thread is active.

getName(): Returns the thread name.

setName(): Set the thread name.

Use the Threading module to create a thread

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

#!/usr/bin/python

import threading

import time

exitFlag = 0

class myThread (threading.Thread): #Inherit the parent class threading.Thread

def __init__(self, threadID, name, counter):

                                                        .Thread.__init__(self)

                                                                                                                                                                      . #Write the code to be executed into the run function. The thread is being created. After that, run the run function

PRINT "Starting"+Self.name

Print_time (Self.name, Self.Counter, 5)

Print "Exning"+Self.name

Def Print_time (ThreadName, Delay, Delay , counter):

while counter:

if exitFlag:

thread.exit()

time.sleep(delay)

print "%s: %s " % (threadName, time.ctime(time.time ()))

counter -= 1

# Create a new thread

thread1 = myThread(1, "Thread-1", 1)

thread2 = myThread(2, "Thread-2", 2)

# Start the thread

thread1.start()

thread2.start()

print "Exiting Main Thread"

The above program execution results 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

Thread synchronization

If multiple threads jointly modify a certain data, it may occur Unpredictable results. In order to ensure the correctness of the data, multiple threads need to be synchronized.

Simple thread synchronization can be achieved by using Lock and Rlock of Thread object. Both objects have acquire method and release method. For those data that need to be operated by only one thread at a time, the operation can be placed in acquire and release. between methods. As follows:

The advantage of multi-threading is that it can run multiple tasks at the same time (at least that's how it feels). But when threads need to share data, there may be data out-of-synchronization problems.

Consider this situation: all elements in a list are 0, the thread "set" changes all elements to 1 from back to front, and the thread "print" is responsible for reading the list from front to back and printing.

Then, maybe when the thread "set" starts to change, the thread "print" will print the list, and the output will be half 0 and half 1. This is the desynchronization of the data. To avoid this situation, the concept of locks was introduced.

The lock has two states - locked and unlocked. Whenever a thread such as "set" wants to access shared data, it must first obtain the lock; if another thread such as "print" has obtained the lock, then let the thread "set" pause, which is synchronous blocking; wait until the thread " Print "After the access is completed and the lock is released, let the thread "set" continue.

After such processing, when printing the list, either all 0s or all 1s will be output, and there will no longer be the embarrassing scene of half 0s and half 1s.

Example:

#!/usr/bin/python

import threading

import time

class myThread (threading.Thread):

def __init__(self, threadID, name, counter):

threading.Thread.__init__(self)

self.threadID = threadID

                   self.name                                                                                                                                                                                                           self.name                                                      U Self.Counter = Country

DEF RUN (Self):

Print "Starting"+Self.name

#获

, successfully get the True

#optional TimeOUT parameter, will always be blocked when it does not fill in Until the lock is obtained

             # Otherwise it will return False after timeout 

                                                                                                                        threadLock.acquire()                                                                                                          

threadLock.release()

def print_time(threadName, delay, counter):

while counter:

time.sleep(delay)

print "%s: %s" % (threadName, time.ctime(time.time()))

counter -= 1

threadLock = threading.Lock()

threads = []

# Create a new thread

thread1 = myThread(1, "Thread-1", 1)

thread2 = myThread( 2, "Thread-2", 2)

# Start a new thread

thread1.start()

thread2.start()

# Add a thread to the thread list

threads.append(thread1)

threads.append(thread2)

# Wait for all threads to complete

for t in threads:

t.join()

print "Exiting Main Thread"

Thread priority queue ( Queue)

Python's Queue module provides synchronous, thread-safe queue classes, including FIFO (first in first out) queue Queue, LIFO (last in first out) queue LifoQueue, and priority queue PriorityQueue. These queues implement lock primitives and can be used directly in multi-threads. Queues can be used to achieve synchronization between threads.

Commonly used methods in the Queue module:

Queue.qsize() Returns the size of the queue

Queue.empty() If the queue is empty, returns True, otherwise False

Queue.full() If the queue is full , returns True, otherwise False

Queue.full corresponds to maxsize size

Queue.get([block[, timeout]]) to get the queue, timeout waiting time

Queue.get_nowait() is equivalent to Queue.get(False)

Queue.put(item) writes to the queue, timeout waiting time

Queue.put_nowait(item) is equivalent to Queue.put(item, False)

Queue.task_done() After completing a job, Queue.task_done() The function sends a signal to the queue where the task has been completed

Queue.join() actually means waiting until the queue is empty before performing other operations

Example:

#!/usr/bin/python

import Queue

import threading

import time

exitFlag = 0

class myThread (threading.Thread):

def __init__( self, threadID, name, q):

   threading. Thread.__init__(self)

                      self.threadID =   threadID

                                                                                                                            self. Starting " + self.name

" process_data(self .Name, Self.q)

Print "Exning"+Self.name

Def Process_data (ThreadName, Q):

While Not Exitflag:

Queuelock.acquire ()

If not workque.emptt Y (Y ( ):

                                                                                                                                   ;S Print "%s processing%s"%(threadName, data)

else:

queuelock.release ()

time.sleep (1)

ThreadList = ["Thread-", "Thread- 2", "Thread-3"]

nameList = ["One", "Two", "Three", "Four", "Five"]

queueLock = threading.Lock()

workQueue = Queue.Queue (10)

threads = []

threadID = 1

# Create a new thread

for tName in threadList:

thread = myThread(threadID, tName, workQueue)

thread.start()

threads.append(thread)

threadID += 1

# Fill queue

queueLock.acquire()

for word in nameList:

workQueue.put(word)

queueLock.release()

# Wait for the queue to be emptied

while not workQueue.empty():

pass

# Notify the thread that it is time to exit

exitFlag = 1

# Wait for all threads to complete

for t in threads:

t.join()

print "Exiting Main Thread"

The above program execution result:

Starting Thread-1

Starting Thread-2

Starting Thread- 3

Thread- 1 processing One

Thread-2 processing Two

Thread-3 processing Three

Thread-1 processing Four

Thread-2 processing Five

Exiting Thread-3

Exiting Thread-1

Exit ing Thread-2

Exiting Main Thread


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
Python: compiler or Interpreter?Python: compiler or Interpreter?May 13, 2025 am 12:10 AM

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Python For Loop vs While Loop: When to Use Which?Python For Loop vs While Loop: When to Use Which?May 13, 2025 am 12:07 AM

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Python loops: The most common errorsPython loops: The most common errorsMay 13, 2025 am 12:07 AM

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

For loop and while loop in Python: What are the advantages of each?For loop and while loop in Python: What are the advantages of each?May 13, 2025 am 12:01 AM

Forloopsareadvantageousforknowniterationsandsequences,offeringsimplicityandreadability;whileloopsareidealfordynamicconditionsandunknowniterations,providingcontrolovertermination.1)Forloopsareperfectforiteratingoverlists,tuples,orstrings,directlyacces

Python: A Deep Dive into Compilation and InterpretationPython: A Deep Dive into Compilation and InterpretationMay 12, 2025 am 12:14 AM

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Is Python an interpreted or a compiled language, and why does it matter?Is Python an interpreted or a compiled language, and why does it matter?May 12, 2025 am 12:09 AM

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

For Loop vs While Loop in Python: Key Differences ExplainedFor Loop vs While Loop in Python: Key Differences ExplainedMay 12, 2025 am 12:08 AM

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

For and While loops: a practical guideFor and While loops: a practical guideMay 12, 2025 am 12:07 AM

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.