Preface:
Multi-threading is simply understood:A CPU, or single core, slices time into pieces, and the CPU rotates through one thing at a time, processing the next thing at a specified time slice.
Properties and methods for displaying information about the current thread in the
# coding:utf-8 # Import the threading package import threading if __name__ == "__main__": print("Number of currently active threads", threading.active_count()) print("Show specific information about all current threads.", ()) print("A display of information about the current thread.", threading.current_thread())
Rendering:
2. Add a thread
# coding:utf-8 import threading import time def job1(): # Let this thread run for a few more seconds (5) print("the number of T1 is %s" % threading.current_thread()) if __name__ == "__main__": # Create a new thread new_thread = (target=job1, name="T1") # Start a new thread new_thread.start() print("The current number of threads is", threading.active_count()) print("Specific information on all threads", ()) print("Current thread specific information", threading.current_thread())
Rendering:
3. thread in the join function
(1) The expectation is to finish executing thread 1 and then output All done... "The ideal is rich, the reality is not"
# coding:utf-8 import threading import time def job1(): print("T1 start") for i in range(5): (1) print(i) print("T1 finish") def main(): # Create a new thread new_thread = (target=job1, name="T1") # Start a new thread new_thread.start() print("All done...") if __name__ == "__main__": main()
Rendering:
(2) In order to achieve our expectations, we use the join function to block the T1 thread. what does it mean to block the join function? That is, which thread uses the join function, when the thread is executing, after him the thread program can not be executed, you have to wait for the blocked thread is executed after all the execution, can be executed!
# coding:utf-8 import threading import time def job1(): print("T1 start") for i in range(5): (1) print(i) print("T1 finish") def main(): # Create a new thread new_thread = (target=job1, name="T1") # Start a new thread new_thread.start() # Block this T1 thread new_thread.join() print("All done...") if __name__ == "__main__": main()
Rendering:
4. Using Queue to store the results of threads
The result of thread execution, which cannot be returned by return, is stored using Queue.
# coding:utf-8 import threading from queue import Queue """ The use of Queue """ def job(l, q): for i in range(len(l)): l[i] = l[i] ** 2 (l) def multithreading(): # Create queues q = Queue() # List of threads threads = [] # Two-dimensional lists data = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [6, 6, 6]] for i in range(4): t = (target=job, args=(data[i], q)) () (t) # Blocking on all threads for thread in threads: () results = [] # Place each element of the new queue one by one in the result list for _ in range(4): (()) print(results) if __name__ == "__main__": multithreading()
Rendering:
5. Thread lock lock
When multiple threads are started at the same time, each thread will grab computing resources from each other, which can cause program chaos.
Take a chestnut:
When we were selecting a class in the course selection system, there were 2 spots left in the current basketball class and three of us went to take the class.
The order of course selection is stu1 stu2 stu3,should print the course selection process of all three of them in turn, but the reality is:
# coding:utf-8 import threading import time def stu1(): print("stu1 begins course selection.") global course if course > 0: course -= 1 (2) print("stu1 has been selected successfully. The number of seats left in basketball is now %d." % course) else: (2) print("stu1 failed to select a class, there are 0 spots in basketball, please select another course") def stu2(): print("stu2 begins course selection.") global course if course > 0: course -= 1 (2) print("stu2 selected successfully, now %d of seats left in Basketball." % course) else: (2) print("stu2 failed to select a class, there are 0 spots in basketball, please select another course") def stu3(): print("stu3 begins course selection.") global course if course > 0: course -= 1 (2) print("stu3 course selection successful.") print("The number of spots left in basketball class is %d" %course) else: (2) print("stu3 class selection failed, 0 spots in basketball class, please select another course") if __name__ == "__main__": # Basketball lesson places course = 2 T1 = (target=stu1, name="T1") T2 = (target=stu2, name="T2") T3 = (target=stu3, name="T3") () () ()
Rendering:
In order to solve this situation, we use lock thread synchronization lock, thread concurrent execution, to ensure the atomicity of each thread execution. This effectively prevents the chaos of concurrent thread execution when sharing unified data.
The improved code is as follows:
# coding:utf-8 import threading import time def stu1(): global lock () print("stu1 begins course selection.") global course if course > 0: course -= 1 (2) print("stu1 has been selected successfully. The number of seats left in basketball is now %d." % course) else: (2) print("stu1 failed to select a class, there are 0 spots in basketball, please select another course") () def stu2(): global lock () print("stu2 begins course selection.") global course if course > 0: course -= 1 print("stu2 selected successfully, now %d of seats left in Basketball." % course) else: (1) print("stu2 failed to select a class, there are 0 spots in basketball, please select another course") () def stu3(): global lock () print("stu3 begins course selection.") global course if course > 0: course -= 1 (1) print("stu3 has been selected successfully. The number of seats left in basketball is now %d." % course) else: (1) print("stu3 class selection failed, 0 spots in basketball class, please select another course") () if __name__ == "__main__": # Basketball lesson places course = 2 # Create synchronization locks lock = () T1 = (target=stu1, name="T1") T2 = (target=stu2, name="T2") T3 = (target=stu3, name="T3") () () ()
Rendering:
This article on Python multi-threaded example (simple to understand) of the article is introduced to this, more related Python multi-threaded content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!