Online learning notes in epidemic environment python 4.22

4.22

class schedule

  1. Concurrent programming 3 ~ 4 days
  2. Project 2 ~ 3 days
  3. MySQL ~ 5 days
  4. Frontend ~ 7 days
  5. django framework ~ 10 days
  6. bbs project 3 ~ 5 days
  7. Code release ~ 5 days
  8. cmdb ~ 5 days

Learning suggestions

  1. Learn to choose, no need to delve into all knowledge points
  2. Develop self-solving skills

Today's content

  1. Operating system history
  2. Multi-channel technology
  3. Process theory
  4. Two ways to start the process
  5. Join method of process object
  6. Data isolation between processes
  7. Other methods of process objects
  8. Zombie process and orphan process
  9. Daemon
  10. Mutex

Operating system history

  • Manual operation: punch card

    At the same time, only one person in the computer room operates, and the CPU waits for manual operation, and the resource utilization rate is low

  • Online batch processing system

    Saves the waiting time for each user to insert a card

  • Offline batch processing system

    Improve the speed of data reading from hard disk to memory, reduce the CPU waiting time

Multi-channel technology

  • Concurrency

    It looks like running concurrently can be called concurrent

  • parallel

    The simultaneous execution in the true sense is also concurrent

    Single-core computers certainly cannot achieve parallelism, but they can achieve concurrency

    We directly assume that a single core is a core, and there is only one person working

Multi-channel technology: saves the total time spent running multiple programs

Key knowledge

Spatial multiplexing and temporal multiplexing

  • Spatial reuse: multiple programs share a set of computer hardware

  • Reuse in time:

    Example: Washing clothes for 50 minutes, cooking for 20 minutes, and boiling water for 5 minutes. . .

    Single channel execution requires the running time of the sum of all times, and multiple channels only need the time with the longest time

Switch program + save state

切换分为两种情况
  - 当一个程序遇到IO操作系统的时候,操作系统会剥夺该程序的CPU执行权限:提高了cpu的利用率,并且也不影响程序的执行效率
  - 当一个程序长时间占用cpu的时候,操作系统也会剥夺该程序的cpu执行权限:降低了程序的执行效率

Process theory

The difference between program and process

  • The program is a bunch of code on the hard disk, it is dead
  • Process represents the process that the program is executing in memory and is alive

Process scheduling

  • First-come-first-served scheduling algorithm: good for long jobs and bad for short jobs

    Now this algorithm has been eliminated

  • Short job priority scheduling algorithm: good for short jobs, bad for long jobs

  • Time slice rotation method: when you want to open a new program, it will stop the currently executing first to open your new program to him

Three state diagram of process running

All programs must go through the ready state before they can be executed

img

Event request includes input, print, open

Blocking state to ready state: input gets the value, file reading is completed, and timesleep ends

img

Two pairs of important concepts

Synchronous and asynchronous

Describes how tasks are submitted

  • Synchronization: After the task is submitted, it waits for the return result of the task in situ. Nothing is done during the waiting process. The feeling at the program level is that it is stuck.

  • Asynchronous: after the task is submitted, it does not wait for the return result of the task in place, and directly does other things, waiting for the return result of the task

    The return result of the task will have an asynchronous callback mechanism

Blocking and non-blocking

Describes the running state of the program

  • Blocking: blocking state
  • Non-blocking: ready state, running state

The most efficient combination is asynchronous + non-blocking

Ideal state: we should keep the code we write in a ready state and a running state without blocking

Two ways to start the process

The way the code starts the process and the thread, the code writing is basically the same, learn how to start the process and learn how to start the thread

The first way

p = Proccess(target=task, args=('deimos',))

from multiprocessing import Process
import time
def task(name):
	print('%s is running'%name)
	time.sleep(2)
	print('%s is over'%name)
	
if __name__ == '__main__':
	# 1. 创建一个对象
	p = Proccess(target=task, args=('deimos',))
	# 2. 开启进程
	p.start() # 告诉操作系统要创建一个进程
	print('main')
	# 运行,则创建两个进程,一个是task,一个是main

Under the windows operating system, the creation process must be created in the main , because the creation process under windows is similar to the way of module import, it will be executed from top to bottom

The second way: class inheritance

from multiprocessing import Process
import time
class MyProcess(Process):
	def run(self):
		print('sleep')
		time.sleep(1)
		print('get up')
		
if __name__ == '__main__':
	p = Myprocess()
	p.start()
	print('main')

The first way is used more

to sum up

To create a process is to apply for a piece of memory space in memory and throw the code that needs to be run. A process corresponds to an independent memory space in memory, and multiple processes correspond to multiple independent memory spaces

The data between processes cannot be directly interacted with by default. If you want to interact, you can use other tools and modules.

join method

After the main process waits for the end of the child process, it will continue to execute

The result is to turn asynchronous into synchronous

from multiprocessing import Process
import time


def task(name, n):
    print('%s is running'%name)
    time.sleep(n)
    print('%s is over'%name)


if __name__ == '__main__':
    # p1 = Process(target=task, args=('jason', 1))
    # p2 = Process(target=task, args=('egon', 2))
    # p3 = Process(target=task, args=('tank', 3))
    # start_time = time.time()
    # p1.start()
    # p2.start()
    # p3.start()  # 仅仅是告诉操作系统要创建进程
    # # time.sleep(50000000000000000000)
    # # p.join()  # 主进程等待子进程p运行结束之后再继续往后执行
    # p1.join()
    # p2.join()
    # p3.join()
    start_time = time.time()
    p_list = []
    for i in range(1, 4):
        p = Process(target=task, args=('子进程%s'%i, i))
        p.start()
        p_list.append(p)
    for p in p_list:
        p.join()
    print('主', time.time() - start_time)

Data isolation between processes

Guess you like

Origin www.cnblogs.com/telecasterfanclub/p/12753162.html