Python 3.x Study Notes 15 (multithreading), python3.x
1. Thread process
Process: The program cannot run independently. Only when it is loaded into the memory can the system allocate resources for it to run. This type of execution program is called a process and does not have the execution sentiment, only a set of various program resources
Thread: A thread is the smallest unit that the operating system can schedule operations. It is included in the process and is the actual operating unit of the process. A thread refers to a single-Order Control Flow in a process. A process can be concurrently executed by multiple threads, and each thread can execute different tasks in parallel.
2. Differences between threads and processes
The thread shares the memory space, and the memory of the process is independent.
The thread shares the address space of the process that created it. The process has its own address space.
The thread can directly access the data segment of its process; the process has its own copy of the Data Segment of the parent process.
A thread can communicate directly with other threads of a process. A process must communicate with a sibling process through inter-process communication.
New threads are easy to create; new processes need to repeat the parent process.
A thread can control the threads of the same process to a considerable extent. A process can only control sub-processes.
Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of other threads of the process; Changes to the parent process will not affect the sub-process.
3. One thread has at least one thread
4. thread lock
When each thread wants to modify public data, in order to prevent others from modifying the data before the modification, it can add a lock to the data, in this way, when other threads want to modify the data, they must wait for you to finish modification and release the lock before accessing the data.
5. Semaphore (Semaphore)
The mutex lock allows only one thread to change data at the same time, while Semaphore allows a certain number of threads to change data at the same time. For example, if there are three pits in the toilet, only three persons can go to the toilet at most, the people later can only wait for someone to come in.
6. The function of join is to wait for the thread to finish execution.
7. Exercise
Semaphores
_ Author _ = "Narwhale" import threading, timedef run (n): semaphore. acquire () time. sleep (1) print ('thread % s is running! '% N) semaphore. release () if _ name _ = '_ main _': semaphore = threading. boundedSemaphore (5) # A maximum of five threads can run for I in range (20) simultaneously: t = threading. thread (target = run, args = (I,) t. start () while threading. active_count ()! = 1: passelse: print ('all threads are finished! ')
Producer and consumer model
_ Author _ = "Narwhale" import queue, time, threadingq = queue. queue (10) def producer (name): count = 0 while True: print ('% s produced Steamed Stuffed Bun % s' % (name, count) q. put ('steamed stuffed bun % s' % count) count + = 1 time. sleep (1) def consumer (name): while True: print ('% s took % s and ate it ..... '% (Name, q. get () time. sleep (1) A1 = threading. thread (target = producer, args = ('a1',) A1.start () B1 = threading. thread (target = consumer, args = ('b1 ',) B1.start () B2 = threading. thread (target = consumer, args = ('b2',) B2.start ()
Traffic lights
_ Author _ = "Narwhale" import threading, timeevent = threading. event () def light (): event. set () count = 0 while True: if count> 5 and count <10: event. clear () print ('\ 033 [41; 1 m red light \ 033 [0m') elif count> 10: event. set () count = 0 else: print ('\ 033 [42; 1 m green light \ 033 [0m') time. sleep (1) count + = 1def car (n): while True: if event. isSet (): print ('\ 033 [34; 1 m % s the car is running! \ 033 [0m' % n) time. sleep (1) else: print ('car stopped ') event. wait () light = threading. thread (target = light, args = () light. start () car1 = threading. thread (target = car, args = ('Tesla ',) car1.start ()