多线程多进程学习

http://www.cnblogs.com/alex3714/articles/5230609.html

什么是线程(thread)?

线程是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务

A thread is an execution执行 context上下文, which is all the information a CPU needs to execute a stream流 of instructions指令.

一个线程就是一个执行的上下文,CPU执行指令所需的指令流。

Suppose假设 you're reading a book, and you want to take休息一下 a break right now,

假设你在读一本书,这个时候你想休息一下

but you want to be able to come back and resume reading from the exact point where you stopped.

但是你想回来的时候从上次精确的位置重新开始读

One way to achieve that is by jotting 笔记down the page number, line number, and word number.

一种实现的方法是去记下这个页码,行号,和字符位置。

So your execution context for reading a book is these 3 numbers.

所以你执行上下文切换需要记住这三个数字

If you have a roommate, and she's using the same technique 技巧, she can take 拿走the book while you're not using it, and resume reading from where she stopped.

如果您有一个室友,他也想用同样技巧,当你不用的时候,他把这个书拿走,从他上次读到的地方开始读。

Then you can take it back, and resume it from where you were.

你可以再拿回来,从你读到的位置重新开始读。

Threads work in the same way. 线程以同样的方式工作。

A CPU is giving you the illusion that it's doing multiple computations at the same time.

一核CPU给你的错觉(illusion) 是同时在处理多个计算

It does that by spending a bit of time on each computation.It can do that because it has an execution context for each computation.

他通过在每个计算上花费一丁点时间,因为他可以执行切换上下文在每个计算任务之间

Just like you can share a book with your friend, many tasks can share a CPU.

就像你可以共享一本书给你朋友一样,许多个任务可以共享一个CPU。

On a more technical level, an execution context (therefore a thread) consists of the values of the CPU's registers.

从技术层面讲,一个执行上下文就是一个线程,一个CPU寄存器的值的组合

Last: threads are different from processes.最后,线程和进程是不一样的。

A thread is a context of execution, while a process is a bunch of resources associated with a computation.

线程是一个上下文执行的指令,进程就是一簇(bunch)资源的集合,在计算时。

A process can have one or many threads.

一个进程可以有一个或多个线程

Clarification: the resources associated with a process include memory pages (all the threads in a process have the same view of the memory), file descriptors (e.g., open sockets), and security credentials (e.g., the ID of the user who started the process).

澄清一下,资源包括内存页(内存地址)(一个进程中的所有线程时共享内存的),如文件描述符(如套接字),安全证书,进程中的用户ID

什么是进程(process)?

An executing instance of a program is called a process.

每个程序执行的实例称作一个进程

Each process provides the resources needed to execute a program.

每个进程提供一个程序执行所需的资源

A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier,

一个进程有一个虚拟的内存地址,可执行代码,系统接口,安全的上下文,进程号

environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution. Each process is started

环境变量,优先级类,最小和最大的工作空间限制,和最少一个线程执行

with a single thread, often called the primary thread, but can create additional threads from any of its threads.

每个进程启动的时候,会自动启动一个线程,第一个线程就是主线程,但是创建一个额外的线程。

进程与线程的区别?

【进程和线程没有可比性,进程只是资源的集合。】

启动一个线程要快于进程。启动一个进程相当于要建一个房间,启动一个线程相当于拉一个人进屋。

  1. Threads share the address space of the process that created it; processes have their own address space.线程个共享内存空间,进程是独立的
  2. Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.线程共享数据的时候是一份数据,进程之间共享数据是进行了拷贝,比如两个子房间,共享数据是完全复制了一份。
  3. Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.线程可以直接与进程的其他线程通信;进程必须使用进程间通信与同级进程通信
  4. New threads are easily created; new processes require duplication of the parent process.一个新的线程较容易创建,一个新进程需要拷贝一份数据
  5. Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.线程可以对相同进程的线程进行相当大的控制;进程只能对子进程进行控制。
  6. Changes to the main thread (cancellation, priority change, etc.) may affect the behavior行为 of the other threads of the process; changes to the parent process does not affect child processes.对主线程的更改(取消、优先级更改等)可能会影响进程中其他线程的行为;对父进程的更改不会影响子进程。
#!/usr/bin/env python
# Author:Zhangmingda
import time,threading
'''常用多线程写法:for循环'''
def run(n):
    print('task:',n)
    time.sleep(2)

t_obj = []
'''每个线程启动后就直接启动下一个线程,不等上一个线程的执行结果,
为了获取每个线程执行的结果(等待线程执行结束),可以用.join(),'''
start_time = time.time()

for i in range(50):
    t = threading.Thread(target=run,args=(i,))
    t.start() #循环启动所有线程
    t_obj.append(t) #将每个线程对象保存下来
for i in t_obj: #对每个线程对象进行循环等待结束
    i.join()
print('cost_time(花费时间):',time.time() - start_time)
# t1 = threading.Thread(target=run,args=('t1',))
# t2 = threading.Thread(target=run,args=('t2',))
# t1.start()
# t2.start()
study_threading
#!/usr/bin/env python
# Author:Zhangmingda
import time,threading

 '''面向对象方式写法'''
 class Mythread(threading.Thread):
     def __init__(self,n):
         super(Mythread,self).__init__()
         self.n = n
     def run(self):
         print('running:',self.n)

 t1 = Mythread('t1')
 t2 = Mythread('t2')
 t1.start()
 t2.start()
多线程面向对象写法
C:\Users\Administrator\Desktop\Python3_study\venv\Scripts\python.exe C:/Users/Administrator/Desktop/Python3_study/day9/study_threading.py
task: 0
task: 1
task: 2
task: 3
task: 4
task: 5
task: 6
task: 7
task: 8
task: 9
task: 10
task: 11
task: 12
task: 13
task: 14
task: 15
task: 16
task: 17
task: 18
task: 19
task: 20
task: 21
task: 22
task: 23
task: 24
task: 25
task: 26
task: 27
task: 28
task: 29
task: 30
task: 31
task: 32
task: 33
task: 34
task: 35
task: 36
task: 37
task: 38
task: 39
task: 40
task: 41
task: 42
task: 43
task: 44
task: 45
task: 46
task: 47
task: 48
task: 49
cost_time(花费时间): 2.011706590652466
study_threading执行结果
#!/usr/bin/env python
# Author:Zhangmingda
'''面向对象方式写法'''
import threading,time

class Mythread(threading.Thread):
    def __init__(self,n):
        super(Mythread,self).__init__()
        self.n = n
    def run(self):
        print('running:',self.n)
        time.sleep(1)

t_obj = []
start_time = time.time()
for i in range(50):
    t = Mythread(i)
    t.start()
    t_obj.append(t)
for i in t_obj:
    i.join()
print('running OVER:time:',time.time() - start_time)
面向对象的for循环并发
C:\Users\Administrator\Desktop\Python3_study\venv\Scripts\python.exe C:/Users/Administrator/Desktop/Python3_study/day9/threading面向对象写法.py
running: 0
running: 1
running: 2
running: 3
running: 4
running: 5
running: 6
running: 7
running: 8
running: 9
running: 10
running: 11
running: 12
running: 13
running: 14
running: 15
running: 16
running: 17
running: 18
running: 19
running: 20
running: 21
running: 22
running: 23
running: 24
running: 25
running: 26
running: 27
running: 28
running: 29
running: 30
running: 31
running: 32
running: 33
running: 34
running: 35
running: 36
running: 37
running: 38
running: 39
running: 40
running: 41
running: 42
running: 43
running: 44
running: 45
running: 46
running: 47
running: 48
running: 49
running OVER:time: 1.008784294128418

Process finished with exit code 0
面向对象的for循环并发执行结果

猜你喜欢

转载自www.cnblogs.com/zhangmingda/p/9240134.html