首页 \ 问答 \ Python多个进程不会循环(Python multiple processes do not loop on for)

Python多个进程不会循环(Python multiple processes do not loop on for)

我正在研究几种算法的实现,以计算图上的最短路径。

我已经设法顺序实现了Dijkstra的算法,现在我正在尝试通过Python的多处理模块来优化我的算法。

整个代码都有效。 我在这里要做的是:

  • 首先用nb_cpu = mp.cpu_count()来检查我可以处理多少cpus
  • 然后相应地划分我在图表中的所有节点
  • 最后调用方法subprocess_dijkstra ,它应该为每个节点计算dijkstra算法,它作为一个参数给出(这个想法是每个进程只需要为图的一小部分计算算法)。

当我运行我的脚本(从main.py文件中调用我只是根据我的需要格式化数据)时,我有4个进程正在启动。

但是,它们似乎不在subprocess_dijkstra for node in nodes定义的for node in nodes循环中执行for node in nodes

每个进程只计算一次代码,然后无限期地暂停...

这是我在Python下进行多处理的第一次尝试,所以我可能错过了一个细节。 有人有想法吗?

当我中断脚本时,python告诉我中断发生在p.join()行上。

感谢有人帮助我:)

这是我的代码:

import multiprocessing as mp

def subprocess_dijkstra(do_print, nodes, tab_contenu, tab_distances):
    tab_dist_initial = dict(tab_distances)
    tab_dist = dict()
    for node in nodes:
        visited_nodes = list()
        tab_dist = dict(tab_dist_initial)
        dmin = -1
        resultat = ""
        filename = "dijkstra"+str(node)+".txt"

        if do_print:
            dt = open(filename, 'w')
            tab_dist[node] = 0

            """Ligne de résultat initiale"""
            for valeur in tab_dist.values():
                resultat += str(valeur)
                resultat += " "
            resultat += "\n"

            dt.write(resultat)

        while len(visited_nodes) != len(tab_contenu):
            """ On se place sur le noeud non visité qui a la distance minimale de notre départ """
            for cle, valeur in tab_dist.items():
                if cle not in visited_nodes:
                    if dmin ==-1 or valeur<dmin:
                        dmin = valeur
                        node = cle


            """ On vérifie que le noeud n'a pas déjà été visité """
            if (node not in visited_nodes):
                """ On regarde les fils de ce noeud et la longueur des arcs"""
                for cle,valeur in tab_contenu[node].items():
                    tab_dist[cle] = min(tab_dist[cle], tab_dist[node]+valeur)

                visited_nodes.append(node)

                if do_print:
                    resultat = ""
                    """ Ligne de résultat """
                    for valeur in tab_dist.values():
                        resultat += str(valeur)
                        resultat += " "
                    resultat += "\n"

                    dt.write(resultat)

        if do_print:
            dt.close()


def main(do_print,donnees):

    tab_contenu = donnees[1]
    nb_nodes = int(donnees[0])
    tab_distances = {x: float('inf') for x in range(nb_nodes)}
    args=[(do_print, x, tab_contenu, tab_distances) for x in range(nb_nodes)]
    nb_cpu = mp.cpu_count()


    pool = mp.Pool(processes = nb_cpu)
    pool.starmap(subprocess_dijkstra, args)
    pool.close()
    pool.join()

I'm working on the implementation of several algorithms to compute shortest paths on graphs.

I have managed to implement Dijkstra's algorithm sequentially and I'm now trying to optimize my algorithm through the multiprocessing module of Python.

As a whole the code works. What I am trying to do here is :

  • First to check how many cpus I can work on with nb_cpu = mp.cpu_count()
  • Then dividing all the nodes I have in my graph accordingly
  • Finally calling the method subprocess_dijkstra that should compute the dijkstra algorithm for each of the nodes it is given as an argument (the idea being that each process only has to compute the algorithm for a smaller part of the graph).

When I run my script (called from a main.py file where I just format the data to suit my needs), I have 4 processes launched as I should.

However, they do not seem to execute the for node in nodes loop defined in subprocess_dijkstra.

Each process only computes the code once and then they go on hold indefinitely...

It is my first attempt at multiprocessing under Python so I may have missed a detail. Does anybody have an idea ?

When I interrupt the script, python tells me that the interruption takes place on the p.join() line.

Thanks to anyone helping me :)

Here is my code :

import multiprocessing as mp

def subprocess_dijkstra(do_print, nodes, tab_contenu, tab_distances):
    tab_dist_initial = dict(tab_distances)
    tab_dist = dict()
    for node in nodes:
        visited_nodes = list()
        tab_dist = dict(tab_dist_initial)
        dmin = -1
        resultat = ""
        filename = "dijkstra"+str(node)+".txt"

        if do_print:
            dt = open(filename, 'w')
            tab_dist[node] = 0

            """Ligne de résultat initiale"""
            for valeur in tab_dist.values():
                resultat += str(valeur)
                resultat += " "
            resultat += "\n"

            dt.write(resultat)

        while len(visited_nodes) != len(tab_contenu):
            """ On se place sur le noeud non visité qui a la distance minimale de notre départ """
            for cle, valeur in tab_dist.items():
                if cle not in visited_nodes:
                    if dmin ==-1 or valeur<dmin:
                        dmin = valeur
                        node = cle


            """ On vérifie que le noeud n'a pas déjà été visité """
            if (node not in visited_nodes):
                """ On regarde les fils de ce noeud et la longueur des arcs"""
                for cle,valeur in tab_contenu[node].items():
                    tab_dist[cle] = min(tab_dist[cle], tab_dist[node]+valeur)

                visited_nodes.append(node)

                if do_print:
                    resultat = ""
                    """ Ligne de résultat """
                    for valeur in tab_dist.values():
                        resultat += str(valeur)
                        resultat += " "
                    resultat += "\n"

                    dt.write(resultat)

        if do_print:
            dt.close()


def main(do_print,donnees):

    tab_contenu = donnees[1]
    nb_nodes = int(donnees[0])
    tab_distances = {x: float('inf') for x in range(nb_nodes)}
    args=[(do_print, x, tab_contenu, tab_distances) for x in range(nb_nodes)]
    nb_cpu = mp.cpu_count()


    pool = mp.Pool(processes = nb_cpu)
    pool.starmap(subprocess_dijkstra, args)
    pool.close()
    pool.join()

原文:https://stackoverflow.com/questions/29447447
更新时间:2022-06-15 06:06

最满意答案

移动上传的文件后,尝试执行以下操作:

@chmod($path ."/" . $_FILES["file"]["name"], 0777);

或者其他的东西。 您可能希望将权限更改为更好的内容。


After you move the uploaded file, try doing:

@chmod($path ."/" . $_FILES["file"]["name"], 0777);

or something. You might want to change the permissions to something better.

相关问答

更多

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)