Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python multithreading application skills

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use Python multithreading application skills, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

In programming language, the application of multithread is an important application technology, so the application of multithread in Python is also very important. Threads are "lightweight" relative to processes, and the operating system uses fewer resources to create and manage threads. Threads in the program execute in the same memory space and share many of the same resources.

How to create a thread object in Python multithreading

If you want to create a thread object, it's simple, as long as your class inherits threading.Thread, and then calls the _ _ init__ method of threading.Thread in _ _ init__ first.

Import threading class mythread (threading.Thread): def _ _ init__ (self, threadname): threading.Thread.__init__ (self, name = threadname)...

This is just an empty thread. I don't want him to pull an empty cart. He has to do some real work for me. Quite simply, override the run () method of the class to include everything you want to do when the thread executes:

Import threading import time class mythread (threading.Thread): def _ init__ (…) :... . Def run (self): for i in range (10): print self.getName, I time.sleep (1)

In the above code, we have the thread output information to the screen every 1 second after execution, and ending getName () 10 times is a method of the threading.Thread class to get the name of the thread object. Another method, setName (), is, of course, to set the thread object's name.

If you want to create a thread, first create a thread object:

Mythreadmythreadmythread1 = mythread ('mythread1')

After a threaded object is created, it is in the "born" state. How do you get the Python multithreaded object running? Simply call the thread object's start () method:

Mythread1.start ()

The thread is now in the "ready" state, or "runnable" state.

Is that weird? Isn't it already start? Why not call it a "running" state? Actually, there's a reason. Because our computers generally do not have the real parallel processing ability. What we call Python multithreading is to divide the time into segments, and then let one thread execute at regular intervals, then enter the "sleeping" state, and then wake up another thread in the "sleeping", so loop runnable- > sleeping- > runnable. Just because the execution speed of the computer is very fast, and the time interval is so small that we can't feel it and think it's going on at the same time. So a thread is only in a runnable state after start, and it is up to the system to schedule when it runs.

When will that thread "dead"? Generally speaking, when the execution of the run method of a thread object ends or an exception is thrown during execution, then the thread ends. The system automatically cleans up the "dead" status thread. If a thread T1 needs to wait for another thread T2 to finish execution before it can run, you can call the T2 join () method on T1:

... . Def T1 (…) :... T2.join ()...

In this way, T1 waits for T2 to finish after executing the t2.join () statement before continuing to run.

But if T1 is an endless cycle, then there is no point in waiting. What should we do? You can give a floating-point number a timeout parameter when calling the join () method of T2, so that the thread does not wait for the flower to thank you. I'll wait for you for 10 seconds, and I won't allow me to remarry until you come back?

Def T1 (…) :... T2.join (10)...

If the main thread of a process is finished and the child thread is still executing, then the process will not exit until all the child threads are finished, how to make the other child threads retreat obediently with the boss when the main thread ends? Then set those who are disobedient as obedient boys and use the thread object's setDaemon () method with a Bool parameter. True's words mean that you have to be obedient, my boss (main thread) yell, you should also follow the withdrawal, can not slow down. If it's False, you don't have to be so obedient. The boss allows you to live up to your orders abroad. It is important to note that the setDaemon () method must be called before the thread object calls the start () method, otherwise it has no effect.

T1 = mythread ('T1') print t1.getName (), t1.isDaemon () t1.setDaemon (True) print t1.getName (), t1.isDaemon () t1.start () print 'main thread exit'

When the print 'main thread exit' is executed, the main thread exits, and of course the T1 thread ends as well. But if you don't use the setDaemon () method of the T1 thread object, even if the main thread ends, you have to wait for the T1 thread to finish itself before exiting the process. IsDaemon () is used to get the Daemonflag state of a thread object.

How do I get information about Python multithreading? Get a reference to the currently running thread:

Running = threading.currentThread ()

Get a list of all currently active objects (that is, any thread that the run method starts but does not terminate):

Threadlist = threading.enumerate ()

Get the length of this list:

Threadcount = threading.activeCount ()

To view the state of a thread object, call the thread object's isAlive () method, and a return of 1 indicates that it is in a "runnable" state and has no "dead":

Threadflag = threading.isAlive () after reading the above, do you have any further understanding of how to use Python multithreading application techniques? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report