Introduction Process


1. Process description
Process: After a program up and running, the code uses the + resource called the process, he is the smallest unit of the operating system resource allocation
process can also be done multi-task.

2. Process difference with thread:
1. process does not share global variables, each carved out a piece of memory space, more memory resources
2. Thread shared global variable, less resource-intensive
3. A thread is part of the process.

3.multiprocessing module
target if you pass a function reference, you can task the child process to execute code here
args parameter of the function passed to target specific to the ancestral way to pass
kwargs named parameters passed to target farmers in Anhui Province designated
name to the process of setting a name, you can not set the
group specified process group, less than the most

commonly used method instance object process created:
start () promoter process
is_alive () to determine whether the child was still alive
join () if the end of the wait for the child to perform, or how many seconds to wait for
terminate () regardless of whether the task is completed, immediately terminate the child process

common attributes process to create an instance of an object:
the alias name of the current process
pid pid the current process

4. Inter-process communication - Queue
multiprocessing.Queue () and queue.Queue () difference
1.queue.Queue () is in the process of blocking queue
2.multiprocessing.Queue () is an inter-process communication queue
3. The former is a multi-process each private, which is the total of each process
between 4.Process sometimes need to communicate operating system provides a number of mechanisms to achieve inter-process communication

example:
from multiprocessing Import process, Queue

def work1(q):
while q.qsize()>0:
print(q.get())

def work2(q):
while q.qsize()>0:
print(q.get())

if __name__ == '__main__':
Note: To create a queue In the following, if you can not share global variables in the above path-breaker.
= Queue Q ()
for I in Range (10):
q.put ( "http://www.baidu.com")

p = Process(target=work1,args=(q,))
p1 = Process(target=work2, args=(q,))

p.start()
p1.start()

5. Process pond
initialization Pool, you can specify a maximum number of processes, when a new request to submit the Pool, if the pool is
not full, so long to create a new process for executing the request; but if the pool the number of processes has reached the specified maximum
request will wait until the end of the process there is the pool, have used the process leading up to perform a new task.

pool common method

apply_async () invoke the use of non-Cypriot rent FUNC
close () close the pool
terminate () regardless of whether the task is completed immediately terminate
join () main process blocks, waiting for the child process exits, must be used after the close or terminate

example:

import os
import time
from multiprocessing import Process,Queue,Manager,Pool

WORK1 DEF ():
  Print (. "{} process pool ---" the format (os.getpid ()))
  the time.sleep (. 1)

the __name__ == IF '__main__':
P = Pool (. 3)
for I in Range (10):
  p.apply_async (WORK1)

p.close ()
p.join ()

6. The process pool Queue
If creation process Pool , it is necessary to use multiprocessing.Manager (Queue) in ()

example:
Import OS
Import Time
from Import Pool multiprocessing, Manager


def work1(q):
  print("进程池---{}".format(os.getpid()))
  print(q.get())
  time.sleep(1)

if __name__ == '__main__':
Manager()
q = Manager().Queue()
for i in range(10):
  q.put("你好啊")

p = Pool(3)
for i in range(10):
  p.apply_async(work1)

p.close()
p.join()

Guess you like

Origin www.cnblogs.com/666666pingzi/p/10993949.html