Differences and related processes and threads

What is the process (process)?

A program executable instances is called a process. (The process is a collection of resources)

Each process provides the resources needed to perform this procedure, a process has a virtual memory address space, executable code, call the operating system of joint security context (permission)

The only process identifier pid, a priority class, minimum and maximum working memory space, the process of implementation requires at least one thread (when a process of time, at least one thread), the process is usually the first thread It called the main thread, but the main thread can create additional threads (child thread, the thread can also create sub-sub-thread, both delivery no affiliation).

The difference between process and thread?

Thread starts faster than the process, but the execution speed of the two are not comparable.

1. The memory space between the threads share memory space, the process is independent.

2. Two threads can directly access data in a process of shared memory space. Data between multiple sub-processes do not share, for the same data, they will copy the parent process, completely independent of each other.

It may interact directly between a plurality of threads 3. A process, (related to shared data, the transmission of information, etc.). Both a process wants to communicate, must be achieved through an intermediate proxy.

4. The new thread is very easy to create, create a new process needs to be a clone of its parent process.

5. A thread can control and operation of the other threads in the same process, but a process can only operate its children.

6. For the main thread changes may affect the behavior of other threads (because they share data) For the parent process modifications do not affect other sub-processes data (but the parent process deleted , the child will be affected)

It starts the main thread and the child thread are parallel, the calculation time, no time serial (sequential order) relationship. 

In order to better understand:

1. Understanding Concurrency

. 1  Import Threading
 2  Import Time
 . 3  
. 4  DEF RUN (n-):
 . 5      Print ( " Task " , n-)
 . 6      the time.sleep (2 )
 . 7  
. 8  # thread concurrent execution of the two programs, to wait for a total of 2 seconds 
. 9 T1 = Threading. the Thread (target = RUN, args = ( " T1 " ,))
 10 T2 of the threading.Thread = (target = RUN, args = ( " T2 " ,))
 . 11  t1.start ()
 12 is  t2.start ()
 13 is  
14  # contrast 
15  #Both serial execution of 4 seconds 
16  # RUN ( 'T1') 
. 17  # RUN ( 'T2')
2. # inherited form of invocation, class of concurrency
1  # inheritance invocation, as a class of concurrency 
2  Import Threading
 . 3  Import Time
 . 4  
. 5  class the MyThread (of the threading.Thread):
 . 6      DEF  the __init__ (Self, n-, sleep_time):
 . 7          . Super (the MyThread, Self) the __init__ ()
 . 8          = self.n n-
 . 9          self.sleep_time = sleep_time
 10  
. 11      DEF RUN (Self):
 12 is          Print ( " running Task " , self.n)
 13 is          the time.sleep (self.sleep_time)
 14         Print ( ' Task DONE ' , self.n)
 15  
16 T1 = the MyThread ( ' T1 ' , 2 )
 . 17 t2 = the MyThread ( ' t2 ' ,. 4) # T1 and the sleep time t2 is inconsistent, the main thread calculating time, both require the Join 
18 is  
. 19  t1.start ()
 20 is  t2.start ()
 21 is  
22 is  t1.join ()
 23 is  t2.join ()
 24  
25  Print ( ' main Thread has ... ' )

3.for concurrent multi-cycle

Its main thread and the child thread are activated in parallel, both the order is not finished.

When no join, the main thread does not wait for the child threads to complete before execution down

 

. 1  Import Threading
 2  Import Time
 . 3  
. 4  DEF RUN (n-):
 . 5      Print ( " Task " , n-)
 . 6      the time.sleep (2 )
 . 7      Print ( ' Task DONE ' , n-)
 . 8      Print ( " ------ has Finished .. threads " , threading.current_thread ()) # thread type currently executing 
. 9  
10  
. 11 START_TIME = the time.time ()
 12 is  # for concurrent multi loop 
13 is t_objs = []# Deposit thread instance 
14  for I in Range (50 ):
 15  
16      T = of the threading.Thread (target = RUN, args = ( " T-% S " % I,))
 . 17  
18 is      t.setDaemon (True) # the current threads to daemon thread 
19  
20      t.start ()
 21  
22      t_objs.append (t) # in order to not block behind the thread starts, not join here, to put a list inside 
23  #
 24-  # for t in t_objs: # cycle thread instance list, wait for all threads finished 
25  #      t.join () 
26  
27  Print ( "Finished .. All Threads has ------ " , threading.current_thread (), threading.active_count ()) # View the current thread type and the number of active 
28  Print ( ' cost: ' , time.time () - START_TIME)
 29  
30  # Comparative 
31 is  # RUN ( 'T1') 
32  # RUN ( 'T2')

Daemon thread:

All sub-thread programming daemon threads: the main thread does not wait for all routine exits after completion, as long as the main thread is finished, you can exit.

The main program does not wait for the end of the main thread exits, it will wait until after the end non-daemon thread exits, the program would not bother daemon thread.

Daemon thread scenarios: socket server settings

 

Multi-core: multiple threads can be executed simultaneously.

But in python, at the same time you can only execute one thread. python interpreter directly interface to the calling thread c language, only the context of the transfer. Process thread execution, python which can not be controlled so that the first thread execution

. Therefore, the interpreter controls the outlet, at the same time, only one thread of work, but also allows only one thread to obtain data. (Adding Global Interpreter Lock ) (cpython)

 

Guess you like

Origin www.cnblogs.com/bocaimao/p/10875778.html