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.
相关问答
更多-
删除单引号 - 使用 unlink($dir); 代替 unlink('$dir'); remove the single quotes - use unlink($dir); instead of unlink('$dir');
-
在目录上设置粘贴位置可能就够了。 用户将能够删除他们拥有的任何文件,但不能删除其他用户。 这可能足以满足您的用例。 在大多数系统上,/ tmp以这种方式设置(/ tmp设置为1777) chmod 1775 /控制 但是,如果您想要更多的控制,则必须在有问题的文件系统上启用ACL。 在/ etc / fstab中,将acl附加到标志: /dev/root / ext3 defaults,acl 1 1 然后可以使用setfacl ...
-
对于使用PHP副本创建的文件,无法更改FTP的权限(Can not change permissions on FTP for files created with PHP copy)[2022-11-14]
PHP脚本与通过FTP登录的用户不同。 使用另一个php脚本更改文件的permisions或通过SSH以root身份登录。 PHP script is a different user than the one that is logging in through FTP. Either change the permisions of the file using another php script or log in as root through SSH. -
无法删除使用PHP创建的目录或文件(也称为权限地狱)(Cannot delete directory or files created with PHP (a.k.a permissions hell))[2023-01-10]
移动上传的文件后,尝试执行以下操作: @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 s ... -
PHP和权限(PHP & Permissions)[2022-04-02]
是的,php可以更改文件的所有权。 使用chown($file, $user) 。 您可以编写一个简单的递归脚本来使用chown更改每个文件的所有者。 Yeah, php can change ownership of a file. Use chown($file, $user). You could write a simple recursive script to change owner for each file using chown. -
尝试chmod功能。 例如chmod("/somedir/somefile", 0755) Try chmod function. For example chmod("/somedir/somefile", 0755)
-
如果您通过执行以下操作运行Linux,则可以在文件系统级别解决此问题: chown support:apache foo chmod 775 foo chmod g + s foo 其中foo是保存文件的文件夹。 现在所有(新创建的)文件都应该由support支持:apache。 如果确保将文件创建为664,则两个位置都可以读/写它。 这不是解决问题的唯一(也许甚至不是最好的)方法,但它可能是最简单的方法。 You can solve this at the file system level if you ...
-
PHP无法删除目录(PHP cannot delete directory)[2023-06-21]
[编辑]只能删除空目录。 尝试:使用PHP备份进行备份后,您将遍历目录,然后删除最旧的备份文件: $files = glob('backupfolder/*.bak'); if(count($files) > 10) foreach($files as $file) if(time() - filectime($file) > 10 * 24 * 60 * 60) unlink($file); After making the backup with the PHP backup you loop through ...事实证明,有问题的目录安装在不支持Linux文件权限的文件系统上。 It turns out the directory in question was mounted on a file system that doesn't support linux file permissions.相关文章
更多- 《虚拟机系统与进程的通用平台》(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应用框架纵览
最新问答
更多- 如何在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)