快捷搜索:
来自 计算机编程 2019-08-03 21:15 的文章
当前位置: 67677新澳门手机版 > 计算机编程 > 正文

Python多进度编制程序,python多进度总计

序. multiprocessing
python中的二十四线程其实并非真的的十二线程,假使想要足够地运用多核CPU的财富,在python中山高校部分气象需求利用多进度。Python提供了那些好用的多进度包multiprocessing,只需求定义多少个函数,Python会实现别的兼具专门的学业。借助这么些包,能够轻便做到从单进度到出现实践的转移。multiprocessing扶助子进程、通讯和分享数据、实施分歧式的共同,提供了Process、Queue、Pipe、Lock等零件。

翻阅目录

 

    1. Process
    1. Lock
    1. Semaphore
    1. Event
    1. Queue
    1. Pipe
    1. Pool

1. Process

创造进度的类:Process([group [, target [, name [, args [, kwargs]]]]]),target表示调用对象,args表示调用对象的职位参数元组。kwargs表示调用对象的字典。name为外号。group实质上不行使。
方法:is_alive()、join([timeout])、run()、start()、terminate()。在那之中,Process以start()运行某些进程。

属性:authkey、daemon(要由此start()设置)、exitcode(进度在运作时为None、借使为–N,表示被实信号N甘休)、name、pid。当中daemon是父进程终止后活动停止,且本人不能够发出新历程,必须在start()此前设置。

 

例1.1:创设函数并将其看成单个进度

import multiprocessing
import time

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

if __name__ == "__main__":
    p = multiprocessing.Process(target = worker, args = (3,))
    p.start()
    print "p.pid:", p.pid
    print "p.name:", p.name
    print "p.is_alive:", p.is_alive()

结果

p.pid: 8736
p.name: Process-1
p.is_alive: True
The time is Tue Apr 21 20:55:12 2015
The time is Tue Apr 21 20:55:15 2015
The time is Tue Apr 21 20:55:18 2015
The time is Tue Apr 21 20:55:21 2015
The time is Tue Apr 21 20:55:24 2015

 

例1.2:创立函数并将其看做四个经过

import multiprocessing
import time

def worker_1(interval):
    print "worker_1"
    time.sleep(interval)
    print "end worker_1"

def worker_2(interval):
    print "worker_2"
    time.sleep(interval)
    print "end worker_2"

def worker_3(interval):
    print "worker_3"
    time.sleep(interval)
    print "end worker_3"

if __name__ == "__main__":
    p1 = multiprocessing.Process(target = worker_1, args = (2,))
    p2 = multiprocessing.Process(target = worker_2, args = (3,))
    p3 = multiprocessing.Process(target = worker_3, args = (4,))

    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!!!!!!!!!!!!!!!!!"

结果

The number of CPU is:4
child   p.name:Process-3 p.id7992
child   p.name:Process-2 p.id4204
child   p.name:Process-1 p.id6380
END!!!!!!!!!!!!!!!!!
worker_1
worker_3
worker_2
end worker_1
end worker_2
end worker_3

 

例1.3:将经过定义为类

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("the time is {0}".format(time.ctime()))
            time.sleep(self.interval)
            n -= 1

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

:进度p调用start()时,自动调用run()

结果

the time is Tue Apr 21 20:31:30 2015
the time is Tue Apr 21 20:31:33 2015
the time is Tue Apr 21 20:31:36 2015
the time is Tue Apr 21 20:31:39 2015
the time is Tue Apr 21 20:31:42 2015

 

例1.4:daemon程序比较结果

#1.4-1 不加daemon属性

import multiprocessing
import time

def worker(interval):
    print("work start:{0}".format(time.ctime()));
    time.sleep(interval)
    print("work end:{0}".format(time.ctime()));

if __name__ == "__main__":
    p = multiprocessing.Process(target = worker, args = (3,))
    p.start()
    print "end!"

结果

end!
work start:Tue Apr 21 21:29:10 2015
work end:Tue Apr 21 21:29:13 2015

#1.4-2 加上daemon属性

import multiprocessing
import time

def worker(interval):
    print("work start:{0}".format(time.ctime()));
    time.sleep(interval)
    print("work end:{0}".format(time.ctime()));

if __name__ == "__main__":
    p = multiprocessing.Process(target = worker, args = (3,))
    p.daemon = True
    p.start()
    print "end!"

结果

end!

:因子进程设置了daemon属性,主进度停止,它们就趁早停止了。

#1.4-3 设置daemon实行完结束的格局

import multiprocessing
import time

def worker(interval):
    print("work start:{0}".format(time.ctime()));
    time.sleep(interval)
    print("work end:{0}".format(time.ctime()));

if __name__ == "__main__":
    p = multiprocessing.Process(target = worker, args = (3,))
    p.daemon = True
    p.start()
    p.join()
    print "end!"

结果

work start:Tue Apr 21 22:16:32 2015
work end:Tue Apr 21 22:16:35 2015
end!

 

序. multiprocessing
python中的二十四线程其实并非真的的十二线程,尽管想要丰硕地使用多核CPU的能源,在python中多数情景须要利用多进度。Python提供了分外好用的多进度包multiprocessing,只必要定义三个函数,Python会完毕别的兼具事情。借助那几个包,能够轻易实现从单进度到并发试行的调换。multiprocessing辅助子进度、通讯和分享数据、施行不一式样的联合,提供了Process、Queue、Pipe、Lock等零件。

2. Lock

当多少个经过供给访谈分享能源的时候,Lock能够用来制止访谈的争持。

import multiprocessing
import sys

def worker_with(lock, f):
    with lock:
        fs = open(f, 'a ')
        n = 10
        while n > 1:
            fs.write("Lockd acquired via withn")
            n -= 1
        fs.close()

def worker_no_with(lock, f):
    lock.acquire()
    try:
        fs = open(f, 'a ')
        n = 10
        while n > 1:
            fs.write("Lock acquired directlyn")
            n -= 1
        fs.close()
    finally:
        lock.release()

if __name__ == "__main__":
    lock = multiprocessing.Lock()
    f = "file.txt"
    w = multiprocessing.Process(target = worker_with, args=(lock, f))
    nw = multiprocessing.Process(target = worker_no_with, args=(lock, f))
    w.start()
    nw.start()
    print "end"

结果(输出文件)

Lockd acquired via with
Lockd acquired via with
Lockd acquired via with
Lockd acquired via with
Lockd acquired via with
Lockd acquired via with
Lockd acquired via with
Lockd acquired via with
Lockd acquired via with
Lock acquired directly
Lock acquired directly
Lock acquired directly
Lock acquired directly
Lock acquired directly
Lock acquired directly
Lock acquired directly
Lock acquired directly
Lock acquired directly

 

 

3. Semaphore

Semaphore用来支配成对分享财富的访问数量,比如池的最亚松森接数。

import multiprocessing
import time

def worker(s, i):
    s.acquire()
    print(multiprocessing.current_process().name   "acquire");
    time.sleep(i)
    print(multiprocessing.current_process().name   "releasen");
    s.release()

if __name__ == "__main__":
    s = multiprocessing.Semaphore(2)
    for i in range(5):
        p = multiprocessing.Process(target = worker, args=(s, i*2))
        p.start()

结果

Process-1acquire
Process-1release

Process-2acquire
Process-3acquire
Process-2release

Process-5acquire
Process-3release

Process-4acquire
Process-5release

Process-4release

 

4. Event

伊芙nt用来落到实处进程间协同通讯。

import multiprocessing
import time

def wait_for_event(e):
    print("wait_for_event: starting")
    e.wait()
    print("wairt_for_event: e.is_set()->"   str(e.is_set()))

def wait_for_event_timeout(e, t):
    print("wait_for_event_timeout:starting")
    e.wait(t)
    print("wait_for_event_timeout:e.is_set->"   str(e.is_set()))

if __name__ == "__main__":
    e = multiprocessing.Event()
    w1 = multiprocessing.Process(name = "block",
            target = wait_for_event,
            args = (e,))

    w2 = multiprocessing.Process(name = "non-block",
            target = wait_for_event_timeout,
            args = (e, 2))
    w1.start()
    w2.start()

    time.sleep(3)

    e.set()
    print("main: event is set")

结果

wait_for_event: starting
wait_for_event_timeout:starting
wait_for_event_timeout:e.is_set->False
main: event is set
wairt_for_event: e.is_set()->True

 

1. Process

成立进度的类:Process([group [, target [, name [, args [, kwargs]]]]]),

  target表示调用对象,

  args表示调用对象的任务参数元组。

  kwargs表示调用对象的字典。

  name为别名。

  group实质上不采用。
方法:is_alive()、join([timeout])、run()、start()、terminate()。

  个中,Process以start()运行有个别进程。

属性:authkey、daemon(要经过start()设置)、exitcode(进程在运营时为None、假使为–N,表示被复信号N甘休)、name、pid。

  在那之中daemon是父进度终止后自行终止,且本身无法生出新进程,必须在start()在此之前安装。

 

67677新澳门手机版 ,例1.1:创造函数并将其看成单个进度

67677新澳门手机版 1

import multiprocessing
import time

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

if __name__ == "__main__":
    p = multiprocessing.Process(target = worker, args = (3,))
    p.start()
    print("p.pid:", p.pid)
    print("p.name:", p.name)
    print("p.is_alive:", p.is_alive())

67677新澳门手机版 2

结果

1
2
3
4
5
6
7
8
p.pid: 8736
p.name: Process-1
p.is_alive: True
The time is Tue Apr 21 20:55:12 2015
The time is Tue Apr 21 20:55:15 2015
The time is Tue Apr 21 20:55:18 2015
The time is Tue Apr 21 20:55:21 2015
The time is Tue Apr 21 20:55:24 2015

 

例1.2:创立函数并将其用作多个进程

67677新澳门手机版 3

import multiprocessing
import time

def worker_1(interval):
    print("worker_1")
    time.sleep(interval)
    print("end worker_1")

def worker_2(interval):
    print("worker_2")
    time.sleep(interval)
    print("end worker_2")

def worker_3(interval):
    print("worker_3")
    time.sleep(interval)
    print("end worker_3")

if __name__ == "__main__":
    p1 = multiprocessing.Process(target = worker_1, args = (2,))
    p2 = multiprocessing.Process(target = worker_2, args = (3,))
    p3 = multiprocessing.Process(target = worker_3, args = (4,))

    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!!!!!!!!!!!!!!!!!")

67677新澳门手机版 4

结果

1
2
3
4
5
6
7
8
9
10
11
The number of CPU is:4
child   p.name:Process-3    p.id7992
child   p.name:Process-2    p.id4204
child   p.name:Process-1    p.id6380
END!!!!!!!!!!!!!!!!!
worker_1
worker_3
worker_2
end worker_1
end worker_2
end worker_3

 

例1.3:将经过定义为类

67677新澳门手机版 5

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("the time is {0}".format(time.ctime()))
            time.sleep(self.interval)
            n -= 1

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

67677新澳门手机版 6

:进度p调用start()时,自动调用run()

结果

1
2
3
4
5
the time is Tue Apr 21 20:31:30 2015
the time is Tue Apr 21 20:31:33 2015
the time is Tue Apr 21 20:31:36 2015
the time is Tue Apr 21 20:31:39 2015
the time is Tue Apr 21 20:31:42 2015

 

例1.4:daemon程序比较结果

#1.4-1 不加daemon属性

67677新澳门手机版 7

import multiprocessing
import time

def worker(interval):
    print("work start:{0}".format(time.ctime()));
    time.sleep(interval)
    print("work end:{0}".format(time.ctime()));

if __name__ == "__main__":
    p = multiprocessing.Process(target = worker, args = (3,))
    p.start()
    print("end!")

67677新澳门手机版 8

结果

1
2
3
end!
work start:Tue Apr 21 21:29:10 2015
work end:Tue Apr 21 21:29:13 2015

#1.4-2 加上daemon属性

67677新澳门手机版 9

import multiprocessing
import time

def worker(interval):
    print("work start:{0}".format(time.ctime()));
    time.sleep(interval)
    print("work end:{0}".format(time.ctime()));

if __name__ == "__main__":
    p = multiprocessing.Process(target = worker, args = (3,))
    p.daemon = True
    p.start()
    print("end!")

67677新澳门手机版 10

结果

1
end!

:因子进度设置了daemon属性,主进度甘休,它们就趁着停止了。

#1.4-3 设置daemon实施完截止的诀窍

67677新澳门手机版 11

import multiprocessing
import time

def worker(interval):
    print("work start:{0}".format(time.ctime()));
    time.sleep(interval)
    print("work end:{0}".format(time.ctime()));

if __name__ == "__main__":
    p = multiprocessing.Process(target = worker, args = (3,))
    p.daemon = True
    p.start()
    p.join()
    print("end!")

67677新澳门手机版 12

结果

1
2
3
work start:Tue Apr 21 22:16:32 2015
work end:Tue Apr 21 22:16:35 2015
end!

 

本文由67677新澳门手机版发布于计算机编程,转载请注明出处:Python多进度编制程序,python多进度总计

关键词: