Home  >  Article  >  Backend Development  >  python3 multithreading

python3 multithreading

(*-*)浩
(*-*)浩Original
2019-09-02 14:40:153809browse

python3 multithreading

Multi-threading is similar to executing multiple different programs at the same time. Multi-threading has the following advantages: (Recommended learning: web front-end video tutorial )

Using threads can put tasks in long-term programs into the background for processing.

The user interface can be more attractive. For example, when 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 program may run faster.

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.

Each independent thread has an entry point for program running, 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.

Threads can be preempted (interrupted).

Threads can be temporarily put on hold (also called sleeping) while other threads are running - this is the thread's retreat.

Threads can be divided into:

Kernel threads: created and revoked by the operating system kernel.

User thread: A thread implemented in the user program without kernel support.

The two commonly used modules in Python3 threads are:

_thread

threading (recommended)

thread module has been Abandoned. Users can use the threading module instead. Therefore, the "thread" module can no longer be used in Python3. For compatibility, Python3 renamed thread to "_thread".

Start learning Python threads

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:

function - thread function.

args - the parameters passed to the thread function, it must be a tuple type.

kwargs - Optional parameters.

Example:

#!/usr/bin/python3

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: 无法启动线程")

while 1:
   pass

The above is the detailed content of python3 multithreading. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn