Home > Article > Backend Development > How to perform Python multi-thread programming, understand Python multi-threading in one article
This article introduces Python multi-threading. If you want to understand Python multi-threading, you must first understand what threading is.
Thread is the smallest unit that the operating system can perform operation scheduling. It is included in the process and is the actual operating unit in the process. A thread refers to a single sequential control flow in a process. Multiple threads can run concurrently in a process, and each thread performs different tasks in parallel.
AndMulti-threading is similar to executing multiple different programs at the same time. Multi-threading has the following advantages:
1. Use threads Tasks in programs that take a long time can be put into the background for processing.
2. The user interface can be more attractive, so that if the user clicks a button to trigger the processing of certain events, a progress bar can pop up to show the progress of the processing
The running speed of the program May speed up
3. Threads are more useful in the implementation of some waiting tasks such as user input, file reading and writing, and network sending and receiving data. In this case we can release some precious resources such as memory usage and so on.
4. There is still a difference between a thread and a process during execution. Each independent thread has an entry point for program execution, a sequential execution sequence, and an exit point for the program. However, threads cannot execute independently and must exist in the application program, and the application program provides multiple thread execution control.
Each thread has its own set of CPU registers, called the thread's context, which reflects the state of the CPU registers the thread last ran.
The instruction pointer and stack pointer register are the two most important registers in the thread context. The thread always runs in the process context. These addresses are used to mark the memory in the address space of the process that owns the thread.
5. Threads can be preempted (interrupted).
6. A thread can be temporarily put on hold (also called sleeping) while other threads are running - this is the thread's retreat.
Start learning Python multi-threading
There are two ways to use threads in Python: functions or classes to wrap thread objects.
Functional: Call the start_new_thread() function in the thread module to generate a new thread. The syntax is as follows:
thread.start_new_thread ( function, args[, kwargs] )
Parameter description:
1.function - Thread function.
2.args - The parameters passed to the thread function must be a tuple type.
3.kwargs - Optional parameters.
Attached example
#!/usr/bin/python # -*- coding: UTF-8 -*- import thread import time # 为线程定义一个函数 def print_time( threadName, delay): count = 0 while count < 5: time.sleep(delay) count += 1 print "%s: %s" % ( threadName, time.ctime(time.time()) ) # 创建两个线程 try: thread.start_new_thread( print_time, ("Thread-1", 2, ) ) thread.start_new_thread( print_time, ("Thread-2", 4, ) ) except: print "Error: unable to start thread" while 1: pass
The output result of executing the above program is as follows:
Thread-1: Thu Jan 22 15:42:17 2009 Thread-1: Thu Jan 22 15:42:19 2009 Thread-2: Thu Jan 22 15:42:19 2009 Thread-1: Thu Jan 22 15:42:21 2009 Thread-2: Thu Jan 22 15:42:23 2009 Thread-1: Thu Jan 22 15:42:23 2009 Thread-1: Thu Jan 22 15:42:25 2009 Thread-2: Thu Jan 22 15:42:27 2009 Thread-2: Thu Jan 22 15:42:31 2009 Thread-2: Thu Jan 22 15:42:35 2009
The end of the thread generally relies on the natural end of the thread function; it can also be done in the thread function Call thread.exit(), it throws SystemExit exception to achieve the purpose of exiting the thread
The above is the detailed content of How to perform Python multi-thread programming, understand Python multi-threading in one article. For more information, please follow other related articles on the PHP Chinese website!