【问题标题】:Python 2.7: How to compensate for missing pool.starmap?Python 2.7:如何弥补缺少的 pool.starmap?
【发布时间】:2018-10-04 16:27:59
【问题描述】:

我已经定义了这个函数

def writeonfiles(a,seed):
    random.seed(seed)

    f = open(a, "w+")
    for i in range(0,10):
        j = random.randint(0,10)
        #print j
        f.write(j)
    f.close()

其中 a 是包含文件路径的字符串,seed 是整数种子。 我想以这样一种方式并行化一个简单的程序,即每个内核采用我提供的可用路径之一,为其随机生成器播种并在该文件上写入一些随机数,例如,如果我通过 向量

vector = [Test/file1.txt, Test/file2.txt] 

和种子

seeds = (123412, 989898), 

它为第一个可用的核心提供功能

writeonfiles(Test/file1.txt, 123412) 

对于第二个具有不同参数的相同函数:

writeonfiles(Test/file2.txt, 989898)

我在 Stackoverflow 上查看了很多类似的问题,但我无法做出任何解决方案。 我尝试的是:

def writeonfiles_unpack(args):
    return writeonfiles(*args)
if __name__ == "__main__":
     folder = ["Test/%d.csv" %i for i in range(0,4)]
     seed = [234124, 663123, 12345 ,123833]
     p = multiprocessing.Pool()
     p.map(writeonfiles, (folder,seed))

并给我 TypeError: writeonfiles() 正好需要 2 个参数(给定 1 个)。

我也试过了

if __name__ == "__main__":
    folder = ["Test/%d.csv" %i for i in range(0,4)]
    seed = [234124, 663123, 12345 ,123833]
    p = multiprocessing.Process(target=writeonfiles, args= [folder,seed])
    p.start()

但它给了我
种子文件“/usr/lib/python2.7/random.py”,第 120 行 超级(随机,自我).seed(一) TypeError: unhashable type: 'list'

最后,我尝试了上下文管理器

 @contextmanager
 def poolcontext(*args, **kwargs):
     pool = multiprocessing.Pool(*args, **kwargs)
     yield pool
     pool.terminate()

if __name__ == "__main__":
    folder = ["Test/%d" %i for i in range(0,4)]
    seed = [234124, 663123, 12345 ,123833]
    a = zip(folder, seed)
    with poolcontext(processes = 3) as pool:
    results = pool.map(writeonfiles_unpack,a )

它会导致 文件“/usr/lib/python2.7/multiprocessing/pool.py”,第 572 行,在 get 提高self._value

TypeError: 'module' 对象不可调用

【问题讨论】:

  • 我想你在def writeonfiles_unpack(args) 中忘记了*
  • 实际上不,您正在将元组 args 解包到函数调用,对不起。但是感觉有点奇怪

标签: python python-2.7 arguments multiprocessing python-multiprocessing


【解决方案1】:

Python 2.7 缺少 Python 3.3+ 中的 starmap pool-method。您可以通过使用包装器来装饰您的目标函数来克服这个问题,该包装器将参数元组解包并调用目标函数:

import os
from multiprocessing import Pool
import random
from functools import wraps


def unpack(func):
    @wraps(func)
    def wrapper(arg_tuple):
        return func(*arg_tuple)
    return wrapper

@unpack
def write_on_files(a, seed):
    random.seed(seed)
    print("%d opening file %s" % (os.getpid(), a))  # simulate
    for _ in range(10):
        j = random.randint(0, 10)
       print("%d writing %d to file %s" % (os.getpid(), j, a))  # simulate


if __name__ == '__main__':

    folder = ["Test/%d.csv" % i for i in range(0, 4)]
    seed = [234124, 663123, 12345, 123833]

    arguments = zip(folder, seed)

    pool = Pool(4)
    pool.map(write_on_files, iterable=arguments)
    pool.close()
    pool.join()

【讨论】:

  • 谢谢,我更改了文件上的写入功能,使其能够打印实际文件上的内容(使用 f = open(a, "w+"))。但是,如果我将 f.close() 放在 for 之后,它会按预期进行,它会给我 ValueError: I/O operation on closed file;好像每个核心都会尝试使用相同的 f...
  • @Francesco Di Lauro 听起来像是 f.close() 的错误缩进。它仍然像你的例子吗?添加我的“打开文件”-额外打印回来看看它是否得到正确的文件名。顺便说一句,您还必须在尝试写入之前将j 设为字符串,否则您将获得TypeError
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-12
  • 2011-11-10
  • 2020-12-11
  • 1970-01-01
相关资源
最近更新 更多