Python thread sorting
Threads have five states
New, ready, running, blocked, and dead.
There are three situations of blocking:
Synchronous blocking refers to the state of competing locks. A thread will enter this state when requesting a lock. Once the lock is successfully obtained, it will return to the running state;
Waiting blocking refers to waiting for notification from other threads. state, after the thread obtains the conditional lock, calling "wait" will enter this state. Once other threads send notifications, the thread will enter the synchronous blocking state and compete for the conditional lock again;
And other blocking refers to calling time.sleep(), anotherthread .join() or blocking while waiting for IO. In this state, the thread will not release the acquired lock.
Python provides two ways to use threads, one is functional and the other is class packaging.
* thread
* threading
1. Thread:
>>> import thread >>> dir(thread) ['LockType', '__doc__', '__name__', '__package__', '_count', '_local', 'allocate', 'allocate_lock', 'error', 'exit', 'exit_thread', 'get_ident', 'interrupt_main', 'stack_size', 'start_new', 'start_new_thread']
thread.start_new_thread ( function , args [ , kwargs ] )
Call the start_new_thread() function in the thread module to generate a new thread.
2. Threading:
>>> import threading >>> dir(threading) ['BoundedSemaphore', 'Condition', 'Event', 'Lock', 'RLock', 'Semaphore', 'Thread', 'ThreadError', 'Timer', '_BoundedSemaphore', '_Condition', '_DummyThread', '_Event', '_MainThread', '_RLock', '_Semaphore', '_Timer', '_VERBOSE', '_Verbose', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_active', '_active_limbo_lock', '_after_fork', '_allocate_lock', '_counter', '_enumerate', '_format_exc', '_get_ident', '_limbo', '_newname', '_pickSomeNonDaemonThread', '_profile_hook', '_shutdown', '_sleep', '_start_new_thread', '_sys', '_test', '_time', '_trace_hook', 'activeCount', 'active_count', 'currentThread', 'current_thread', 'deque', 'enumerate', 'local', 'setprofile', 'settrace', 'stack_size', 'warnings']
>>> dir(threading.Thread) ['_Thread__bootstrap', '_Thread__bootstrap_inner', '_Thread__delete', '_Thread__exc_clear', '_Thread__exc_info', '_Thread__initialized', '_Thread__stop', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_block', '_note', '_reset_internal_locks', '_set_daemon', '_set_ident', 'daemon', 'getName', 'ident', 'isAlive', 'isDaemon', 'is_alive', 'join', 'name', 'run', 'setDaemon', 'setName', 'start']
Common methods provided by the threading module:
threading.currentThread(): Returns the current thread variable.
threading.enumerate(): Returns a list containing running threads. Running refers to after the thread starts and before it ends, excluding threads before starting and after termination.
threading.activeCount(): Returns the number of running threads, which has the same result as len(threading.enumerate()).
Inherit the threading.Thread method and override the run method.
Initialization function prototype of threading.Thread class: def __init__(self, group=None, target=None, name=None, args=(), kwargs={})
The parameter group is reserved for future expansion ;
The parameter target is a callable object (also called activity [activity]), which is executed after the thread is started;
The parameter name is the name of the thread. The default value is "Thread-N", where N is a number.
The parameters args and kwargs respectively represent the parameter list and keyword parameters when calling target.
join() method, the thread calling this method will wait until the Thread object is completed and then resume running.
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 be blocked until the called thread ends.
threading.Lock object: mutex, has acquire() and release() methods
RLock allows to be acquired multiple times in the same thread. But Lock does not allow this. Note: If RLock is used, 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.
Threading.Condition object: condition variable. When this object is created, it will contain a Lock object (because condition variable is always used together with mutex). The acquire() and release() methods can be called on the Condition object to control potential Lock objects.
Condition.wait([timeout]): The wait method releases the internal memory occupied, and the thread is suspended until it is awakened after receiving a notification or times out (if the timeout parameter is provided). When the thread is awakened and reoccupies the thread, the program will continue to execute.
Condition.notify(): Wake up a suspended thread (if there is a suspended thread). Note: The notify() method will not release the occupied memory.
Condition.notifyAll() Wake up all suspended threads (if there are suspended threads). Note: These methods do not release the occupied memory.

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.

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.

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.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

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".

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
