python 并行计算 pathos模块 简介

2023-05-16

目录

pathos模块

1、pathos自身的多进程方法(pathos.multiprocessing.ProcessPool、pathos.multiprocessing.ProcessingPool、pathos.pools.ProcessPool)

2、映射multiprocess模块的多进程方法(pathos.multiprocessing.Pool)

3、映射pp模块的多进程方法1(pathos.pools.ParallelPool、pathos.pp.ParallelPool、pathos.pp.ParallelPythonPool、pathos.parallel.ParallelPythonPool、pathos.parallel.ParallelPool)

4、映射pp模块的多进程方法2(pathos.pp.pp模块)

5、映射python内置map函数的方法(pathos.serial.SerialPool、pathos.pools.SerialPool)

实例(pathos模块)

(1)pathos.multiprocessing.ProcessPool(),pipe方法

(2)pathos.multiprocessing.ProcessPool(),apipe方法

(3)pathos.multiprocessing.ProcessPool(),map方法

(4)pathos.multiprocessing.ProcessPool(),imap方法

(5)pathos.multiprocessing.ProcessPool(),uimap方法

(6)pathos.multiprocessing.ProcessPool(),amap方法

(7)pathos.pp.ParallelPool(),pipe方法

(8)pathos.pp.ParallelPool(),apipe方法

(9)pathos.pp.ParallelPool(),map方法

(10)pathos.pp.ParallelPool(),amap方法

(11)pathos.pp.ParallelPool(),imap方法

(12)pathos.pp.ParallelPool(),uimap方法


pathos模块

pathos是一个较为综合性的模块,既能多进程,也能多线程。其主要采用进程池/线程池方法。

pathos本身有一套进程池方法,同时也集成了multiprocess、pp模块的进程池方法。

1、pathos自身的多进程方法(pathos.multiprocessing.ProcessPool、pathos.multiprocessing.ProcessingPool、pathos.pools.ProcessPool)

(1)建立进程池

pathos.multiprocessing.ProcessPool(*args, **kwds) #建立pathos的进程池(pathos.multiprocessing.ProcessPool实例)。

pathos.multiprocessing.ProcessingPool(*args, **kwds) #同上。

pathos.pools.ProcessPool(*args, **kwds) #同上。

nodes:workers的数量。如果不指定nodes,则自动检测processors的数量(即ncpus)。
ncpus:worker processors的数量。
servers:worker servers的列表。
scheduler:相应的scheduler。
workdir:用于scratch calculations/files的$WORKDIR。
scatter:如为True,表示采用scatter-gatter(默认为worker-pool)。
source:如为False,表示尽可能少使用TemporaryFiles。
timeout:等待scheduler返回值的时间。

同样也有几个进程池通用的方法:

XXX.close() #关闭进程池,关闭后不能往pool中增加新的子进程,然后可以调用join()函数等待已有子进程执行完毕。XXX为进程池。

XXX.join() #等待进程池中的子进程执行完毕。需在close()函数后调用。XXX为进程池。

def f(a, b = value):
    pass

pool = pathos.multiprocessing.Pool() 
pool.map(f, a_seq, b_seq)
pool.close()
pool.join()

(2)创建子进程

(a)单个子进程可通过pipe方法创建:

XXX.pipe(f, *args, **kwds) #采用阻塞方式(非并行)提交一个任务,阻塞直到返回结果为止。XXX为进程池实例。

XXX.apipe(f, *args, **kwds) #异步(并行)提交一个任务到队列(queue)中,返回ApplyResult实例(其get方法可获得任务返回值,但get方法是阻塞的,应在所有子进程添加完后再调用)。XXX为进程池实例。

f(*args,**kwds)为子进程对应的活动。

(b)如果子进程有返回值,且返回值需要集中处理,则建议采用map方式(子进程活动允许多个参数)

XXX.map(f, *args, **kwds) #采用阻塞方式按顺序运行一批任务,返回结果组成的list。func(iterable1[i], iterable2[i], ...)为子进程对应的活动。XXX为进程池实例。

XXX.amap(f, *args, **kwds) #XXX.map()的异步(并行)版本,返回MapResult实例(其具有get()方法,获取结果组成的list)。XXX为进程池实例。

def f(a, b): #map方法允许多个参数
    pass

pool = pathos.multiprocessing.Pool() 
result = pool.map_async(f, (a0, a1, ...), (b0, b1, ...)).get()
pool.close()
pool.join()

(c)如果内存不够用,也可采用imap迭代器方式

XXX.imap(f, *args, **kwds) #XXX.map()的非阻塞、按顺序迭代器版本,返回迭代器实例。XXX为进程池实例。

XXX.uimap(f, *args, **kwds) #XXX.imap()的无序版本(不会按照调用顺序返回,而是按照结束顺序返回),返回迭代器实例。XXX为进程池实例。

def f(a, b): 
    pass

pool = pathos.multiprocessing.Pool() 
result = pool.uimap(f, a_seq, b_seq)
pool.close()
pool.join()

for item in result:
    pass

2、映射multiprocess模块的多进程方法(pathos.multiprocessing.Pool)

(1)建立进程池

pathos.multiprocessing.Pool(processes=None, initializer=None, initargs=(), maxtasksperchild=None, context=None) #建立multiprocess的进程池。

processes :使用的工作进程的数量,如果processes是None那么使用 os.cpu_count()返回的数量。
initializer: 如果initializer不是None,那么每一个工作进程在开始的时候会调用initializer(*initargs)。
maxtasksperchild:工作进程退出之前可以完成的任务数,完成后用一个新的工作进程来替代原进程,来让闲置的资源被释放。maxtasksperchild默认是None,意味着只要Pool存在工作进程就会一直存活。
context: 用在制定工作进程启动时的上下文,一般使用 multiprocess.Pool() 或者一个context对象的Pool()方法来创建一个池,两种方法都适当的设置了context。

(2)创建子进程

该进程池对应的创建子进程方法与multiprocess.Pool()(也即multiprocessing.Pool())完全相同。

3、映射pp模块的多进程方法1(pathos.pools.ParallelPool、pathos.pp.ParallelPool、pathos.pp.ParallelPythonPool、pathos.parallel.ParallelPythonPool、pathos.parallel.ParallelPool)

(1)建立进程池

pathos.pp.ParallelPool(*args, **kwds) #建立映射pp模块方法的进程池,返回pathos.parallel.ParallelPool实例。注意,建立的进程池的方法与pp模块完全不同

pathos.pp.ParallelPythonPool(*args, **kwds) #等价pathos.pp.ParallelPool()。

pathos.pools.ParallelPool(*args, **kwds) #等价pathos.pp.ParallelPool()。

pathos.parallel.ParallelPool(*args, **kwds) #等价pathos.pp.ParallelPool()。

pathos.parallel.ParallelPythonPool(*args, **kwds) #等价pathos.pp.ParallelPool()。

nodes:workers的数量。如果不指定nodes,则自动检测processors的数量(即ncpus)。
ncpus:worker processors的数量。
servers:worker servers的列表。
scheduler:相应的scheduler。
workdir:用于scratch calculations/files的$WORKDIR。
scatter:如为True,表示采用scatter-gatter(默认为worker-pool)。
source:如为False,表示尽可能少使用TemporaryFiles。
timeout:等待scheduler返回值的时间。

(2)创建子进程

该进程池对应的创建子进程方法与pathos.multiprocessing.ProcessPool()完全相同(与pp模块完全不同)。

注意,multiprocessing.Pipe()或multiprocess.Pipe()建立的管道对象无法传入子进程(可能是pickle错误)。但是,ParallelPool进程池中,子进程print函数可以直接输出到标准输出,因此也不必通过管道将信息传递到主进程了。但是,子进程print输出的格式经常出现异常,最好还是通过返回值在主进程输出

而且,amap方法是个特例。amap方法中,如果子进程有print语句,会导致返回结果不对,只包含最后一个子进程返回值的tuple,而不是所有子进程的返回值组成完整list,原因暂不清楚。因此,amap方法中,子进程需要输出的内容只能通过返回值在主进程输出。

4、映射pp模块的多进程方法2(pathos.pp.pp模块)

该方法实质就是pp模块。

5、映射python内置map函数的方法(pathos.serial.SerialPool、pathos.pools.SerialPool)

该类方法实际是串行(非并行),不做具体介绍。

SerialPool建立的进程池实际只能用pipe、map、imap方法(均是阻塞的),不能使用apipe、amap、uimap方法。

实例(pathos模块)

(1)pathos.multiprocessing.ProcessPool(),pipe方法

import pathos
import multiprocess
import time

def f(x, conn, t0):
    ans = 1
    x0 = x
    t = time.time() - t0
    conn.send('factorial of %d: start@%.2fs' % (x0, t))
    while x > 1:
        ans *= x
        time.sleep(0.5)
        x -= 1
    t = time.time() - t0
    conn.send('factorial of %d: finish@%.2fs, res = %d' %(x0, t, ans))
    return ans

def main():
    res = []
    var = (4, 8, 12, 20, 16)
    p = pathos.multiprocessing.ProcessPool()
    p_conn, c_conn = multiprocess.Pipe()
    t0 = time.time()
    for i in var:
        res.append(p.pipe(f, i, c_conn, t0))

    print('output:')
    while p_conn.poll():
        print(p_conn.recv())
    t = time.time() - t0
    print('factorial of %s@%.2fs: %s' % (var, t, res))

if __name__ == '__main__':
    main()

结果:可以看出,所有子进程都是逐个执行的。

 output:
factorial of 4: start@1.11s
factorial of 4: finish@2.61s, res = 24
factorial of 8: start@2.61s
factorial of 8: finish@6.12s, res = 40320
factorial of 12: start@6.12s
factorial of 12: finish@11.62s, res = 479001600
factorial of 20: start@11.63s
factorial of 20: finish@21.13s, res = 2432902008176640000
factorial of 16: start@21.15s
factorial of 16: finish@28.65s, res = 20922789888000
factorial of (4, 8, 12, 20, 16)@28.73s: [24, 40320, 479001600, 2432902008176640000, 20922789888000]

(2)pathos.multiprocessing.ProcessPool(),apipe方法

import pathos
import multiprocess
import time

def f(x, conn, t0):
    ans = 1
    x0 = x
    t = time.time() - t0
    conn.send('factorial of %d: start@%.2fs' % (x0, t))
    while x > 1:
        ans *= x
        time.sleep(0.5)
        x -= 1
    t = time.time() - t0
    conn.send('factorial of %d: finish@%.2fs, res = %d' %(x0, t, ans))
    return ans

def main():
    res = []
    var = (4, 8, 12, 20, 16)
    p = pathos.multiprocessing.ProcessPool()
    p_conn, c_conn = multiprocess.Pipe()
    t0 = time.time()
    for i in var:
        res.append(p.apipe(f, i, c_conn, t0))
    for i in range(len(res)):
        res[i] = res[i].get()

    print('output:')
    while p_conn.poll():
        print(p_conn.recv())
    t = time.time() - t0
    print('factorial of %s@%.2fs: %s' % (var, t, res))

if __name__ == '__main__':
    main()

结果:

output:
factorial of 4: start@1.10s
factorial of 8: start@1.18s
factorial of 12: start@1.19s
factorial of 20: start@1.25s
factorial of 4: finish@2.60s, res = 24
factorial of 16: start@2.61s
factorial of 8: finish@4.69s, res = 40320
factorial of 12: finish@6.69s, res = 479001600
factorial of 16: finish@10.11s, res = 20922789888000
factorial of 20: finish@10.75s, res = 2432902008176640000
factorial of (4, 8, 12, 20, 16)@10.85s: [24, 40320, 479001600, 2432902008176640000, 20922789888000]

(3)pathos.multiprocessing.ProcessPool(),map方法

注意,实例将multiprocessing.Pipe()创建的连接作为参数传递给子进程,pickle出错,改为multiprocess.Pipe()创建连接即可解决。

import pathos
import multiprocess
import time

def f(x, conn, t0):
    ans = 1
    x0 = x
    t = time.time() - t0
    conn.send('factorial of %d: start@%.2fs' % (x0, t))
    while x > 1:
        ans *= x
        time.sleep(0.5)
        x -= 1
    t = time.time() - t0
    conn.send('factorial of %d: finish@%.2fs, res = %d' %(x0, t, ans))
    return ans

def main():
    var = (4, 8, 12, 20, 16)
    p = pathos.multiprocessing.ProcessPool()
    p_conn, c_conn = multiprocess.Pipe()
    t0 = time.time()
    conn_s = [c_conn] * len(var)
    t0_s = [t0] * len(var)
    res = p.map(f, var, conn_s, t0_s)

    print('output:')
    while p_conn.poll():
        print(p_conn.recv())
    t = time.time() - t0
    print('factorial of %s@%.2fs: %s' % (var, t, res))

if __name__ == '__main__':
    main()

结果:可以看出,第一批次4个子进程几乎同时开启;当一个子进程结束后,马上开启第5个子进程。

output:
factorial of 4: start@1.15s
factorial of 8: start@1.15s
factorial of 12: start@1.19s
factorial of 20: start@1.26s
factorial of 4: finish@2.65s, res = 24
factorial of 16: start@2.65s
factorial of 8: finish@4.66s, res = 40320
factorial of 12: finish@6.70s, res = 479001600
factorial of 16: finish@10.15s, res = 20922789888000
factorial of 20: finish@10.76s, res = 2432902008176640000
factorial of (4, 8, 12, 20, 16)@10.91s: [24, 40320, 479001600, 2432902008176640000, 20922789888000]

(4)pathos.multiprocessing.ProcessPool(),imap方法

import pathos
import multiprocess
import time

def f(x, conn, t0):
    ans = 1
    x0 = x
    t = time.time() - t0
    conn.send('factorial of %d: start@%.2fs' % (x0, t))
    while x > 1:
        ans *= x
        time.sleep(0.5)
        x -= 1
    t = time.time() - t0
    conn.send('factorial of %d: finish@%.2fs, res = %d' %(x0, t, ans))
    return ans

def main():
    var = (4, 8, 12, 20, 16)
    p = pathos.multiprocessing.ProcessPool()
    p_conn, c_conn = multiprocess.Pipe()
    t0 = time.time()
    conn_s = [c_conn] * len(var)
    t0_s = [t0] * len(var)
    res = list(p.imap(f, var, conn_s, t0_s))

    print('output:')
    while p_conn.poll():
        print(p_conn.recv())
    t = time.time() - t0
    print('factorial of %s@%.2fs: %s' % (var, t, res))

if __name__ == '__main__':
    main()

结果:可以看出,第一批次4个子进程几乎同时开启;当一个子进程结束后,马上开启第5个子进程。

output:
factorial of 4: start@1.27s
factorial of 8: start@1.29s
factorial of 12: start@1.30s
factorial of 20: start@1.38s
factorial of 4: finish@2.77s, res = 24
factorial of 16: start@2.77s
factorial of 8: finish@4.79s, res = 40320
factorial of 12: finish@6.81s, res = 479001600
factorial of 16: finish@10.27s, res = 20922789888000
factorial of 20: finish@10.89s, res = 2432902008176640000
factorial of (4, 8, 12, 20, 16)@11.01s: [24, 40320, 479001600, 2432902008176640000, 20922789888000]

(5)pathos.multiprocessing.ProcessPool(),uimap方法

import pathos
import multiprocess
import time

def f(x, conn, t0):
    ans = 1
    x0 = x
    t = time.time() - t0
    conn.send('factorial of %d: start@%.2fs' % (x0, t))
    while x > 1:
        ans *= x
        time.sleep(0.5)
        x -= 1
    t = time.time() - t0
    conn.send('factorial of %d: finish@%.2fs, res = %d' %(x0, t, ans))
    return ans

def main():
    var = (4, 8, 12, 20, 16)
    p = pathos.multiprocessing.ProcessPool()
    p_conn, c_conn = multiprocess.Pipe()
    t0 = time.time()
    conn_s = [c_conn] * len(var)
    t0_s = [t0] * len(var)
    res = list(p.uimap(f, var, conn_s, t0_s))

    print('output:')
    while p_conn.poll():
        print(p_conn.recv())
    t = time.time() - t0
    print('factorial of %s@%.2fs: %s' % (var, t, res))

if __name__ == '__main__':
    main()

结果:可以看出,第一批次4个子进程几乎同时开启;当一个子进程结束后,马上开启第5个子进程。而且,第5个进程的返回值排在第4个进程的返回值之前。

output:
factorial of 4: start@1.03s
factorial of 8: start@1.08s
factorial of 12: start@1.10s
factorial of 20: start@1.15s
factorial of 4: finish@2.53s, res = 24
factorial of 16: start@2.53s
factorial of 8: finish@4.58s, res = 40320
factorial of 12: finish@6.60s, res = 479001600
factorial of 16: finish@10.03s, res = 20922789888000
factorial of 20: finish@10.66s, res = 2432902008176640000
factorial of (4, 8, 12, 20, 16)@10.78s: [24, 40320, 479001600, 20922789888000, 2432902008176640000]

(6)pathos.multiprocessing.ProcessPool(),amap方法

import pathos
import multiprocess
import time

def f(x, conn, t0):
    ans = 1
    x0 = x
    t = time.time() - t0
    conn.send('factorial of %d: start@%.2fs' % (x0, t))
    while x > 1:
        ans *= x
        time.sleep(0.5)
        x -= 1
    t = time.time() - t0
    conn.send('factorial of %d: finish@%.2fs, res = %d' %(x0, t, ans))
    return ans

def main():
    var = (4, 8, 12, 20, 16)
    p = pathos.multiprocessing.ProcessPool()
    p_conn, c_conn = multiprocess.Pipe()
    t0 = time.time()
    conn_s = [c_conn] * len(var)
    t0_s = [t0] * len(var)
    res = p.amap(f, var, conn_s, t0_s).get()

    print('output:')
    while p_conn.poll():
        print(p_conn.recv())
    t = time.time() - t0
    print('factorial of %s@%.2fs: %s' % (var, t, res))

if __name__ == '__main__':
    main()

结果:可以看出,第一批次4个子进程几乎同时开启;当一个子进程结束后,马上开启第5个子进程。

output:
factorial of 4: start@1.04s
factorial of 8: start@1.07s
factorial of 12: start@1.12s
factorial of 20: start@1.13s
factorial of 4: finish@2.54s, res = 24
factorial of 16: start@2.54s
factorial of 8: finish@4.58s, res = 40320
factorial of 12: finish@6.62s, res = 479001600
factorial of 16: finish@10.04s, res = 20922789888000
factorial of 20: finish@10.64s, res = 2432902008176640000
factorial of (4, 8, 12, 20, 16)@10.76s: [24, 40320, 479001600, 2432902008176640000, 20922789888000]

(7)pathos.pp.ParallelPool(),pipe方法

注意,multiprocessing.Pipe()或multiprocess.Pipe()产生的管道对象无法传入子进程(可能是pickle错误)。但是,pathos.pp.ParallelPool()进程池中,子进程print函数可以直接输出到标准输出,因此也不必通过管道将信息传递到主进程了。

import pathos
import time

def f(x, t0):
    ans = 1
    x0 = x
    t = time.time() - t0
    print('factorial of %d: start@%.2fs' % (x0, t))
    while x > 1:
        ans *= x
        time.sleep(0.5)
        x -= 1
    t = time.time() - t0
    print('factorial of %d: finish@%.2fs, res = %d' %(x0, t, ans))
    return ans

def main():
    res = []
    var = (4, 8, 12, 20, 16)
    p = pathos.pp.ParallelPool()
    t0 = time.time()
    for i in var:
        res.append(p.pipe(f, i, t0))
        
    print('output:')
    t = time.time() - t0
    print('factorial of %s@%.2fs: %s' % (var, t, res))

if __name__ == '__main__':
    main()

结果:可以看出,所有子进程都是逐个执行的。

factorial of 4: start@0.12s
factorial of 4: finish@1.62s, res = 24
factorial of 8: start@1.80s
factorial of 8: finish@5.30s, res = 40320
factorial of 12: start@5.46s
factorial of 12: finish@10.96s, res = 479001600
factorial of 20: start@11.16s
factorial of 20: finish@20.66s, res = 2432902008176640000
factorial of 16: start@20.94s
factorial of 16: finish@28.44s, res = 20922789888000
output:
factorial of (4, 8, 12, 20, 16)@28.67s: [24, 40320, 479001600, 2432902008176640000, 20922789888000]

(8)pathos.pp.ParallelPool(),apipe方法

import pathos
import time

def f(x, t0):
    ans = 1
    x0 = x
    t = time.time() - t0
    print('factorial of %d: start@%.2fs' % (x0, t))
    while x > 1:
        ans *= x
        time.sleep(0.5)
        x -= 1
    t = time.time() - t0
    print('factorial of %d: finish@%.2fs, res = %d' %(x0, t, ans))
    return ans

def main():
    res = []
    var = (4, 8, 12, 20, 16)
    p = pathos.pp.ParallelPool()
    t0 = time.time()
    for i in var:
        res.append(p.apipe(f, i, t0))
        
    print('output:')
    for i in range(len(res)):
        res[i] = res[i].get()
    t = time.time() - t0
    print('factorial of %s@%.2fs: %s' % (var, t, res))

if __name__ == '__main__':
    main()

结果:可以看出,第一批次4个子进程几乎同时开启;当一个子进程结束后,马上开启第5个子进程。

output:
factorial of 4: start@0.20s
factorial of 4: finish@1.70s, res = 24
 factorial of 8: start@0.21s
factorial of 8: finish@3.71s, res = 40320
 factorial of 12: start@0.13s
factorial of 12: finish@5.63s, res = 479001600
 factorial of 20: start@0.18s
factorial of 20: finish@9.68s, res = 2432902008176640000
factorial of 16: start@1.70s
factorial of 16: finish@9.20s, res = 20922789888000
 factorial of (4, 8, 12, 20, 16)@9.72s: [24, 40320, 479001600, 2432902008176640000, 20922789888000]

(9)pathos.pp.ParallelPool(),map方法

import pathos
import time

def f(x, t0):
    ans = 1
    x0 = x
    t = time.time() - t0
    print('factorial of %d: start@%.2fs' % (x0, t))
    while x > 1:
        ans *= x
        time.sleep(0.5)
        x -= 1
    t = time.time() - t0
    print('factorial of %d: finish@%.2fs, res = %d' %(x0, t, ans))
    return ans

def main():
    var = (4, 8, 12, 20, 16)
    p = pathos.pp.ParallelPool()
    t0 = time.time()
    res= p.map(f, var, [t0] * 5)
        
    print('output:')
    t = time.time() - t0
    print('factorial of %s@%.2fs: %s' % (var, t, res))

if __name__ == '__main__':
    main()

结果:可以看出,所有子进程都是逐个执行的。

factorial of 4: start@0.14s
factorial of 4: finish@1.64s, res = 24
factorial of 8: start@1.74s
factorial of 8: finish@5.24s, res = 40320
factorial of 12: start@5.35s
factorial of 12: finish@10.85s, res = 479001600
factorial of 20: start@11.01s
factorial of 20: finish@20.51s, res = 2432902008176640000
factorial of 16: start@20.66s
factorial of 16: finish@28.16s, res = 20922789888000
 output:
factorial of (4, 8, 12, 20, 16)@28.51s: [24, 40320, 479001600, 2432902008176640000, 20922789888000]

(10)pathos.pp.ParallelPool(),amap方法

注意:amap方法中,如果子进程有print语句,会导致返回结果是只包含最后一个子进程返回值的tuple,而不是所有子进程的返回值组成完整list,原因暂不清楚。因此,amap方法中,子进程需要输出的内容只能通过返回值在主进程输出。

import pathos
import time

def f(x, t0):
    ans = 1
    x0 = x
    t = time.time() - t0
    msg1 = 'factorial of %d: start@%.2fs' % (x0, t)
    while x > 1:
        ans *= x
        time.sleep(0.5)
        x -= 1
    t = time.time() - t0
    msg2 = 'factorial of %d: finish@%.2fs, res = %d' %(x0, t, ans)
    return (ans, msg1, msg2)

def main():
    var = (4, 8, 12, 20, 16)
    p = pathos.pp.ParallelPool()
    t0 = time.time()
    ret = p.amap(f, var, [t0] * 5).get()
    res = [item[0] for item in ret]
        
    print('output:')
    for item in ret:
        print(item[1])
        print(item[2])
    t = time.time() - t0
    print('factorial of %s@%.2fs: %s' % (var, t, res))

if __name__ == '__main__':
    main()

结果:可以看出,第一批次4个子进程几乎同时开启;当一个子进程结束后,马上开启第5个子进程。

output:
factorial of 4: start@0.16s
factorial of 4: finish@1.66s, res = 24
factorial of 8: start@0.18s
factorial of 8: finish@3.68s, res = 40320
factorial of 12: start@0.19s
factorial of 12: finish@5.69s, res = 479001600
factorial of 20: start@0.14s
factorial of 20: finish@9.64s, res = 2432902008176640000
factorial of 16: start@1.66s
factorial of 16: finish@9.16s, res = 20922789888000
factorial of (4, 8, 12, 20, 16)@9.72s: [24, 40320, 479001600, 2432902008176640000, 20922789888000]

(11)pathos.pp.ParallelPool(),imap方法

import pathos
import time

def f(x, t0):
    ans = 1
    x0 = x
    t = time.time() - t0
    msg1 = 'factorial of %d: start@%.2fs' % (x0, t)
    while x > 1:
        ans *= x
        time.sleep(0.5)
        x -= 1
    t = time.time() - t0
    msg2 = 'factorial of %d: finish@%.2fs, res = %d' %(x0, t, ans)
    return (ans, msg1, msg2)

def main():
    var = (4, 8, 12, 20, 16)
    p = pathos.pp.ParallelPool()
    t0 = time.time()
    ret = list(p.imap(f, var, [t0] * 5))
    res = [item[0] for item in ret]
        
    print('output:')
    for item in ret:
        print(item[1])
        print(item[2])
    t = time.time() - t0
    print('factorial of %s@%.2fs: %s' % (var, t, res))

if __name__ == '__main__':
    main()

结果:可以看出,所有子进程都是逐个执行的。

output:
factorial of 4: start@0.17s
factorial of 4: finish@1.67s, res = 24
factorial of 8: start@1.67s
factorial of 8: finish@5.17s, res = 40320
factorial of 12: start@5.17s
factorial of 12: finish@10.67s, res = 479001600
factorial of 20: start@10.67s
factorial of 20: finish@20.17s, res = 2432902008176640000
factorial of 16: start@20.17s
factorial of 16: finish@27.67s, res = 20922789888000
factorial of (4, 8, 12, 20, 16)@28.41s: [24, 40320, 479001600, 2432902008176640000, 20922789888000]

(12)pathos.pp.ParallelPool(),uimap方法

import pathos
import time

def f(x, t0):
    ans = 1
    x0 = x
    t = time.time() - t0
    msg1 = 'factorial of %d: start@%.2fs' % (x0, t)
    while x > 1:
        ans *= x
        time.sleep(0.5)
        x -= 1
    t = time.time() - t0
    msg2 = 'factorial of %d: finish@%.2fs, res = %d' %(x0, t, ans)
    return (ans, msg1, msg2)

def main():
    var = (4, 8, 12, 20, 16)
    p = pathos.pp.ParallelPool()
    t0 = time.time()
    ret = list(p.uimap(f, var, [t0] * 5))
    res = [item[0] for item in ret]
        
    print('output:')
    for item in ret:
        print(item[1])
        print(item[2])
    t = time.time() - t0
    print('factorial of %s@%.2fs: %s' % (var, t, res))

if __name__ == '__main__':
    main()

结果:可以看出,第一批次4个子进程几乎同时开启;当一个子进程结束后,马上开启第5个子进程。而且,第5个进程的返回值排在第4个进程的返回值之前。

output:
factorial of 4: start@0.26s
factorial of 4: finish@1.76s, res = 24
factorial of 8: start@0.29s
factorial of 8: finish@3.79s, res = 40320
factorial of 12: start@0.25s
factorial of 12: finish@5.75s, res = 479001600
factorial of 16: start@1.77s
factorial of 16: finish@9.28s, res = 20922789888000
factorial of 20: start@0.31s
factorial of 20: finish@9.81s, res = 2432902008176640000
factorial of (4, 8, 12, 20, 16)@10.24s: [24, 40320, 479001600, 20922789888000, 2432902008176640000]

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python 并行计算 pathos模块 简介 的相关文章

  • 廣州亞運會項目維護?!

    今天要搞什麽亞運會的項目維護 xff0c 計算機學院需要招收一些精英 唉 xff01 他們就是好 xff01 有這麼多的機會 xff0c 我們這邊呢 xff01 看來學院老師一點都不關心我們的將來就業情況 我就鬱悶了 難道我們註定是這樣的嗎
  • arch linux 命令行设置临时静态IP

    设置临时静态ip 查看网卡名称 启用网卡 设置ip 设置默认网关 设置dns服务器 设置备用dns服务器 测试ping百度域名 xff08 等几秒出结果 xff09 span class token function ip span add
  • Jetpack-LiveData原理补充

    本篇主要对上篇分析LiveData时遗留的问题进行补充 xff0c 阅读本文需要实现对LiveData的原理有一定的了解 xff0c 如果不了解可以参考上一篇文章 xff1a Jetpack LiveData LiveData粘性事件 可以
  • postgre删除数据表重复数据

    select distinct into t sup supcomm copy from t sup supcomm drop table t sup supcomm select into t sup supcomm from t sup
  • postgre——to_date使用

    s date 61 to date to char date in 39 yyyy MM dd 39 39 yyyy MM dd 39 date in为time类型 xff0c s date为date类型
  • 旧电脑通过转接卡安装nvme固态硬盘无需修改bios即可用旧SATA硬盘实现UEFI启动win10/11

    注意 xff1a 本文探讨的是支持UEFI启动的旧电脑 MBR启动个人感觉已经被淘汰 xff0c 作者不再做研究 主要材料 xff1a PCI E转M 2的转接卡 xff08 根据你旧电脑支持情况选择 xff09 支持NVME协议M 2接口
  • postgre——case、union、小计总计(GROUP BY ROLLUP)写法

    将几张不同表结构的数据全部合并在一起 xff0c 使用了case union xff0c 同时实现小计总计 小计总计使用的 GROUP BY ROLLUP函数 xff0c GROUP BY ROLLUP使用双括号将字段括起来 xff0c 是
  • postgre连接符

    一 逻辑操作符 xff1a 常用的逻辑操作符有 xff1a AND OR 和NOT 其语义与其它编程语言中的逻辑操作符完全相同 二 比较操作符 xff1a 下面是PostgreSQL中提供的比较操作符列表 xff1a 操作符 描述 lt 小
  • springmvc拦截器

    1 自定义的拦截器 publicclass FirstInterceptor implements HandlerInterceptor 实现这个接口并重写方法 lt 配置自定义的拦截器 gt lt bean class 61 34 com
  • springmvc异常处理

    1 64 ExceptionHandler 在 64 ExceptionHandler 方法的入参中可以加入 Exception 类型的参数 该参数即对应发生的异常对象 64 ExceptionHandler 方法的入参中不能传入 Map
  • SpringMCV运行流程及整合spring

    一 Spring整合SpringMVC 64 Service用于标注业务层组件 64 Controller用于标注控制层组件 xff08 如struts中的action xff09 64 Repository用于标注数据访问组件 xff0c
  • Mac 下 man 命令汉化

    brew install automake brew install opencc git clone https github com man pages zh manpages zh git cd manpages zh autorec
  • FTP配置多用户多目录多权限

    环境介绍 根据开发的需求 要求创建FTP服务器 xff0c 把前端和后端分开用不同的FTP账号 系统环境 centos 7 4 防火墙设置 systemctl stop firewalld service 停止firewall xff08
  • Process finished with exit code -1073740791 (0xC0000409)

    选择编辑配置 勾选这个 出来具体问题 RuntimeError Unsupported image type must be 8bit gray or RGB image 就是这个原因 然后我卡在这里 xff0c 现在都还没有解决这个问题
  • Centos 6.8升级Python2.6.6至2.7.8

    由于之前用Python 2 7版本写了一个脚本 xff0c 移植到新的环境之后 xff0c 由于CentOS自带的Python版本较低 xff0c 有些函数执行出错 本文介绍CentOS 6 8从自带的Pyhon版本是2 6 6升级到2 7
  • Windows Server取消登录需按Ctrl+Alt+Delete组合键

    通过远程软件频繁远程Windows Server会遇到Ctrl 43 Alt 43 Delete组合键无法进入输入用户和密码的界面 xff0c 导致机器并没死机但是想登录又没法输入密码 xff0c 记录下取消方法 xff1a 1 Windo
  • 安装 Ubuntu 22.04.1 LTS 桌面版(详细步骤)

    文章目录 前言 xff1a 安装 Ubuntu 22 04 1 LTS 的先决条件 一 下载 Ubuntu 22 04 1 LTS 安装介质 二 制作 Ubuntu 22 04 1 LTS 引导 U 盘 三 从 U 盘引导 Ubuntu 四
  • Windows Server 2016-重置目录还原模式密码

    目录还原模式 xff1a Directory Services Restore Mode xff0c 简称DSRM xff0c 又称目录服务恢复模式 是Windows域控制器的服务器安全模式启动选项 DSRM允许管理员用来修复或还原修复或重
  • 人的梦想 是不会结束的!

    文章目录 前言一 一年之约1 学习嵌入式2 探寻嵌入式之路 二 我的心跳1 奉劝2 行动人的梦想是永远不会结束的 xff01 前言 随着在程序员这条路上不断发展 xff0c 自己学得越多 xff0c 就会感觉自己学的东西有多渺小 下面就说说
  • 记一次centos系统下的串口登陆异常问题

    一 问题概述 在使用centos7 5版本系统时 xff0c 安装配置好官方原版桌面环境后 xff0c 发现通过串口登陆系统出现了问题 具体问题现象有两种情况 xff1a 第一次通过串口输入用户名后 xff0c 输入密码的第一字符 xff0

随机推荐

  • Android Studio中引入Kotlin

    安装Kotlin插件 从Android studio3 0开始我们已经全面支持Kotlin 我们不需要添加Kotlin插件 而在Android2 X xff0c 我们还是需要添加Kotlin插件 Setting gt Plugins中下载k
  • 串口输出乱码问题的解决方法汇总(持续更新):

    平时工作中程序员在调试时总会用到串口打印数据以及一些标志位查看程序是否出现问题 但是在使用时总会遇到各种各样的问题 xff0c 最常见的就是输出乱码问题 xff08 指的是有收到数据但数据显示的是一堆不认识的汉字或字符 xff09 xff0
  • 谈谈技术在日常工作生活中的重要性

    我从事 技术方面 的工作也有10多年了 从最初的一个软件工程师 xff0c 也逐渐成长为项目经理 xff0c 部门经理 无论岗位怎么变换 xff0c 但是我还是没有离开技术方面的工作 xff0c 一直对技术的研究有很大热情 Csdn是非常棒
  • JSP中的网页编写格式——MIME TYPE?

    一 首先 xff0c 我们要了解浏览器是如何处理内容的 在浏览器中显示的内容有 HTML 有 XML 有 GIF 还有 Flash 那么 xff0c 浏览器是如何区分它们 xff0c 决定什么内容用什么形式来显示呢 xff1f 答案是 MI
  • 安卓开发05:Activity之间链接和传递参数

    Activity之间链接和传递参数主要通过Intent安卓的一个对象来实现 首先我们创建一个MainActivity xff1a java view plain copy print package com example androidt
  • 安卓开发06:布局-线性布局 LinearLayout

    LinearLayout把视图组织成一行或一列 子视图能被安排成垂直的或水平的 线性布局是非常常用的一种布局方式 请看一个布局例子 xff1a html view plain copy print lt LinearLayout xmlns
  • 可以带到2015年的几点思考

    1 自己的事情永远得自己出头 我从小就是个很独立的人 我不知道这种独立是什么时候培养起来的 xff0c 但清楚的记得一件事情 上小学那会儿 xff0c 有一年母亲生病了 xff0c 在医院 xff0c 家里就我一个人 xff0c 有时候 x
  • 远程桌面登录Lubuntu

    Lubuntu也就是ubuntu的服务器版本 xff0c 其实远程很简单 xff0c 并没有很多教程讲的那么复杂 远程桌面比较好的解决方式是用xrdb xff0c 效果也很不错 第一步 xff1a sudo apt get install
  • 用java做的一个小游戏—黑白反斗棋(适合菜鸟)

    用Java做的一个小游戏 xff0c 黑白反斗棋 xff0c 我玩过了5 5和10 10的 是学习之后做的 xff0c 不是自己原始开发的 import java awt Color import java awt FlowLayout i
  • 我的精神分裂——普通青年用二-B的方式走文艺的范儿

    一直以来都是以一种低沉的文笔在写些我的垃圾生活 xff0c 垃圾感想 xff0c 每次都会放那些特定的音乐 xff0c 那是一种心境 xff0c 那些音乐带着我的手在敲动 今天我想换种音乐 xff0c 猜猜我在放什么音乐 xff0c 很Hi
  • 读书随笔(1)——《计较是贫穷的开始》

    xff08 读书之后写感 xff0c 本该是读书之后自然的一个延续 xff0c 但我却很少这样了 xff0c 这不能说是一个极其坏的习惯 xff0c 虽不知道我究竟能不能改了 xff0c 但还是希望能尽可能的写写 xff0c 对自己想法有个
  • 2012年终随笔

    时至年终 xff0c 按我此前的惯例 xff0c 该写篇年终总结性的文章 xff0c 在之前末日说沸沸扬扬的时候 xff0c 我在想是否该早点写 xff0c 写个末日遗言什么的 xff0c 但还是没有写 xff0c 觉得如果真的末日来临 x
  • ath10k 出现ath10k_htt_t2h_msg_handler+0xebc/0x1efc解决方案

    问题log Fri Feb 21 20 08 34 2020 kern warn kernel nbsp 86 625786 WARNING CPU 0 PID 8 at openwrt 18 06 git build dir target
  • 解决第一次使用zookeeper闪退问题

    1 复制zookeeper 3 4 13 conf zoo sample cfg文件 xff0c 改名为zoo cfg 2 windows点击zookeeper 3 4 13 bin zkServer cmd命令启动zookeeper注册中
  • Android 11 update-api 遇到的问题

    1 缺少 非空判断 error log Missing nullability on method 96 getSecurityRestrictPwd 96 return MissingNullability import android
  • linux 文件 目录 默认权限

    结论 在linux系统中任何文件 目录都有一定的权限 xff0c 对于新创建的文件或目录 xff0c 也会有默认的访问权限 可能是出于安全的考虑 xff0c linux系统新建文件默认没有执行权限 xff0c 其最大权限为rw rw rw
  • centos 7 安装xfce 4桌面环境

    默认情况下 xff0c 完整安装的CentOS 7将安装GNOME图形用户界面 xff08 GUI xff09 xff0c 并且将在系统启动后加载 但是 xff0c 如果我们安装的CentOS没有安装GUI xff0c 那么我们总是可以稍后
  • Android 屏幕处于横屏状态 旋转180度界面切换显示

    span span public SreenOrientationListener mSreenOrientationListener span span mSreenOrientationListener 61 new SreenOrie
  • linux 桌面管理器 xfce 用户自动登录

    vi etc lightdm lightdm conf SeatDefaults autologin user 61 username 需要登录的用户名 autologin user timeout 61 delay
  • python 并行计算 pathos模块 简介

    目录 pathos模块 1 pathos自身的多进程方法 xff08 pathos multiprocessing ProcessPool pathos multiprocessing ProcessingPool pathos pools