In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Thread explanation
multithreading is similar to executing several different programs at the same time, and multithreading has the following advantages:
Threads can be used to put tasks in a program that have been occupied for a long time in the background.
The user interface can be more attractive, such as when the user clicks a button to trigger the handling of certain events, and a progress bar pops up to show the progress of the processing.
The program may run faster.
Threads are more useful in the implementation of some waiting tasks, such as user input, file reading and writing, and network data sending and receiving. In this case, we can release some precious resources such as memory footprint and so on.
threads are different from processes in the process of execution. Each independent thread has an entrance to the program, a sequential execution sequence, and an exit to the program. However, threads cannot execute independently and must be stored in the application, which provides multiple thread execution control.
each thread has its own set of CPU registers, called the thread's context, which reflects the status of the last time the thread ran the thread's CPU register.
The instruction pointer and the stack pointer register are the two most important registers in the thread context. The thread always runs in the context of the process. These addresses are used to indicate the memory in the process address space that owns the thread.
threads can be preempted (interrupted).
threads can be put on hold (also known as sleep) while other threads are running-this is the thread's concession.
threads can be divided into:
Kernel threads: created and undone by the operating system kernel.
User thread: a thread implemented in a user program without kernel support.
The two modules commonly used in Python3 threads are:
_ thread
Threading (recommended)
The thread module is obsolete. Users can use the threading module instead. Therefore, the "thread" module can no longer be used in Python3. For compatibility, Python3 renamed thread to "_ thread".
Start learning Python threads
There are two ways to use threads in Python: functions or wrapping thread objects with classes.
function: 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 parameter passed to the thread function, which must be of type tuple.
Kwargs-optional parameter.
instance:
#! / usr/bin/python3import _ threadimport time# defines a function def print_time (threadName, delay): count = 0 while count < 5: 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
The output of the above program executed by is as follows:
Thread module
Python3 provides support for threads through two standard libraries, _ thread and threading.
_ thread provides low-level, raw threads and a simple lock, which is limited compared to the threading module.
In addition to all the methods in the _ thread module, the threading module also provides other methods:
Threading.currentThread (): returns the current thread variable.
Threading.enumerate ():
Returns a list that contains the running thread. Running refers to threads after starting and ending, excluding threads before starting and terminating.
Threading.activeCount ():
Returns the number of running threads, with the same result as len (threading.enumerate ()).
In addition to the usage of , the threading module also provides the Thread class to handle threads, and the Thread class provides the following methods:
Run (): the method used to represent thread activity.
Start (): starts thread activity.
Join ([time]): wait until the thread is aborted. This blocks the calling thread to the thread's join ()
Method is called to abort-either exits normally or throws an unhandled exception-or an optional timeout occurs.
IsAlive (): returns whether the thread is active.
GetName (): returns the thread name.
SetName (): sets the thread name.
Create a thread using the threading module
We can create a new subclass by inheriting directly from threading.Thread, and then call the start () method to start the new thread, that is, it calls the thread's run () method:
5) print ("exit thread:" + self.name) def print_time (threadName, delay, counter): while counter: if exitFlag: threadName.exit () time.sleep (delay) print ("% s:% s"% (threadName, time.ctime (time.time () counter-= create a new thread thread1 = myThread (1, "Thread-1" 1) thread2 = myThread (2, "Thread-2", 2) # start a new thread thread1.start () thread2.start () thread1.join () thread2.join () print ("exit main thread")
The execution results of the above programs in are as follows:
Thread synchronization
if multiple threads modify a certain data together, unexpected results may occur. In order to ensure the correctness of the data, multiple threads need to be synchronized.
As follows:
The advantage of multithreading is that it can run multiple tasks at the same time (at least it feels that way). However, when threads need to share data, the data may be out of sync.
considers a situation where all elements in a list are 0, and thread "set" changes all elements to 1 from back to front, while thread "print" is responsible for reading the list from front to back and printing.
, 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, which is the data out of sync. In order to avoid this situation, the concept of lock is introduced.
locks have two states-locked and unlocked. Whenever a thread such as "set" wants to access shared data, it must first obtain a lock; if another thread, such as "print", has acquired the lock, then let the thread "set" pause, that is, synchronous blocking; wait until the thread "print" has finished accessing, release the lock, and then let the thread "set" continue.
After this processing, prints out either all zeros or all 1s when printing the list, and there will be no more embarrassing situations of half zero and half one.
instance:
#! / usr/bin/python3import threadingimport timeclass myThread (threading.Thread): def _ init__ (self, threadID, name, counter): threading.Thread.__init__ (self) self.threadID = threadID self.name = counter def run (self): print ("Open Thread:" + self.name) # acquire the lock For thread synchronization threadLock.acquire () print_time (self.name, self.counter, 3) # release locks Start the next thread threadLock.release () def print_time (threadName, delay, counter): while counter: time.sleep (delay) print ("% s:% s"% (threadName, time.ctime (time.time () counter-= 1threadLock = 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 finish for t in threads: t.join () print ("exit main thread")
executes the above program, and the output is as follows:
Thread priority queue (Queue)
Synchronous, thread-safe queue classes are provided in Python's Queue module, including FIFO (first-in, first-out) queues, Queue,LIFO (last-in, first-out) queues, LifoQueue, and priority queues, PriorityQueue.
these queues implement lock primitives, which can be used directly in multithreading, and queues can be used to achieve synchronization between threads.
Common methods in the Queue module:
Queue.qsize () returns the size of the queue
Queue.empty () returns True if the queue is empty, and vice versa False
Queue.full () returns True if the queue is full, and vice versa False
Queue.full corresponds to maxsize size
Queue.get ([block [, timeout]]) get queue, timeout wait time
Queue.get_nowait () is equivalent to Queue.get (False)
Queue.put (item) write queue, timeout wait time
Queue.put_nowait (item) is equivalent to Queue.put (item, False)
Queue.task_done () after completing a task, the Queue.task_done () function sends a signal to the queue where the task has been completed.
Queue.join () actually means waiting for the queue to be empty before performing anything else.
instance:
#! / usr/bin/python3import queueimport threadingimport timeexitFlag = 0class myThread (threading.Thread): def _ _ init__ (self, threadID, name, Q): threading.Thread.__init__ (self) self.threadID = threadID self.name = q def run (self): print ("Open thread:" + self.name) process_data (self.name) Self.q) print ("exit thread:" + self.name) def process_data (threadName, Q): while not exitFlag: queueLock.acquire () if not workQueue.empty (): data = q.get () queueLock.release () print ("% s processing% s"% (threadName) Data) else: queueLock.release () time.sleep (1) threadList = ["Thread-1", "Thread-2", "Thread-3"] nameList = ["One", "Two", "Three", "Four", "Five"] queueLock = threading.Lock () workQueue = queue.Queue (10) threads = [] threadID = create a new thread for tName in threadList: thread = myThread (threadID, tName) WorkQueue) thread.start () threads.append (thread) threadID + = filling queue queueLock.acquire () for word in nameList: workQueue.put (word) queueLock.release () # waiting queue emptying while not workQueue.empty (): pass# notifies thread that it is time to exit exitFlag = waiting for all threads to finish for t in threads: t.join () print ("exit main thread")
The execution results of the above programs:
This is the end of the introduction of "Python multithreaded instance Analysis". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.