使用multiprocessing.P访问类字段

2024-09-25 10:27:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个有向图,在这个图上我必须找到从一组给定的节点经过的最短路径。为了做到这一点,我寻找这个集合上的排列,对于每一个排列,我计算每一对节点之间最短路径的长度总和。我有一个变量,计算到目前为止发现的最短排列和长度。你知道吗

在我必须处理10个节点之前,我设法在1s内检查所有的排列,之后由于这个原因我想使用一个瓶颈多处理池(). 你知道吗

问题是我必须读写池.imap被称为。你知道吗

这是我在不使用多处理/多线程的情况下使用的代码。你知道吗

此代码包含在类区域的createRoute(self)方法中。你知道吗

#This returns a list of nodes as integers
binsToEmpty = self.calculateBinsToEmpty() 

minLength = float("inf")
minPath = None

allPermutationsOfPaths = permutations(binsToEmpty)

for p in allPermutationsOfPaths:
    readElem = iter(p)
    next(readElem)

    length = 0
    for a,b in izip(p, readElem):
        if length < minLength:
            #getShortestLength returns an integer
            length += self.getSorthestLength(a._id, b._id)
        else:
            break

    #Every path starts and ends at node 0 which is not included in the permutation        
    length += self.getSorthestLength(0, p[0]._id)
    length += self.getSorthestLength(p[-1]._id, 0)

    if length < minLength:
        minLength = length
        minPath = p

我试图从类似的问题中找出解决方法,但我还不知道如何实现它。你知道吗


Tags: 方法代码inself路径id节点length