python process and thread Essentials

Procedures and processes

A program can not be run separately, only the program is loaded into memory, the system allocates resources to run for it, and the execution of this program it is called process.

The difference is that the procedures and processes: the program is a set of instructions, it is static text describing processes running; the process is a program of implementation activities, is the dynamic concept.

The process is the smallest unit of resource allocation, the thread is the smallest unit of CPU scheduling, each process has at least one thread.

 Thread

A thread is the smallest unit of the process operation is performed, the process is an entity, usually a process comprising a plurality of threads, each thread is a basic unit of CPU utilization.

1, in a multi-threaded OS, the thread is the basic unit can operate independently, thus also independent of the scheduling and dispatch of the basic unit

2, the same process for each thread can share resources owned by the process

3, between multiple threads in a process can be executed concurrently

When a process starts, it will default to produce a main thread, because the thread is executing the smallest unit streams, when setting multiple threads, the main thread creates multiple child threads, in python, by default (in fact setDaemon (False )), the main thread after the completion of the implementation of its mandate, withdrew, this time the child thread will continue to execute its mandate until the end of their mission,

 

Python3 provide support for threading through two standard library _thread and threading. 
_thread provides a low level, the original thread and a simple lock, it is compared to the threading module function is quite limited. 
Other methods threading module contains all the methods _thread modules, also provided:

  • threading.currentThread (): Returns the current thread variable.
  • threading.enumerate (): Returns a list of running threads. Refers to the thread starts running, before the end, does not include a thread before starting and after termination.
  • threading.activeCount (): Returns the number of running threads, and len (threading.enumerate ()) have the same result.

In addition to using the method, the threading module also provides a Thread class processing thread, Thread class provides the following methods:

    • run (): to indicate the method of thread activity.
    • start (): start thread activity.
    • join ([time]): Wait until the thread suspended. This blocks the calling thread until the thread's join () method is called suspension - normal exit or throw an unhandled exception - or the optional timeout occurs.
    • isAlive (): Returns the thread is active.
    • getName (): Returns the thread name.
    • setName (): Set the thread name.
    • setDaemon (): set to a background thread or a foreground thread (default) if a background thread, the main thread execution process, also perform a background thread, after the main thread is finished, the background thread whether successful or not, are stopped; if the foreground thread, the main thread execution process, the foreground thread are executed after the main thread is finished, waiting foreground thread is also performed after the completion of the program stops.
#!/usr/bin/env python
#coding:utf8

Import Threading   # threading module 
Import Time

DEF sayHi (NUM):   # function definition for each thread to run 
    Print ( ' running ON Number The ' NUM,)
    time.sleep(3)

if __name__ == "__main__":
    T1 = of the threading.Thread (target = sayHi, args = (33 is,)) # generates a thread instance 
    T2 of the threading.Thread = (target = sayHi, args = (22 is,)) # generates another thread instance 

    t1.start ()   # start a thread 
    t2.start ()
     Print (t1.getName ()) # get the thread name 
    Print (t2.getName ())
    t1.join ()   # block the main thread, the thread waits for the sub-t1 after executing the execution code behind 
    t2.join ()   # block the main thread, the sub-code execution waiting t2 later after executing the thread 
    Print ( ' --- --end ' )

Guess you like

Origin www.cnblogs.com/Christbao/p/12023118.html