mutilprocess usage
Process class introduced
Use the Process Class objects generated
Call the object's methods
start()
Open sub-process
join()
Waiting for the child process to finish and then execute the following code
pid and ppid
View pid
- Import current_process
- current_process().pid
- Check the child process pid property
- p.pid
- Import os
- os.get_pid()
View ppid
- Import os
- os.get_ppid()
os methods can view the current process and the parent process pid in any process
Daemon
process(daemon= true)
When the last line of the parent process, daemon to execute no matter what the place will end
PS: The last line of code is the parent process, rather than when the program finishes running of the parent process
terminate()
Instruction to the operating system sends an interrupt program
is_alived
The return value is of type bool
Determine whether the process is still running
name
Check the name of the process
Typically Process-1 [-2,] and the like, meaningless
JoinableQueue
It differs from the Queue in more than a join method
Recognize join method
- Task_done called once every come up with a data () method
- If there is data in the current queue
- will join up here until the queue is empty of data, the program will continue
Producer consumer model
from multiprocessing import Queue,Process
def producer(q,food,name):
for i in range(10):
res = f"{name}生产{food},编号:{i}"
q.put(res)
print(res)
q.put('')
def consumer(q,name):
while True:
res = q.get()
if not res :break
print(f"{name}吃了{res}")
if __name__ == '__main__':
q = Queue(5)
p1 = Process(target=producer,args=(q,'cake','mark'))
p2 = Process(target=producer,args=(q,'mike','nick'))
c1 = Process(target=consumer,args=(q,'jiangheng'))
c2 = Process(target=consumer,args=(q,'yanzhibing'))
c3 = Process(target=consumer,args=(q,'yanzhibing'))
p1.start()
p2.start()
c1.start()
c2.start()
q.put(None)
c3.start()
Producers
- Production Data
Buffer
- Manufacturer data into the buffer,
the consumer is removed from the data buffer
consumer
- Data processing
advantage
- Decoupling
- Support concurrent
- Support busy uneven
- Producer consumer model, greatly improving production efficiency producer, but also greatly enhance the efficiency of consumer spending.
queue
Pipeline + Lock
- Pipeline: Based on the underlying shared memory
- Lock: Lock process
q=Queue(n)
- Generating a queue object, set the queue size
q.put()
- Any data type into the
q.get()
- The first queue data fetch
The default parameters put / get the
- block
- The default is true
- When the queue is full, it will then put into a wait state
- When the queue is empty, and then you will get into a wait state
- When is False
- When the queue is full, then put wrong will throw
- When the queue is empty, and then get the wrong will throw
- The default is true
- timeout
- Only when the block is True, it makes sense
- Set the duration of the wait state,
beyond being given the same time
Process lock
from multiprocessing import Lock
- Import Lock
lock = Lock()
- Defined lock object
lock.acquire()
- Lock for your particular part of the code
- Note that, when the lock is locked, other processes can not run the code in this section
lock.release()
- Unlock
- Only unlocked, other processes can grab the lock to continue to run their own code
Note: In multi-process is to ensure that multiple processes are using the same lock
XMind: ZEN - Trial Version