亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

????

Python? ?? ???? ??? ?? ?? ???? ????. ?? ?? CPU? ???? ??? ????? Python?? ???? ?? ?? ????? ???? ???. Python? ?? ???? ?? ?? ?? ???? ?????. ??? ???? Python? ??? ?? ??? ?????. ? ???? ???? ?? ?????? ?? ????? ??? ?? ??? ? ????. ??????? ?? ????, ?? ? ??? ??? ???? ??? ??? ???? ???? ????, ?, ??? ? ??? ?? ?? ??? ?????.

1. ???? ???

????? ???? ???: Process([group [, target [, name [, args [, kwargs]]]])

target? ?? ??? ?????.

args? ?????. ?? ?? ?? ???? ??

kwargs? ?? ??? ??? ?????.

name? ?????.

group? ????? ???? ????.

??? ???? ?? ?? ????? ???? ?? ???????.

#!/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, '兩點(diǎn)水1'))
    p2 = multiprocessing.Process(target=worker, args=(3, '兩點(diǎn)水2'))
    p3 = multiprocessing.Process(target=worker, args=(4, '兩點(diǎn)水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!!!!!!!!!!!!!!!!!")

?? ??:

?? ???? ?? ??

2. ????? ???? ??

?? ???? p ??? ???? ?? ?? ?? ???? ????? ??? ?? ????( ), run() ???? ???? ?????.

# -*- 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("當(dāng)前時(shí)間: {0}".format(time.ctime()))
            time.sleep(self.interval)
            n -= 1
if __name__ == '__main__':
    p = ClockProcess(3)
    p.start()

?? ??? ??? ????.

Create process class

3.Daemon attribute

daemon ??? ??? ????? ?? ??? ?? ? ?? ?? ?????. daemon ??? ??? ?? ?? ?? ?? ?? ??:

deamon ??? ???? ?? ?:

# -*- coding: UTF-8 -*-
import multiprocessing
import time
def worker(interval):
    print('工作開始時(shí)間:{0}'.format(time.ctime()))
    time.sleep(interval)
    print('工作結(jié)果時(shí)間:{0}'.format(time.ctime()))
if __name__ == '__main__':
    p = multiprocessing.Process(target=worker, args=(3,))
    p.start()
    print('【EMD】')

?? ??:

【EMD】
工作開始時(shí)間:Mon Oct  9 17:47:06 2017
工作結(jié)果時(shí)間:Mon Oct  9 17:47:09 2017

? ??? ???? p? ?? ??? ?????:

# -*- coding: UTF-8 -*-
import multiprocessing
import time
def worker(interval):
    print('工作開始時(shí)間:{0}'.format(time.ctime()))
    time.sleep(interval)
    print('工作結(jié)果時(shí)間:{0}'.format(time.ctime()))
if __name__ == '__main__':
    p = multiprocessing.Process(target=worker, args=(3,))
    p.daemon = True
    p.start()
    print('【EMD】')

?? ??:

【EMD】

It ?? ??? ?? ? ? ???, ?? ????? daemon ??? ???? ?? ????? ???? ?? ????? ?????. ??? ?? ????? ?? ??? ???? ????.

4. ?? ??

?? ?? ???? ?? ???? ??? ???? ??? ?? ????

?? ?? ?? ???? ??? ? ????. ?? ???? ?? ??? ?? ???? ???? ????? ??? ?? ??? ?? ????? ??? ?? ?? ????? ?? ???? ????.

??? ?? ???? ???? ?? ???:

import multiprocessing
import time
def worker(interval):
    print('工作開始時(shí)間:{0}'.format(time.ctime()))
    time.sleep(interval)
    print('工作結(jié)果時(shí)間:{0}'.format(time.ctime()))
if __name__ == '__main__':
    p = multiprocessing.Process(target=worker, args=(3,))
    p.daemon = True
    p.start()
    p.join()
    print('【EMD】')

?? ??:

工作開始時(shí)間:Tue Oct 10 11:30:08 2017
工作結(jié)果時(shí)間:Tue Oct 10 11:30:11 2017
【EMD】

5, Pool

?? ????? ?? ??? ?? ??? ???? ????

?? ??? ????. ???? ? ??? ???? ????? ?? ????? ??? ? ????.

??? ??? ????.

# -*- coding: UTF-8 -*-
from multiprocessing import Pool
import os, time, random
def long_time_task(name):
    print('進(jìn)程的名稱:{0} ;進(jìn)程的PID: {1} '.format(name, os.getpid()))
    start = time.time()
    time.sleep(random.random() * 3)
    end = time.time()
    print('進(jìn)程 {0} 運(yùn)行了 {1} 秒'.format(name, (end - start)))
if __name__ == '__main__':
    print('主進(jìn)程的 PID:{0}'.format(os.getpid()))
    p = Pool(4)
    for i in range(6):
        p.apply_async(long_time_task, args=(i,))
    p.close()
    # 等待所有子進(jìn)程結(jié)束后在關(guān)閉主進(jìn)程
    p.join()
    print('【End】')

?? ??? ??? ????.

主進(jìn)程的 PID:7256
進(jìn)程的名稱:0 ;進(jìn)程的PID: 1492
進(jìn)程的名稱:1 ;進(jìn)程的PID: 12232
進(jìn)程的名稱:2 ;進(jìn)程的PID: 4332
進(jìn)程的名稱:3 ;進(jìn)程的PID: 11604
進(jìn)程 2 運(yùn)行了 0.6500370502471924 秒
進(jìn)程的名稱:4 ;進(jìn)程的PID: 4332
進(jìn)程 1 運(yùn)行了 1.0830621719360352 秒
進(jìn)程的名稱:5 ;進(jìn)程的PID: 12232
進(jìn)程 5 運(yùn)行了 0.029001712799072266 秒
進(jìn)程 4 運(yùn)行了 0.9720554351806641 秒
進(jìn)程 0 運(yùn)行了 2.3181326389312744 秒
進(jìn)程 3 運(yùn)行了 2.5331451892852783 秒
【End】

??? ??? ?? ? ?? ????. Pool ??? Join() ???? ???? ?? ?? ????? ??? ??? ??? ?????. ??? Join()? ???? ?? close()? ???? ???. close()? ??? ??? ???? ? ????? ??? ? ????.

?? ??? ?????. ?? ???? 0, 1, 2, 3? ?? ???? ??, ?? ???? 4? ???? ?? ?? ?? ????? ??? ??? ???? ???. ?? Pool? ?? ?? ?????. ? ????? 4?? ???? ?? 4?? ????? ??? ??? ? ????. ?? ?? ??? ??? ?? ?? ???? ?? ?????.

p = Pool(5)

? ???? ??? 5?? ????? ??? ? ????.

6. ???? ? ??

????? ??? ???? ???. ?? ??? ???? ? ??? ???? ?? ?? ????? ?????. Python? ?? ?? ??? ?? ????? ???? ??? ??? ?? ? ? ???? ?? ?? ???? ?????.

???? ?? ?? ?? ????? ? ?? ?? ????? ????. ??? ???? ???? ?? ?? ??? ????? ???? ????.

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
from multiprocessing import Process, Queue
import os, time, random
def write(q):
    # 寫數(shù)據(jù)進(jìn)程
    print('寫進(jìn)程的PID:{0}'.format(os.getpid()))
    for value in ['兩點(diǎn)水', '三點(diǎn)水', '四點(diǎn)水']:
        print('寫進(jìn) Queue 的值為:{0}'.format(value))
        q.put(value)
        time.sleep(random.random())
def read(q):
    # 讀取數(shù)據(jù)進(jìn)程
    print('讀進(jìn)程的PID:{0}'.format(os.getpid()))
    while True:
        value = q.get(True)
        print('從 Queue 讀取的值為:{0}'.format(value))
if __name__ == '__main__':
    # 父進(jìn)程創(chuàng)建 Queue,并傳給各個(gè)子進(jìn)程
    q = Queue()
    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))
    # 啟動(dòng)子進(jìn)程 pw
    pw.start()
    # 啟動(dòng)子進(jìn)程pr
    pr.start()
    # 等待pw結(jié)束:
    pw.join()
    # pr 進(jìn)程里是死循環(huán),無(wú)法等待其結(jié)束,只能強(qiáng)行終止
    pr.terminate()

?? ??? ??? ????.

讀進(jìn)程的PID:13208
寫進(jìn)程的PID:10864
寫進(jìn) Queue 的值為:兩點(diǎn)水
從 Queue 讀取的值為:兩點(diǎn)水
寫進(jìn) Queue 的值為:三點(diǎn)水
從 Queue 讀取的值為:三點(diǎn)水
寫進(jìn) Queue 的值為:四點(diǎn)水
從 Queue 讀取的值為:四點(diǎn)水
???? ??