[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/Lovepython015/Python/master/Article/PythonBasis/python13/3.md [Back]  [Original]

#  #

Python  CPU  Python 

Python  multiprocessingPython 

multiprocessing  ProcessQueuePipeLock 


## 1 Process ##

`Process([group [, target [, name [, args [, kwargs]]]]])`

* target 
* args 
* kwargs
* name
* group



```python
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import multiprocessing
import time


def worker(interval, name):
    print(name + 'start')
    time.sleep(interval)
    print(name + 'end')


if __name__ == "__main__":
    p1 = multiprocessing.Process(target=worker, args=(2, '1'))
    p2 = multiprocessing.Process(target=worker, args=(3, '2'))
    p3 = multiprocessing.Process(target=worker, args=(4, '3'))

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

    print("The number of CPU is:" + str(multiprocessing.cpu_count()))
    for p in multiprocessing.active_children():
        print("child   p.name:" + p.name + "\tp.id" + str(p.pid))
    print("END!!!!!!!!!!!!!!!!!")

```



![](http://twowaterimage.oss-cn-beijing.aliyuncs.com/2019-10-14-%E5%A4%9A%E8%BF%9B%E7%A8%8B%E8%BE%93%E5%87%BA%E7%BB%93%E6%9E%9C.gif)


## 2 ##

 p  start()  run() 


```python
# -*- coding: UTF-8 -*-

import multiprocessing
import time


class ClockProcess(multiprocessing.Process):
    def __init__(self, interval):
        multiprocessing.Process.__init__(self)
        self.interval = interval

    def run(self):
        n = 5
        while n > 0:
            print(": {0}".format(time.ctime()))
            time.sleep(self.interval)
            n -= 1


if __name__ == '__main__':
    p = ClockProcess(3)
    p.start()

```



![](http://twowaterimage.oss-cn-beijing.aliyuncs.com/2019-10-14-%E5%88%9B%E5%BB%BA%E8%BF%9B%E7%A8%8B%E7%B1%BB.gif)

## 3daemon  ##

 daemon  daemon 

 deamon 

```python
# -*- coding: UTF-8 -*-
import multiprocessing
import time


def worker(interval):
    print('{0}'.format(time.ctime()))
    time.sleep(interval)
    print('{0}'.format(time.ctime()))


if __name__ == '__main__':
    p = multiprocessing.Process(target=worker, args=(3,))
    p.start()
    print('EMD')

```



```txt
EMD
Mon Oct  9 17:47:06 2017
Mon Oct  9 17:47:09 2017
```

 p  daemon 

```python
# -*- coding: UTF-8 -*-

import multiprocessing
import time


def worker(interval):
    print('{0}'.format(time.ctime()))
    time.sleep(interval)
    print('{0}'.format(time.ctime()))


if __name__ == '__main__':
    p = multiprocessing.Process(target=worker, args=(3,))
    p.daemon = True
    p.start()
    print('EMD')
```



```txt
EMD
```


 daemon 


## 4join  ##



 join join  join 

 join 

```python
import multiprocessing
import time


def worker(interval):
    print('{0}'.format(time.ctime()))
    time.sleep(interval)
    print('{0}'.format(time.ctime()))


if __name__ == '__main__':
    p = multiprocessing.Process(target=worker, args=(3,))
    p.daemon = True
    p.start()
    p.join()
    print('EMD')
```



```txt
Tue Oct 10 11:30:08 2017
Tue Oct 10 11:30:11 2017
EMD
```

## 5Pool ##







```python
# -*- coding: UTF-8 -*-

from multiprocessing import Pool
import os, time, random


def long_time_task(name):
    print('{0} PID: {1} '.format(name, os.getpid()))
    start = time.time()
    time.sleep(random.random() * 3)
    end = time.time()
    print(' {0}  {1} '.format(name, (end - start)))


if __name__ == '__main__':
    print(' PID{0}'.format(os.getpid()))
    p = Pool(4)
    for i in range(6):
        p.apply_async(long_time_task, args=(i,))
    p.close()
    # 
    p.join()
    print('End')
```



```txt
 PID7256
0 PID: 1492
1 PID: 12232
2 PID: 4332
3 PID: 11604
 2  0.6500370502471924 
4 PID: 4332
 1  1.0830621719360352 
5 PID: 12232
 5  0.029001712799072266 
 4  0.9720554351806641 
 0  2.3181326389312744 
 3  2.5331451892852783 
End
```

 `Pool`  `join()`  `join()`  `close()` `close()`  Process 

 0123 4  Pool  4 4  Pool 

```python
p = Pool(5)
```

 5 


## 6 ##

Process Python  multiprocessing QueuePipes 

 Queue  Queue  Queue 

```python
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

from multiprocessing import Process, Queue
import os, time, random


def write(q):
    # 
    print('PID:{0}'.format(os.getpid()))
    for value in ['', '', '']:
        print(' Queue {0}'.format(value))
        q.put(value)
        time.sleep(random.random())


def read(q):
    # 
    print('PID:{0}'.format(os.getpid()))
    while True:
        value = q.get(True)
        print(' Queue {0}'.format(value))


if __name__ == '__main__':
    #  Queue
    q = Queue()
    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))
    #  pw
    pw.start()
    # pr
    pr.start()
    # pw:
    pw.join()
    # pr 
    pr.terminate()

```



```txt
PID:13208
PID:10864
 Queue 
 Queue 
 Queue 
 Queue 
 Queue 
 Queue 
```



Web Proxy Viewer  |  New URL  |  Original Page