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 insubprocess_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
满意答案
移动上传的文件后,尝试执行以下操作:
@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.
相关问答
更多无法使用php从目录中删除文件(Can Not delete a file from directory using php)
Linux目录权限读写但不删除(Linux directory permissions read write but not delete)
对于使用PHP副本创建的文件,无法更改FTP的权限(Can not change permissions on FTP for files created with PHP copy)
无法删除使用PHP创建的目录或文件(也称为权限地狱)(Cannot delete directory or files created with PHP (a.k.a permissions hell))
PHP和权限(PHP & Permissions)
修改php脚本创建的文件的权限(modify permissions for files created by php script)
PHP:创建文件并保留文件权限(PHP: Creating files and preserving file permissions)
PHP无法删除目录(PHP cannot delete directory)
如何使用php删除目录中的旧文件(how to delete old files in a directory using php)
PHP fopen()创建没有权限修改文件的文件(PHP fopen() creating file without permissions to modify it)
相关文章
更多《虚拟机系统与进程的通用平台》(Virtual Machines: Versatile Platforms for Systems and Processes)扫描版[PDF]
探索 Python,第 1 部分: Python 的内置数值类型
HDFS patch前后Ganglia看到running processes变化的分析
Python资源索引 【转载】
python字典操作
python下载pps视频
python top project of 2013
Python:渗透测试开源项目【源码值得精读】
【转帖】Python 资源索引
(转)Python WEB应用框架纵览
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!