Home >Backend Development >Python Tutorial >Multi-process and multi-thread examples in Python (2) Programming methods
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.
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.
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!