


Detailed explanation of the simple use of Python multi-threading, locks, and event event mechanisms
This article mainly introduces the simple use of Python multi-threading, locks, and event event mechanisms. Now I share it with you and give it as a reference. Let’s take a look together
Threads and processes
1. Threads share the address space of the process that created it, and the process has its own The address space
2. Threads can access all data of the process, and threads can access each other
3. The data between threads are independent
4. The child process copies the thread's data
5. The child process is independent after it is started. The parent process can only kill the child process but cannot exchange data
6. Modify the data in the thread. All will affect other threads, but changes to the process will not affect the child process
threading.Thread
Thread is a threading module One of the most important classes in , you can use it to create threads. There are two ways to create a thread: one is to inherit the Thread class and override its run method; the other is to create a threading.Thread object and pass the callable object as a parameter in its initialization function (__init__) enter.
Let’s first look at an example of creating a thread by inheriting the threading.Thread class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Another way to create a thread:
1 2 3 4 5 6 7 8 9 10 11 |
|
The Thread class also defines the following common methods and attributes:
Thread.getName() Gets the thread name
Thread.setName() Sets the thread name
Thread.name Thread name
Thread.ident Gets the identifier of the thread. The thread identifier is a non-zero integer. This attribute is only valid after the start() method is called. Otherwise, it only returns None
to determine whether the thread is active. From the time the start() method is called to start the thread until the run() method is completed or interrupted due to an unhandled exception, the thread is active
Thread.is_alive()
Thread.isAlive ()
Thread.join([timeout]) Calling Thread.join will block the calling thread until the called thread ends or times out. The parameter timeout is a numeric type, indicating the timeout time. If this parameter is not provided, the calling thread will block until the called thread ends
Python GIL (Global Interpreter Lock)
GIL is not a feature of Python, it is a concept introduced when implementing the Python parser (CPython). Just like C is a set of language (grammar) standards, but it can be compiled into executable code using different compilers. Famous compilers such as GCC, INTEL C, Visual C, etc. The same is true for Python. The same piece of code can be executed through different Python execution environments such as CPython, PyPy, and Psyco. For example, JPython does not have GIL. However, CPython is the default Python execution environment in most environments. Therefore, in the concept of many people, CPython is Python, and they take it for granted that GIL is a defect of the Python language. So let’s be clear here: GIL is not a feature of Python, and Python does not need to rely on GIL at all.
Use of thread lock:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
##Join & Daemon
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Multiple threads can be started under one process, and multiple threads share the memory space of the parent process, which means that each thread can access the same data. At this time, if two threads want to modify the same data at the same time , what will happen?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
##The difference between Rlock and Lock:
RLock allows to be acquired multiple times in the same thread. But Lock does not allow this. Otherwise, an infinite loop will occur, and the program will not know which lock to unlock. Note: If you use RLock, acquire and release must appear in pairs, that is, acquire is called n times, and release must be called n times to truly release the occupied lock
Events
Python provides the Event object for inter-thread communication. It is a signal flag set by the thread. If the signal flag is true, other threads wait until the signal is contacted.
The Event object implements a simple thread communication mechanism. It provides setting signals, clearing signals, waiting, etc. for realizing communication between threads.
event = threading.Event() Create an event
event.set()
Use the set() method of Event to set the Event object The internal signal flag is true. The Event object provides the isSet() method to determine the status of its internal signal flags.
When the set() method of the event object is used, the isSet() method returns true
2 Clear signal
event.clear()
使用Event对象的clear()方法可以清除Event对象内部的信号标志,即将其设为假,当使用Event的clear方法后,isSet()方法返回假
3 等待
event.wait()
Event对象wait的方法只有在内部信号为真的时候才会很快的执行并完成返回。当Event对象的内部信号标志位假时,
则wait方法一直等待到其为真时才返回。也就是说必须set新号标志位真
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
相关推荐:
Python多线程中阻塞(join)与锁(Lock)使用误区解析
The above is the detailed content of Detailed explanation of the simple use of Python multi-threading, locks, and event event mechanisms. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
