concurrent programming, processes

Daemon:

What is a daemon? The daemon is like a eunuch, always guarding the emperor. When the emperor dies, he will be buried with him.

Guaranteed to follow the main process for a lifetime, if the main process dies, all will die

from multiprocessing import Process


def run(name):
    print('%s is runing'%name)

if __name__ == '__main__':
    p=Process(target=run,args=('young friend',))
    # p.daemon = True
    p.start()
    print( ' Main ===" ' )#Main === "
                    #young friend is runing


def run(name):
    print('%s is runing'%name)

if __name__ == '__main__':
    p=Process(target=run,args=('young friend',))
    p.daemon = True
    p.start()
    print( ' Main ===" ' )#Main ==="

Mutex:

When multiple child processes are born, which child process grabs the lock first and which child process can execute first (this lock can only be used by one person at a time, and it will be automatically released after the process ends)

from multiprocessing import Process,Lock
#Use join to implement serialization
def task1():
    print( ' Task one, name: egon ' )
    print( ' Task one, age, 18 ' )
    print( ' Task one, gender: male ' )

def task2():
    print( ' Task 2, name: alex ' )
    print( ' Task two, age, 78 ' )
    print( ' Task 2, gender: male ' )


def task3():
    print( ' Task three, name: lxx ' )
    print( ' task three, age, 38 ' )
    print( ' Task three, gender: male ' )


if __name__ == '__main__':
    p1=Process(target=task1)
    p2 = Process(target=task2)
    p3 = Process(target=task3)

    p1.start()
    p1.join()
    p2.start()
    p2.join()
    p3.start()
    p3.join()



# Implement serialization with mutex
mutex=Lock()
def task1(Lock):
    Lock.acquire()
    print( ' Task one, name: egon ' )
    print( ' Task one, age, 18 ' )
    print( ' Task one, gender: male ' )
    Lock.release()

def task2(Lock):
    Lock.acquire()
    print( ' Task 2, name: alex ' )
    print( ' Task two, age, 78 ' )
    print( ' Task 2, gender: male ' )
    Lock.release()


def task3(Lock):
    Lock.acquire()
    print( ' Task three, name: lxx ' )
    print( ' task three, age, 38 ' )
    print( ' Task three, gender: male ' )
    Lock.release()


if __name__ == '__main__':
    p1=Process(target=task1,args=(mutex,))
    p2 = Process(target=task2,args=(mutex,))
    p3 = Process(target=task3,args=(mutex,))

    p1.start()
    p2.start()
    p3.start()

The difference between mutex and join

Major premise: The principle of both is the same, both turn concurrency into serial, so as to ensure order

Difference: join is executed in an artificially specified order, while mutex is a process of equal competition, and whoever grabs the first one executes

     Mutexes can serialize part of code (code that modifies shared data), while join can only serialize code as a whole

Simulated ticketing system

from multiprocessing import Process,Lock
import time,random,json,os

x=Lock()

def select():
    time.sleep (random.randint ( 1 , 3 ))
    dic=json.load(open('a.txt','r'))
    print( ' %s votes still have %s remaining ' %(os.getpid(),dic[ ' count ' ]))


def get():
    dic = json.load(open('a.txt', 'r'))
    if dic['count']>0:
       dic['count']-=1
       time.sleep(random.randint(1, 3))
       json.dump(dic,open('a.txt','w'))
       print( ' %s successfully grabbed the ticket ' % os.getpid())


def lock_(Lock):
    select()
    Lock.acquire()
    get()
    Lock.release()

if __name__ == '__main__':
    for i in range(10):
        p=Process(target=lock_,args=(x,))
        p.start()

ICP communication mechanism:

Communication between processes must find a medium, which must satisfy,

1. is shared by all processes

2. Must be memory space

from multiprocessing import Queue

# Alignment:
# 1. Shared space
# 2 , is the memory space
# 3. Automatically help us deal with locking problems
q=Queue(3)
q.put('first')
q.put({'second':None})
q.put('')

# q.put( 4 ) #blocking
print(q.get())
print(q.get())
print(q.get())

emphasize:

1. The queue is used to store the information communicated between processes, and the amount of data should not be too large

2. The memory limit that the value of maxsize exceeds becomes meaningless

Producer-Consumer Model
This model contains two important roles:
1. Producer: The task responsible for creating data is likened to the producer
2. Consumer: Receive the data created by the producer for further processing. Characters are likened to consumers


 Implement the three elements of the producer-consumer model:
1, producer
2, consumer
3, queue

 When to use this model:
There are two obvious types of tasks in the program, one task is responsible for production, and the other task is responsible for processing production data.

 The benefits of this model:
 1. Realize the decoupling of producers and consumers and
 2. Balance productivity and consumption, that is, producers can keep producing and consumers can keep processing, because the two
are no longer directly communicate, but communicate with the queue

import time
import random
from multiprocessing import Process,Queue

def consumer(name,q):
    while True:
        res=q.get()
        time.sleep(random.randint(1,3))
        print('\033[46m消费者===》%s 吃了 %s\033[0m' %(name,res))


def producer(name,q,food):
    for i in range(5):
        time.sleep(random.randint(1,2))
        res='%s%s' %(food,i)
        q.put(res)
        print('\033[45m生产者者===》%s 生产了 %s\033[0m' %(name,res))


if __name__ == '__main__':
    #1、共享的盆
    q=Queue()

    #2、生产者们
    p1=Process(target=producer,args=('egon',q,'包子'))
    p2=Process(target=producer,args=('刘清政',q,'泔水'))
    p3=Process(target=producer,args=('杨军',q,'米饭'))

    #3、消费者们
    c1=Process(target=consumer,args=('alex',q))
    c2=Process(target=consumer,args=('梁书东',q))


    p1.start()
    p2.start()
    p3.start()
    c1.start()
    c2.start()

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324860578&siteId=291194637