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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about how to use Python multithreading. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

First, practical methods

1. Execution between threads is unordered, and cpu executes whatever thread is scheduled.

two。 The main thread waits for all the child threads to finish before it ends. Setting the daemon thread can realize that the child thread ends immediately when the main thread ends.

3. Set up the daemon thread: 1.threading.Thread (daemon=True), 2. Thread object .setDaemon (True)

4. Global variables are shared among threads, so there is a problem of resource competition.

Execution between threads is unordered. The main thread of cpu will wait for all child threads to finish before it ends. Setting the daemon thread can realize that when the main thread ends, the child thread ends immediately. Daemon thread: 1.threading.Thread (daemon=True), 2. Thread object .setDaemon (True) global variables are shared between threads, there is a resource competition problem''# import module (module name .py) Package name init.py) import threadingimport time def task1 (count): # get the current thread object # t=threading.current_thread () # print ('Task1_name:',t.name) print () for i in range (count): print (' Task A', iTun1) time.sleep (0.5) def task2 (content,count): print ('Task2_name:' Threading.current_thread () .name) for i in range (count): print (f'{content} _ Task B', iTun1) time.sleep (0.5) if _ name__ = ='_ main__': t1=threading.Thread (target=task1,name='T1',daemon=True,args= (5,)) t2=threading.Thread (target=task2,name='T2',kwargs= {'content':'Yes') 'count':5}) # the second way to set the daemon main thread # t1.setDaemon (True) t2.setDaemon (True) t1.start () t1.join () # blocking function T1 will not execute t2.start () # t2.join () print ('Main thread over') until it is finished. 2. Add: Python multi-thread sharing variable resource competition problem

1. Resource competition: sum+=1 execution is roughly divided into three steps (value, plus 1, writeback). You may switch to another thread as soon as you execute 2 steps of cpu.

two。 Solution: 1. Use the join () method to set thread synchronization (inefficient), 2. Lock (it ensures that only one thread is executing at a time).

Resource competition problem: sum+=1 execution will be roughly divided into three steps (value, add 1, write back), may just execute 2 steps cpu to switch to another thread to implement the solution: 1. Use the join () method to set thread synchronization (inefficient), 2. Locking (ensures that only one thread is executing at a time)''import threadingimport time sum=0lock=threading.Lock () def add_num1 (): global sum for i in range (1000000): # lock.acquire () # locking sum+=1 # lock.release () # unlocking print (f' {threading.current_thread (). Name} evaluates to {sum}') def add _ num2 (): global sum for i in range (1000000): # lock.acquire () sum+=1 # lock.release () print (f'{threading.current_thread (). Name} evaluates to {sum}') if _ _ name__ = ='_ main__': t1=threading.Thread (target=add_num1) t2=threading.Thread (target=add_num2) t1.start () # sets the blocking function Set thread synchronization t1.join () t2.start () print (the sum value of f'Main is {sum}') above is all the content of this article "how to use Python multithreading". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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