English 中文(简体)
• 如何在平时使用多处理库时具体说明多功能的论点?
原标题:How to specify the arguments of a multi-variable function when using multiprocessing Pool in python?

我想在以下几页用多处理模块在座标上找到Pythagorean关系的一些解决办法。 为了加快找到答案的进程,我正试图将进程平行进行:

import itertools
from multiprocessing import Pool, Array
import numpy as np

MAXIMUM_INT: int = 10
answers = { A : [],  B : [],  C : []}

base_range = np.arange(1, MAXIMUM_INT + 1)

A_range = B_range = C_range = base_range.tolist()

combinatorics = [A_range, B_range, C_range]
iteration = list(itertools.product(*combinatorics))

dim1 = dim2 = dim3 = MAXIMUM_INT


def init(A: int, B: int, C: int):
    global test1, test2, test3
    test1, test2, test3 = A, B, C


def conditionalAssert(answerDict: dict, A: int, B: int, C: int):
    t1 = np.frombuffer(test1.get_obj())
    t2 = np.frombuffer(test2.get_obj())
    t3 = np.frombuffer(test3.get_obj())

    if A**2 + B**2 == C**2:
        answerDict[ A ].append(t1)
        answerDict[ B ].append(t2)
        answerDict[ C ].append(t3)


if __name__ ==  __main__ :
    relevantArray = Array( i , dim1 * dim2 * dim3)
    A = B = C = relevantArray
    pool = Pool(processes=7, initializer=init, initargs=(A, B, C))
    pool.starmap(conditionalAssert, iteration)
    print(answers)

However, running the script "as is" creates an error that I don t know how to solve. I would appreciate any help:

multiprocessing.pool.RemoteTraceback: 
"""
Traceback (most recent call last):
  File "C:Usersusernameanaconda3Libmultiprocessingpool.py", line 125, in worker
    result = (True, func(*args, **kwds))
                    ^^^^^^^^^^^^^^^^^^^
  File "C:Usersusernameanaconda3Libmultiprocessingpool.py", line 51, in starmapstar
    return list(itertools.starmap(args[0], args[1]))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: conditionalAssert() missing 1 required positional argument:  C 
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:UsersusernameOneDriveDesktopProjectspythagorean.py", line 38, in <module>
    pool.starmap(conditionalAssert, iteration)
  File "C:Usersusernameanaconda3Libmultiprocessingpool.py", line 375, in starmap
    return self._map_async(func, iterable, starmapstar, chunksize).get()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:Usersusernameanaconda3Libmultiprocessingpool.py", line 774, in get
    raise self._value
TypeError: conditionalAssert() missing 1 required positional argument:  C 
问题回答

根据Andrej Kesely的建议,我得以修改我的原始文字,说明作为我初步草案一部分提出的问题。 这里是我解决这一问题的办法。 (阿加因,感谢你!) 并请让我知道这是否能够做得更好:

import itertools
from multiprocessing import Pool, Array, RawArray
import numpy as np

MAXIMUM_INT: int = 10
answers = { A : [],  B : [],  C : []}

base_range = np.arange(1, MAXIMUM_INT + 1)

A_range = B_range = C_range = base_range.tolist()

combinatorics = [A_range, B_range, C_range]
iteration = list(itertools.product(*combinatorics))

dim1 = dim2 = dim3 = MAXIMUM_INT

def init(A: int, B: int, C: int):
    global test1, test2, test3
    test1, test2, test3 = A, B, C


def conditionalAssert(A: int, B: int, C: int):
    test1 = RawArray("i", dim1)
    test2 = RawArray("i", dim2)
    test3 = RawArray("i", dim3)

    t1 = np.frombuffer(test1, dtype=int)
    t2 = np.frombuffer(test2, dtype=int)
    t3 = np.frombuffer(test3, dtype=int)

    if int(A**2 + B**2) == int(C**2):
        answers[ A ].append(A)
        answers[ B ].append(B)
        answers[ C ].append(C)
        return A, B, C
    return None

if __name__ ==  __main__ :
    relevantArray = Array( i , dim1 * dim2 * dim3)
    A = B = C = relevantArray
    pool = Pool(processes=7, initializer=init, initargs=(A, B, C))
    for object_returned_from_conditionalAssert in pool.starmap(conditionalAssert, iteration):
        if object_returned_from_conditionalAssert:
            e1, e2, e3 = object_returned_from_conditionalAssert
            answers[ A ].append(e1)
            answers[ B ].append(e2)
            answers[ C ].append(e3)

    print(answers)




相关问题
Get webpage contents with Python?

I m using Python 3.1, if that helps. Anyways, I m trying to get the contents of this webpage. I Googled for a little bit and tried different things, but they didn t work. I m guessing that this ...

What is internal representation of string in Python 3.x

In Python 3.x, a string consists of items of Unicode ordinal. (See the quotation from the language reference below.) What is the internal representation of Unicode string? Is it UTF-16? The items ...

What does Python s builtin __build_class__ do?

In Python 3.1, there is a new builtin function I don t know in the builtins module: __build_class__(...) __build_class__(func, name, *bases, metaclass=None, **kwds) -> class Internal ...

what functional tools remain in Python 3k?

I have have read several entries regarding dropping several functional functions from future python, including map and reduce. What is the official policy regarding functional extensions? is lambda ...

Building executables for Python 3 and PyQt

I built a rather simple application in Python 3.1 using PyQt4. Being done, I want the application to be distributed to computers without either of those installed. I almost exclusively care about ...

热门标签