


In the previous chapter, we learned some basic methods of Python multi-process programming: using the Process, Pool, Queue, Lock, Pipe and other classes provided by the cross-platform multi-process module multiprocessing to implement sub-process creation, process pool (batch) Create child processes and manage the maximum number of child processes) and inter-process communication. In this chapter, you will learn about multi-threaded programming methods in Python.
1. Threading
Thread is the smallest unit for the operating system to perform tasks. The threading module is provided in the Python standard library, which provides very convenient support for multi-threaded programming.
The following is the code for using threading to implement multi-threading:
1 #!/usr/bin/python 2 # -*- coding: utf-8 -* 3 author = 'zni.feng' 4 import sys 5 reload (sys) 6 sys.setdefaultencoding('utf-8') 7 8 import threading, time 9 10 def test(index):11 print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))12 print 'thread %s starts.' % threading.current_thread().name13 print 'the index is %d' % index14 time.sleep(3)15 print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))16 print 'thread %s ends.' % threading.current_thread().name17 18 if name == "main":19 print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))20 print 'thread %s starts.' % threading.current_thread().name21 #创建线程22 my_thread = threading.Thread(target = test, args=(1,) , name= 'zni_feng_thread')23 #等待2s24 time.sleep(2)25 #启动线程26 my_thread.start()27 #等待线程结束28 my_thread.join()29 print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))30 print 'thread %s ends.' % threading.current_thread().name
The output result is:
2017-01-12 22:06:32 thread MainThread starts. 2017-01-12 22:06:34 thread zni_feng_thread starts. the index is 1 2017-01-12 22:06:37 thread zni_feng_thread ends. 2017-01-12 22:06:37 thread MainThread ends. [Finished in 5.1s]
Among them, the current_thread() function of the threading module will Returns the instance of the current thread.
2. Lock
The biggest difference between multi-process and multi-thread is that in multi-process, the same variable has a copy in each process. Do not affect each other. In multi-threading, all variables are shared by all threads, so any shared variable can be modified by any thread. Therefore, the biggest danger in sharing data between threads is that multiple threads change a variable at the same time. In order to solve this problem, we can use the Lock class of the threading module to lock the shared variables.
Let’s first look at an example of using multiple threads to write the same shared variable without locking:
1 #!/usr/bin/python 2 # -*- coding: utf-8 -* 3 author = 'zni.feng' 4 import sys 5 reload (sys) 6 sys.setdefaultencoding('utf-8') 7 import threading 8 9 class Account:10 def init(self):11 self.balance = 012 13 def add(self):14 for i in range(0,100000):15 self.balance += 116 17 def delete(self):18 for i in range(0,100000):19 self.balance -=1 20 21 if name == "main":22 account = Account()23 #创建线程24 thread_add = threading.Thread(target=account.add, name= 'Add')25 thread_delete = threading.Thread(target=account.delete, name= 'Delete')26 27 #启动线程28 thread_add.start()29 thread_delete.start()30 31 #等待线程结束32 thread_add.join()33 thread_delete.join()34 35 print 'The final balance is: ' + str(account.balance)
The running result is:
The final balance is: -51713 [Finished in 0.1s]
It can be found that every time it is run, it The final results will be different, and none of them will be 0. This is because conflicts occur when different threads modify the same variable at the same time, and some intermediate variables are not used in order.
Now we use Lock to lock the program:
1 #!/usr/bin/python 2 # -*- coding: utf-8 -* 3 author = 'zni.feng' 4 import sys 5 reload (sys) 6 sys.setdefaultencoding('utf-8') 7 import threading 8 9 class Account:10 def init(self):11 self.balance = 012 13 def add(self, lock):14 #获得锁15 lock.acquire()16 for i in range(0,100000):17 self.balance += 118 #释放锁19 lock.release()20 21 def delete(self, lock):22 #获得锁23 lock.acquire()24 for i in range(0,100000):25 self.balance -=1 26 #释放锁27 lock.release()28 29 30 if name == "main":31 account = Account()32 lock = threading.Lock()33 #创建线程34 thread_add = threading.Thread(target=account.add, args=(lock, ), name= 'Add')35 thread_delete = threading.Thread(target=account.delete, args=(lock, ), name= 'Delete')36 37 #启动线程38 thread_add.start()39 thread_delete.start()40 41 #等待线程结束42 thread_add.join()43 thread_delete.join()44 45 print 'The final balance is: ' + str(account.balance)
It can be found that no matter how many times it is executed, the balance result is 0. If you print out the results of each balance calculation, you will also find that when one thread starts executing, the other thread will wait until the previous thread has finished executing (to be precise, lock.release() has finished executing) before starting. implement.
The final balance is: 0 [Finished in 0.1s]
【Related recommendations】
1. Examples of multi-process and multi-threading in Python (1)
3. Is multi-process or multi-thread faster in python?
4. Detailed introduction to Python processes, threads, and coroutines
5. Python concurrent programming thread pool/process pool
The above is the detailed content of Multi-process and multi-thread examples in Python (2) Programming methods. For more information, please follow other related articles on the PHP Chinese website!

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

Useanarray.arrayoveralistinPythonwhendealingwithhomogeneousdata,performance-criticalcode,orinterfacingwithCcode.1)HomogeneousData:Arrayssavememorywithtypedelements.2)Performance-CriticalCode:Arraysofferbetterperformancefornumericaloperations.3)Interf

No,notalllistoperationsaresupportedbyarrays,andviceversa.1)Arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,whichimpactsperformance.2)Listsdonotguaranteeconstanttimecomplexityfordirectaccesslikearraysdo.

ToaccesselementsinaPythonlist,useindexing,negativeindexing,slicing,oriteration.1)Indexingstartsat0.2)Negativeindexingaccessesfromtheend.3)Slicingextractsportions.4)Iterationusesforloopsorenumerate.AlwayschecklistlengthtoavoidIndexError.

ArraysinPython,especiallyviaNumPy,arecrucialinscientificcomputingfortheirefficiencyandversatility.1)Theyareusedfornumericaloperations,dataanalysis,andmachinelearning.2)NumPy'simplementationinCensuresfasteroperationsthanPythonlists.3)Arraysenablequick

You can manage different Python versions by using pyenv, venv and Anaconda. 1) Use pyenv to manage multiple Python versions: install pyenv, set global and local versions. 2) Use venv to create a virtual environment to isolate project dependencies. 3) Use Anaconda to manage Python versions in your data science project. 4) Keep the system Python for system-level tasks. Through these tools and strategies, you can effectively manage different versions of Python to ensure the smooth running of the project.

NumPyarrayshaveseveraladvantagesoverstandardPythonarrays:1)TheyaremuchfasterduetoC-basedimplementation,2)Theyaremorememory-efficient,especiallywithlargedatasets,and3)Theyofferoptimized,vectorizedfunctionsformathematicalandstatisticaloperations,making


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
