In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to display Python thread multithreading, for this problem, this article details the corresponding analysis and solution, hoping to help more friends who want to solve this problem find a simpler and easier way.
What multithreading?
Multithreading is multiple independent units of operation that perform the same thing simultaneously.
Think about it, articles are published and read by many readers at the same time. These readers are doing things 'reading' one thread after another.
Multithreading is when multiple readers read the article simultaneously. The point is that there are multiple readers reading at the same time.
If there are multiple readers, read in time, and finally only one reader is reading at any time, although it is multiple readers, it is still a single thread.
Let's take the code we shared earlier: follow and like.
def dianzan_guanzhu(): now = datetime.datetime.now() name = "python is new" print("%s name:%s" % (now, name)) time.sleep(1) result = "Great! " + name + " Pay attention to Lei Xueshui and learn to develop knowledge! " print("%s result:%s" % (now, result)) return result
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# @Project : helloimport threadingimport datetimedef dianzan_guanzhu(): now = datetime.datetime.now() name = "python is new" print("%s name:%s" % (now, name)) time.sleep(1) result = "Great! " + name + " Pay attention to Lei Xueshui and learn to develop knowledge! " print("%s result:%s" % (now, result)) return resultfor i in range(3): 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())
Thread class can be passed name to specify the thread name.
Copy directly to run, here we create 3 threads.
They, in turn, call the dianzan_guanzhu function
Here are the results:
These three threads print at different times, but the content prints out of order and even in series.
Readers can run it several times.
Get active thread related data
thread.active_count function: can get the number of active threads.
threading.current_thread function: can get active thread object, so we can get thread name like this: threading.current_thread().getName().
As mentioned earlier, plus the main thread, there are 4 threads in total.
Run the following code to see:
#!/ 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# @Project : helloimport randomimport threadingimport datetimedef dianzan_guanzhu(): thread_name = threading.current_thread().getName() now = datetime.datetime.now() print("thread started:", thread_name) name = "python new"+thread_name print("%s - %s name:%s" % (thread_name, now, name)) time.sleep(1) result = "Great! " + name + " Pay attention to Lei Xueshui and learn to develop knowledge! " print("%s - %s result:%s" % (thread_name, now, result)) return resultfor i in range(3): mythread = threading.Thread(name="t-" + str(i), target=dianzan_guanzhu) print("mythread:", mythread) print("is_alive:", mythread.is_alive()) mythread.start() ac = threading.active_count() print("active_count:", ac)
If we print the number of active threads, then wait for all three threads to start calling.
With the main thread, there are at most 4 active threads.
Today I'll show you a code implementation where multiple threads execute the same task.
About how to carry out Python thread multi-thread display questions to share the answer here, I hope the above content can have some help for everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.
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.