Multitasking programming python

Operating efficiency full use of computer resources to enhance program: The Meaning

Definition: The purpose of simultaneous execution of multiple tasks through the use of multiple cores computer applications, computer time improving operational efficiency.

Embodiment: multi-process multi-threaded

Parallel: multiple computer cores to handle multiple tasks, this time among multiple tasks in parallel relationship.

Concurrency: handle multiple tasks simultaneously, the kernel constantly switch between multiple tasks, to the effect seems to have run in the process. However, the actual point in time kernel can only handle one task.

Process (Process)

Definition: once the program is running in the computer

Program: is an executable file, static occupy disk space, do not possess the resources to run a computer

Process: The process is described in a dynamic process, possession of computer resources, there is a certain life cycle

  • Different processes running the same program are different processes, resource-intensive and life cycle are not the same.

Create a process flow
1. The user space by running the program or call interface to initiate the process of creating
2 operating system accepts a user request to start the process of creating
3 operating system to allocate computer resources to determine the process status, open up the process space, etc.
4. Operating system Creating good processes to the application use

cpu time slice

If a process occupies a central computer, called the reform process we occupy computer cpu time slice.

  • The relationship between multiple tasks is to fight for the cpu
  • Cpu who occupy the final decision is the operating system

PCB (process control block)
an open space in memory for recording information of a process

  • Process control block is the operating system looks for a sign of recognition process

Process information: ps -aux

PID (process ID): the operating system each process has a unique ID number to distinguish it from other processes. ID numbers automatically assigned by the operating system, is an integer greater than 0

Parent and child: in addition to the system initialization process, each process has a parent process, there may be zero or more child processes. Thereby forming a parent and child relationship.

See the process tree: pstree
View parent PID: ps -ajx

Status of the process

Tristate
* ready state: Process capable of performing the condition, wait for the system to allocate resources
* Running state: Process occupy cpu running
* wait state: the process of being executed does not have the condition, block waiting to meet the conditions before execution

Five-state (tri-state based on the addition of new state, termination state)
* New state: to create a new process, the process of obtaining resources
* termination of state: the end of the process execution, resource release recycling process

ps aux -> STAT

S wait states (wait interruptible)
D wait states (wait uninterruptible)
T wait state (suspended state)
R & lt operating mode (containing the Ready state)
the Z zombie

<High-priority process
N low priority
l have subprocess
s session group leader

  • Foreground process

Process priority

Role: determines the execution of a process and the degree of authority preempt resources

View process priority
top view dynamic process information system, with <> page
ranges -20--19-20 highest priority

Use to run the program specified priority
nice: priority designation operation

e.g.   nice -9  ./while.py   以优先级9运行
       nice --9  ./while.py  以-9优先级运行

Process features

  1. Running independently of each other independently run between processes
  2. The process is the smallest unit of the operating system resource allocation
  3. Each independent process space each occupies a certain virtual memory

Claim:

  1. What is the difference between the process, processes and procedures
  2. Understand the process characteristics
  3. Clear the relationship between the conversion process for each state, and the state

Multi-process programming

import  os  

pid = os.fork()
功能 : 创建新的进程
参数 : 无
返回值: 失败返回一个负数
成功 : 在原有进程中返回新的进程的PID号,在新的进程中返回0
  • The child will copy all the code segment of the parent process, the memory space generated before including fork
  • A child process begins execution under the fork, do not interfere with the parent process
  • The execution order is not necessarily the parent and child, the parent and child to share a common terminal display
  • According to fork parent and child will usually return is worth the difference choose to perform a different code. So if the structure is almost fixed with fork
  • Parent and child space independent, content is this space operations, independently of each other
  • The child also has its own characteristics, such as PID number, PCB, command sets, etc.

Processes related functions

Get Process PID
os.getpid ()
function: Get the current process of process ID
Return Value: Returns the process ID

os.getppid ()
function: Get the current process of the parent process PID number
Return Value: Returns the process ID

Process exit

os._exit (status)
Function: Process Exit
parameters: exit status of the process

sys.exit ([status])
Function: Process Exit
parameters: number indicates exit status, do not write the default is 0
string representing the content printed on exit

  • sys.exit can capture SystemExit prevent abnormal exit

Orphan processes: the parent before the child process exits, the child process at this time is called an orphan process.

  • Orphan adoption process is operating system-specific processes, system processes become orphaned new parent

Zombie process: the child before the parent process to exit, but the parent process does not deal with the exit status of the child, then the child will become a zombie process.

  • PCB zombie process will remain a small amount of information in memory, a large number of zombie process consumes system resources and should be avoided produce zombie process

How to avoid the zombie process produces
* handling child process exit status
pid, status = os.wait ()
function: in the parent process child process exits blocked pending
the return value: PID pid number of child processes exit
status to obtain child process exit status

pid,status = os.waitpid(pid,option)
功能 :在父进程中阻塞等待处理子进程退出
参数 : pid  -1 表示等待任意子进程退出
             >0 表示等待对应PID号的子进程退出
	option  0 表示阻塞等待
	        WNOHANG 表示非阻塞
    返回值: pid 退出的子进程的PID号
         status  获取子进程退出状态

waitpid(-1,0)  ===> wait()

* 让父进程先退出
   1. 父进程创建子进程等待子进程退出
   2. 子进程创建二级子进程后立即退出
   3. 二级子进程称为孤儿,和原来的父进程各自执行事件

multiprocessing module creation process

  1. Things need to be performed as a function of the package
  2. The process of creating objects using multiprocessing module Process class
  3. Function set the course set by the initialization function object properties and Process of binding to be performed
  4. Start the process, the process is performed automatically binding function
  5. Complete the process of recovery
Process()
功能 : 创建进程对象
参数 : name  进程名称  Process-1
        target  绑定函数 
	args  元组  给target函数按照位置传参
        kwargs  字典  给target函数按照键值对传参

p.start()
功能:启动进程
* target函数会自动执行,此时进程真正被创建

p.join([timeout])
功能 : 阻塞等待回收子进程
参数 : 超时时间

* 使用multiprocessing创建子进程,同样子进程复制父进程的全部代码段,
* 父子进程各自执行互不影响,父子进程有各自的运行空间

* 如果不使用join回收子进程则子进程退出后会成为僵尸进程
* 使用multiprocessing创建子进程往往父进程只是用来创建进程回收进程

Process进程对象属性

p.start()
p.join()

p.is_alive()  
判断进程生命周期状态,处于生命周期得到True否则返回False

p.name 进程名称 默认为Process-1
p.pid  进程的PID号

p.daemon 
默认状态False  主进程退出不会影响子进程执行
如果设置为True 则子进程会随着主进程结束而结束

* 要在start前设置
* 一般不和join一起使用

Create a custom class process

  1. Inheritance Process
  2. Write your own __init__, while loading the parent class init method
  3. Rewriting run method, you can call start operation automatically generated by the object

Pros: You can use multi-core computer, concurrent tasks, and improve the efficiency of the independent space, data from other processes affect the safe operation, easy to create
disadvantages: the creation and deletion process consumes more system resources

Process pool technique

Reason: If there is a large number of tasks required to complete the multi-process, you may need to create frequent removal process, to bring more into the calculation of resource consumption.

Principle: Create the appropriate process into the process pool to handle the pending event, after processing process is not destroyed, are still waiting for other events in the process pool. The multiplexing process reduces the consumption of resources

Instructions

  1. Create a process pool, put the appropriate processes in the pool
  2. The event is added to the process pool wait queue
  3. Continue to take the process execution events until all events finished
  4. Pool closed process, recycling process
from multipeocessing import  Pool


Pool(processes)
功能 : 创建进程池对象
参数 :表示进程池中有多少进程

pool.apply_async(func,args,kwds)
功能 : 将事件放入到进程池队列
参数 : func 事件函数
        args 以元组形式给func传参
	kwds 以字典形式给func传参
返回值 : 返回一个代表进程池事件的对象

pool.apply(func,args,kwds)
功能 : 将事件放入到进程池队列
参数 : func 事件函数
        args 以元组形式给func传参
	kwds 以字典形式给func传参

pool.close()
功能: 关闭进程池

pool.join()
功能:回收进程池

pool.map(func,iter)
功能: 将要做的时间放入进程池
参数: func  要执行的函数
       iter  迭代对象
返回值 : 返回事件函数的返回值列表

Interprocess communication (IPC)

Reason: The process is relatively independent of space, resources can not get to each other, then the need for specialized communication methods between different processes.

Interprocess communication method: the signal conduit message queue shared memory
semaphore socket

Communication Pipe conduit

Communication Theory: open up space in the memory pipeline, pipeline operations generated objects, multiple processes using the "same" pipe object to operate the communication can be realized

multiprocessing ---》 Pipe

fd1,fd2 = Pipe(duplex = True)
功能 : 创建管道
参数 : 默认表示双向管道
        如果设置为False则为单向管道
返回值 : 表示管道的两端
          如果是双向管道 都可以读写
	  如果是单向管道 则fd1只读 fd2只写

fd.recv()
功能 : 从管道读取信息
返回值: 读取到的内容

* 如果管道为空则阻塞

fd.send(data)
功能:向管道写入内容
参数: 要写入的内容
* 可以发送python数据类型

message queue

Queue: FIFO
Communication Theory: establishing a queue data structure model in memory. Can be stored in a plurality of processes by contents of the queue, and stored in the order taken out sequentially consistent

创建队列
q = Queue(maxsize = 0)
功能 : 创建消息队列
参数 : 表示最多存放多少消息。默认表示根据内存分配存          储
返回值 : 队列对象

q.put(data,[block,timeout])
功能: 向队列存储消息
参数 :data 要存的内容
       block 默认队列满时会阻塞,设置为False则非阻塞
       timeout 超时时间

data = q.get([block,timeout])
功能:获取队列消息
参数:block 默认队列空时会阻塞,设置为False则非阻塞
      timeout 超时时间
返回值 : 返回取出的内容

q.full()   判断队列是否为满
q.empty()  判断队列是否为空
q.qsize()  判断队列中消息数量
q.close()  关闭队列

Shared memory

Communication Theory: in memory to open up an empty space, visible to multiple processes, the process can write input, but the content will overwrite the contents of each write before.

obj = Value(ctype,obj)
功能 : 开辟共享内存空间
参数 : ctype  要存储的数据类型
        obj  共享内存的初始化数据
返回 :共享内存对象

obj.value 即为共享内存值,对其修改即修改共享内存

obj = Array(ctype,obj)
功能 : 开辟共享内存空间
参数 : ctype  要存储的数据格式
        obj  初始化存入的内容 比如列表,字符串
	     如果是整数则表示开辟空间的个数
返回值 : 返回共享内存对象
         * 可以通过遍历过户每个元素的值
	   e.g.  [1,2,3]  ---> obj[1] == 2
         * 如果存入的是字符串
	   obj.value 表示字符串的首地址
       	  管道         消息队列       共享内存
开辟空间   内存         内存           内存

读写方式   两端读写     先进先出       覆盖之前内容
           双向/单向

效率       一般          一般          较高

应用       多用于父     广泛灵活       需要注意
           子进程                      进行互斥操作

Signal communication

A process sends a signal to another process to convey some message recipient corresponding behavior according to the received signal

kill -l to view the system signal
kill -sig PID sends a signal to a process

About Signal
Signal Name Signal Meaning default processing method

Disconnect SIGHUP
SIGINT CTRU-C
a SIGQUIT CTRU-
the SIGTSTP the CTRL-the Z
the SIGKILL to terminate a process
SIGSTOP a pause process
SIGALRM clock signal
to the parent process issues a sub-process state change SIGCHLD

python 发送信号

signal  

os.kill(pid,sig)
功能: 发送信号
参数: pid 目标进程
       sig 要发送的信号
import signal

signal.alarm(sec)
功能 : 向自身发送时钟信号 --》 SIGALRM
参数 : sec  时钟时间

* 进程中只能有一个时钟,第二个会覆盖第一个时间

同步执行 : 按照顺序逐句执行,一步完成再做下一步
异步执行 : 在执行过程中利用内核记录延迟发生或者准备             处理的事件。这样不影响应用层的持续执行。             当事件发生时再由内核告知应用层处理

* 信号是唯一的异步通信方法

signal.pause()
功能:阻塞等待接收一个信号

signal.signal(signum,handler)
功能: 处理信号
参数: signum  要处理的信号
       handler 信号的处理方法 
           SIG_DFL  表示使用默认的方法处理
	   SIG_IGN  表示忽略这个信号
	   func     传入一个函数表示用指定函数处理
		def func(sig,frame)
		    sig: 捕获到的信号
		    frame : 信号对象

Semaphore (lights)

Principle: Given a number of multiple processes visible, and multiple processes can operate. The implementation of their actions through the process to determine the number of how many.

multiprocessing --》 Semaphore()

sem = Semaphore(num)
功能: 创建信号量
参数: 信号量初始值
返回: 信号量对象

sem.get_value()  获取信号量值
sem.acquire() 将信号量减1  当信号量为0会阻塞
sem.release() 将信号量加1

进程的同步互斥

临界资源 :多个进程或者线程都能够操作的共享资源
临界区 : 操作临界资源的代码段

同步 : 同步是一种合作关系,为完成某个任务,多进程或者多线程之间形成一种协调,按照约定或条件执行操作临界资源。

互斥 : 互斥是一种制约关系,当一个进程或者线程使用临界资源时进行上锁处理,当另一个进程使用时会阻塞等待,直到解锁后才能继续使用。

Event  事件

multiprocessing  --》 Event

创建事件对象
e = Event()

设置事件阻塞
e.wait([timeout])

事件设置 当事件被设置后e.wait()不再阻塞
e.set()

清除设置 当事件设置被clear后 e.wait又会阻塞
e.clear()

事件状态判断
e.is_set()



Lock 锁

创建对象
lock = Lock() 

lock.acquire() 上锁  如果锁已经是上锁状态调用此函数会阻塞

lock.release() 解锁

with lock:   上锁
   ....
   ....
              解锁

Thread

Thread is a multi-tasking programming, you can use multi-core computer resources to complete the concurrent execution of the program. Also known as the thread lightweight process.

Thread features

  • The smallest unit of threaded computing multi-core distribution
  • A process can contain multiple threads
  • Resources and space is also a thread running process, consume computer resources, multiple threads share the process of
  • Create Delete thread resources consumed should be much smaller than the process
  • Interference between multiple execution threads
  • Thread also has its own unique attributes, such as instruction set ID
threading 模块创建线程

threading.Thread()
功能 : 创建线程对象
参数 :name  线程名称  默认 Thread-1 
       target  线程函数 
       args  元组   给线程函数位置传参
       kwargs  字典  给线程函数键值传参
 
t.start()  启动线程 自动运行线程函数
t.join([timeout])  回收线程


线程对象属性

t.is_alive()  查看线程状态
t.name  线程名称
t.setName()  设置线程名称
t.getName()  获取线程名称
threading.currentThread()  获取当前线程对象

t.daemon 属性
默认情况主线程退出不会影响分支线程执行
如果设置为True 则分支线程随主线程退出

设置方法:
t.daenon = True
t.setDaemon(True)

判断属性值
t.isDaemon()

* 要在start前设置,不会和join同用

创建自己的线程类
步骤:
1.继承Thread
2.加载Thread中的__init__
3.重写run方法

Thread communication

通信方法: 多个线程共享进程的空间,所以线程间通            信使用全局变量完成。

注意事项: 线程间使用全局变量往往要同步互斥机制            保证通信安全

Mutex thread synchronization method

线程的event
e = threading.Event()  创建事件对象
e.wait([timeout])  如果e为设置状态则不阻塞否则阻塞
e.set()  将e变为设置状态
e.clear()  清除设置

线程锁
lock = threading.Lock()  创建锁对象
lock.acquire()  上锁
lock.release()  解锁

* 也可以通过with上锁,上锁状态调用acquire会阻塞

Multithreading

threading multi-threaded

Comparative more complicated process:
* consume fewer resources
* thread should pay more attention to the operation of shared resources
* It should be noted that in python GIL problems, high network latency, thread concurrency is also a viable option

Implementation steps
1. create a socket and bind monitor
2 receives the client request, creates a new thread
3. The main thread continues to receive other client connection
4. Start the branch corresponding thread function processing client requests
5. When a client disconnected, the branch end of the thread

cookie

import traceback

traceback.print_exc()
功能 : 更详细的打印异常信息


集成模块的使用
python3 socketserver

功能 : 通过模块的不同类的组合完成多进程/多线程 的           tcp/udp的并发

StreamRequestHandler  处理tcp套接字请求
DatagramRequestHandler  处理udp套接字请求

TCPServer  创建tcp server
UDPServer  创建udp server

ForkingMixIn   创建多进程
ForkingTCPServer -->  ForkingMinIn + TCPServer
ForkingUDPServer -->  ForkingMinIn + UDPServer

ThreadingMixIn  创建多线程
ThreadingTCPServer --> ThreadingMinIn + TCPServer
ThreadingUDPServer --> ThreadingMinIn + UDPServer
 

HTTPServer V2.0

  1. Receiving a client request
  2. To resolve the client request
  3. Organizational data, is formed HTTP response
  4. It sends the data to the client

upgrade

  1. Concurrently receiving a plurality of multi-threaded client requests
  2. Basic request resolution, returns the corresponding content according to the request
  3. In addition to requests for static pages, or simple data request
  4. The function encapsulated in a class

Technical point:

  1. socket tcp socket
  2. Http protocol request response format
  3. Create a method of thread concurrency
  4. The basic use of class

Coroutine basis

Definition: Fibers, micro-threads. Coroutine is essentially a single-threaded program, it can not be used coroutine multicore computer resources.

Role: concurrent tasks can be completed efficiently, uses less resources. Thus higher concurrency coroutine

Principle: context stack area by the recording layer is applied to achieve a context jump operation, the operation may be selectively achieved partial want to run, in order to improve the efficiency of the program.

Advantages: less consumption of resources
without switching overhead
without synchronizing mutually exclusive
IO concurrency good

Disadvantages: can not take advantage of multi-core computer

yield - "Basic keyword coroutines implemented

greenlet

greenlet.greenlet()  生成协程对象
gr.switch() 选择要执行的协程事件

gevent

1. 将协程事件封装为函数
2. 生成协程对象
  gevent.spawn(func,argv)
  功能 : 生成协程对象
  参数 : func  协程函数
          argv  给协程函数传参
  返回值 : 返回协程对象

3.回收协程
  gevent.joinall()
  功能 : 回收协程
  参数: 列表 将要回收的协程放入列表

 gevent.sleep(n)
 功能: 设置协程阻塞,让协程跳转
 参数: n  阻塞时间

from gevent import monkey
monkey.patch_all()
功能: 修改套接字的IO阻塞行为

* 必须在socket导入之前使用
Published 49 original articles · won praise 6 · views 5003

Guess you like

Origin blog.csdn.net/qq_43959027/article/details/102991346