Python thread 3-29

Source: Internet
Author: User
Tags processing text switches

Before we knew the concept of the process in the operating system, the program could not be run alone, and the program would be allocated resources to run it only if it was loaded into memory, and this kind of execution would be called a process. The difference between a program and a process is that a program is a set of instructions, which is a static descriptive text of the process, and a process is an execution activity of a program, which belongs to the dynamic concept. In multi-channel programming, we allow multiple programs to be loaded into memory at the same time, which can be implemented concurrently under the dispatch of the operating system. This is the design that greatly improves the CPU utilization. The advent of the process makes it possible for each user to feel the CPU alone, so the process is proposed for multi-channel programming on the CPU.

The process has many advantages, it provides multi-channel programming, let us feel each of us have their own CPU and other resources, can improve the utilization of the computer. Many people do not understand, since the process is so good, why do threads? In fact, careful observation will find that the process is still a lot of defects, mainly reflected in two points:

    • A process can only do one thing at a time, and if you want to do two or more things at once, the process is powerless.

    • If a process is blocked during execution, such as waiting for input, the entire process hangs, even if some work in the process does not depend on the input data, it cannot be executed.

If these two shortcomings are difficult to understand, give a realistic example, perhaps you are clear: if we take the course of the process as a process, then we have to do is ear to listen to the teacher lectures, hand also take notes, the brain still have to think about the problem, so as to effectively complete the task of the lectures. And if only to provide the process of this mechanism, the above three things will not be executed at the same time, can only do one thing, listen to the time can not take notes, and can not use the brain to think, this is one; if the teacher wrote the calculation process on the blackboard, we began to take notes, and the teacher suddenly had a step down, blocked He was thinking over there, and we couldn't do anything else, even if you wanted to think about a problem you didn't understand just now, which is second.

Now you should understand the defects of the process, and the solution is very simple, we can let listen, write, think three independent process, parallel together, so it is obvious to improve the efficiency of lectures. The same mechanism, the thread, is also introduced in the actual operating system.

60 's, in the OS can have resources and independent operation of the basic unit is the process, but with the development of computer technology, the process has a lot of drawbacks, one is because the process is the resource owner, creating, undoing and switching there is a large space-time overhead, so need to introduce Light ProcessThe second is due to the presence of symmetric multiprocessor (SMP), can satisfy multiple operating units, and multiple processes are too expensive to parallel. So in the 80 's, there was units capable of running independently--Thread (Threads) . Note: A process is the smallest unit of resource allocation, and a thread is the smallest unit of CPU scheduling. there is at least one thread in each process.

the difference between a thread and a processCan be summed up in the following 4 points: 1) address space and other resources (such as open files): Processes are independent of each other and shared among threads of the same process.  Threads within a process are not visible in other processes.  2) Communication: Inter-process communication IPC, between threads can directly read and write process data segments (such as global variables) to communicate-the need for process synchronization and mutual exclusion means of support to ensure data consistency.  3) Scheduling and switching: Thread context switches are much faster than process context switches. 4) in a multithreaded operating system, the process is not an executable entity. In a multithreaded operating system, it is common to include multiple threads in a process, each as the basic unit of CPU utilization, and an entity that spends the least overhead.  The thread has the following properties.  1) entities in a lightweight entity thread do not have system resources at all, just a bit of an essential resource that can guarantee independent operation. The entities of the thread include programs, data, and TCB. A thread is a dynamic concept, and its dynamic nature is described by the thread control block TCB.
The TCB includes the following information: ( 1 ) thread state. (2) when the thread is not running, the field resource is saved. (3) A set of execution stacks. (4) store the local variable memory area of each thread. (5) access to main memory and other resources in the same process. A set of registers and stacks that indicate the program counter, the reserved local variable, the few state parameters, and the return address of the instruction sequence being executed. 
The TCB includes the following information2) The basic unit of independent Dispatch and dispatch. In multi-threaded OS, threads are the basic units that can run independently, and thus are the basic units of independent Dispatch and dispatch.  Because the thread is "light", the thread switches very quickly and with little overhead (in the same process). 3) Share process resources.threads in the same process can share the resources owned by the process, which first manifests that all threads have the same process ID, which means that the thread can access every memory resource of the process, and also access the open files, timers, semaphores, etc. owned by the process. Because threads in the same process share memory and files, there is no need to call the kernel to communicate with one another. 4 ) can be executed concurrently. in a process between multiple threads, can be executed concurrently, and even allow all threads in a process to execute concurrently, also, the threads in different processes can execute concurrently, take full advantage of and play the processor and peripheral equipment to work in parallel.

Open a word processing software process, the process must do more than one thing, such as listening to keyboard input, processing text, automatically save the text to the hard disk, the three tasks are the same piece of data, and therefore can not be used multi-process. Only in one process can open three threads concurrently, if it is a single thread, it can only be, keyboard input, can not handle text and auto-save, automatically save the text can not be entered and processed.

Multiple threads share resources in the address space of the same process, which is a simulation of multiple processes on a single computer, sometimes referred to as a lightweight process.

For multiple processes on a single computer, you share physical memory, disks, printers, and other physical resources. Multi-threaded running is similar to running multiple processes, which is a fast switchover between multiple threads by the CPU.

The different processes are full of hostility, each other is preemption, competition CPU relationship, if Thunderbolt will and QQ Rob Resources. And the same process is created by a programmer's program, so the thread within the same process is a partnership, one thread can access the memory address of the other thread, everyone is shared, and one thread dries the memory of another thread, which is purely a problem for the programmer.

Similar to the process, each thread also has its own stack, unlike the process, where the line libraries cannot force the thread out of the CPU using the clock interrupt, and can call the Thread_yield run thread to automatically abandon the CPU and let another thread run.

Threads are usually useful, but the problem with threading is that it is difficult to design a program:

1. If the parent process has multiple threads, does the open child thread require the same number of threads

2. In the same process, if one thread closes the file and another thread is preparing to write to the file?

Therefore, in multithreaded code, more attention is needed to design the program's Logic and protect the program's data.

Thread implementations can be divided into two categories: User-level threads (user-level thread) and kernel thread threads (kernel-level thread), which are also known as kernel-supported threads or lightweight processes. In multi-threaded operating system, the implementation of each system is not the same, in some systems to implement the user-level threads, and some systems to implement the kernel-level threads.

Kernel switching by the user state program itself control the kernel switch, do not need the core interference, less in and out of the kernel state consumption, but not very good use of multi-core CPU.

  

The user space simulates the scheduling of the operating system to invoke a thread in a process, and each process has a runtime system to dispatch the thread. At this point, when the process acquires the CPU, the process then dispatches a thread to execute, and only one thread executes at the same time.

Kernel-level thread: The switch is controlled by the kernel, and when the thread switches, the user state is converted to the kernel state. The switch is completed to return the user state from the kernel state, and the SMP can be used to make use of the multi-core CPU. This is the case with Windows threads.

  

1 kernel support threads are perceived by the OS kernel, while user-level threads are not perceived by the OS kernel.  2 The creation, revocation, and dispatch of user-level threads do not require the support of the OS kernel, which is handled at the level of language (such as Java), while kernel support for thread creation, revocation, and dispatch requires the OS kernel to provide support, and is largely the same as process creation, revocation, and scheduling.  3  When a user-level thread executes a system invoke instruction, it causes its owning process to be interrupted, while the kernel support thread executes the system call instruction, causing the thread to be interrupted only.  4  in a system with only a user-level thread, CPU scheduling is a process-based process in which multiple threads are in a running state, and the user program controls the rotation of the thread, and in a system with kernel support threads, the CPU is dispatched in a thread. The thread scheduler of the OS is responsible for the scheduling of threads.  5 The program entity of a user-level thread is a program that runs under a user-state, while a kernel-enabled program entity is a program that can run in any state.
the difference between a user-level thread and a kernel-level thread
Advantage: When there are multiple processors, multiple threads of a process can execute concurrently. Cons: Dispatched by the kernel.
advantages and disadvantages of kernel threads
Advantage: The thread scheduling does not require the kernel to participate directly, the control is simple. Can be implemented in an operating system that does not support threading. Thread management, such as creating and destroying threads, thread switching costs, and so on, is much less expensive than kernel threads. Allow each process to customize its own scheduling algorithm, thread management is more flexible. Threads can take advantage of more table space and stack space than kernel-level threads. Only one thread is running in the same process, and if one thread is blocking with a system call, the entire process is suspended. Also, a page failure can cause the same problem. Disadvantage: Resource scheduling according to the process, multiple processors, the same process of the thread can only be used in the same processor time-sharing reuse
advantages and disadvantages of user-level threading

User-level and kernel-level multiplexing, kernel same dispatch kernel thread, each kernel thread corresponding to n user thread

  

Introduction

The execution of the Python code is controlled by the Python virtual machine (also known as the interpreter main loop). Python was designed to take into account the main loop, while only one thread was executing. Although the Python interpreter can "run" multiple threads, only one thread runs in the interpreter at any time.
Access to the Python virtual machine is controlled by the global Interpreter Lock (GIL), which ensures that only one thread is running at the same time.

In a multithreaded environment, the Python virtual machine executes as follows:

A, set GIL;

b, switch to a thread to run;

C, run a specified number of bytecode instructions or threads actively give up control (can call Time.sleep (0));

D, set the thread to sleep state;

e, unlock GIL;

D. Repeat all of the above steps again.
The Gil will be locked until the function ends (because no Python bytecode is running, so there is no thread switching) when calling external code (such as C + + extension functions), programmers who write extensions can actively unlock the Gil.

Python provides several modules for multithreaded programming, including thread, threading, and queue. The thread and threading modules allow programmers to create and manage threads. The thread module provides basic threading and lock support, and threading provides a higher-level, more functional thread-management capability. The queue module allows a user to create a data structure that can be used to share information between multiple threads.
Avoid using the thread module because the higher-level threading modules are more advanced, the support for threading is better, and the use of attributes in the thread module may conflict with the threading, followed by a low level of synchronization primitives for the thread module ( There is actually only one, and the threading module is a lot; Moreover, in the thread module, when the main thread ends, all threads are forced to end, no warnings and no normal cleanup work, at least the threading module ensures that the important child threads exit after the process exits.

The thread module does not support daemon threads, and when the main thread exits, all child threads are forced to quit, regardless of whether they are still working. While the threading module supports the daemon thread, the daemon thread is typically a server waiting for a client request, and if no client requests it, it waits, if a thread is set to be a daemon, it means that the thread is unimportant, and the process exits without waiting for the thread to exit.

The Multiprocess module completely imitates the interface of the threading module, which has a great similarity in the use level, so it is no longer described in detail

 from Threading Import Threadimport timedef Sayhi (name):    time.sleep (2)    print ('  %s Say hello' %name)if'__main__'  :    t=thread (target=sayhi,args= ('Egon',))    T.start ()    Print (' main thread ')
How threads are created 1
 fromThreading Import Threadimport timeclassSayhi (Thread): def __init__ (Self,name): Super (). __init__ () Self.name=name def run (self): Time.sleep (2) Print ('%s Say hello'%self.name)if__name__ = ='__main__': T= Sayhi ('Egon') T.start () print ('Main Thread')
How threads are created 2

 fromThreading Import Thread frommultiprocessing Import processimport osdef work (): Print ('Hello', Os.getpid ())if__name__ = ='__main__': #part1: Open multiple threads under the main process, each with the same PID as the main process T1=thread (target=Work ) T2=thread (target=Work ) T1.start () T2.start () print ('Main thread /master process PID', Os.getpid ()) #part2: Multiple processes are open, each process has a different PID P1=process (target=Work ) P2=process (target=Work ) P1.start () P2.start () print ('Main thread /master process PID', Os.getpid ())
PID Comparison
 fromThreading Import Thread frommultiprocessing Import processimport osdef work (): Print ('Hello')if__name__ = ='__main__': #在主进程下开启线程 t=thread (target=Work ) T.start () print ('Main thread /master process')    " "print Result: Hello main thread/Main Process" "#在主进程下开启子进程 T=process (target=Work ) T.start () print ('Main thread /master process')    " "Print Result: main thread/Main process Hello" "
Open the Battle of efficiency
 fromThreading Import Thread frommultiprocessing Import processimport osdef work ():GlobalN N=0if__name__ = ='__main__': # N= -# p=process (target=Work ) # P.start () # p.join () # Print ('Master', N) #毫无疑问子进程p已经将自己的全局的n改成了0, but only to its own, to view the parent process n is still n=1T=thread (target=Work ) T.start () T.join () print ('Master', N) #查看结果为0 because the threads within the same process share the data of the process within the same process as the threads sharing the process? 
sharing problems with memory data

Import multiprocessingimport threadingimport sockets=Socket.socket (Socket.af_inet,socket. Sock_stream) S.bind (('127.0.0.1',8080)) S.listen (5) def action (conn): whileTrue:data=CONN.RECV (1024x768) print (data) conn.send (Data.upper ())if__name__ = ='__main__':     whiletrue:conn,addr=s.accept () p=threading. Thread (target=action,args=(conn,)) P.start ()
Server
Import Sockets=Socket.socket (Socket.af_inet,socket. Sock_stream) S.connect (('127.0.0.1',8080)) whiletrue:msg=input ('>>:'). Strip ()ifNot msg:Continues.send (Msg.encode ('Utf-8')) Data=S.RECV (1024x768) print (data)
Client
  # isAlive (): Returns whether the thread is active.  # getName (): Returns the thread name.  # SetName (): Sets the thread name. Some of the methods provided by the threading module are:  # threading.currentthread (): Returns the current thread variable.  # threading.enumerate (): Returns a list that contains the running thread. Running refers to threads that do not include pre-and post-termination threads until after the thread has started and ends.  # Threading.activecount (): Returns the number of running threads with the same result as Len (Threading.enumerate ()).
methods for thread instance objects
 fromThreading Import Threadimport timedef Sayhi (name): Time.sleep (2) Print ('%s Say hello'%name)if__name__ = ='__main__': T=thread (target=sayhi,args= ('Egon',)) T.start () T.join () print ('Main Thread') Print (T.is_alive ())" "Egon say hello main thread False" "
Join Method

Python thread 3-29

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.