- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个正在运行的命令行程序,我通过管道输入文本作为参数:
somecommand.exe
它会运行一段时间(通常是一小部分到几个小时),然后将结果写入许多文本文件。我正在尝试编写一个脚本来同时启动其中的几个,使用多核机器上的所有内核。在其他操作系统上,我会 fork ,但在 Windows 的许多脚本语言中都没有实现。 Python 的多处理看起来可以解决问题,所以我想我会试一试,尽管我根本不懂 python。我希望有人能告诉我我做错了什么。
我写了一个脚本(如下),我指向一个目录,如果找到可执行文件和输入文件,并使用 pool.map 和 n 的池启动它们,以及使用调用的函数。我看到的是,最初(启动了第一组 n 个进程)它看起来很好,100% 使用 n 个核心。但随后我看到进程进入空闲状态,不使用或仅使用其 CPU 的百分之几。那里总是有 n 个进程,但他们做的并不多。当他们去写输出数据文件时似乎会发生这种情况,一旦它开始一切都陷入困境,整体核心利用率范围从百分之几到偶尔达到 50-60% 的峰值,但永远不会接近 100%。
如果我可以附加它(编辑:我不能,至少现在不能),这里是进程的运行时间图。较低的曲线是当我打开 n 个命令提示符并手动保持 n 个进程同时运行时,轻松地将计算机保持在接近 100% 的状态。 (这条线是规则的,在 32 个不同的进程中,参数不同,从接近 0 小时缓慢增加到 0.7 小时。)上面的线是这个脚本的某些版本的结果——运行时间平均增加了大约 0.2 小时,并且更不可预测,就像我已经采取了底线并添加了 0.2 + 一个随机数。
这是剧情的链接: Run time plot
编辑:现在我想我可以添加情节了。
我做错了什么?
from multiprocessing import Pool, cpu_count, Lock
from subprocess import call
import glob, time, os, shlex, sys
import random
def launchCmd(s):
mypid = os.getpid()
try:
retcode = call(s, shell=True)
if retcode < 0:
print >>sys.stderr, "Child was terminated by signal", -retcode
else:
print >>sys.stderr, "Child returned", retcode
except OSError, e:
print >>sys.stderr, "Execution failed:", e
if __name__ == '__main__':
# ******************************************************************
# change this to the path you have the executable and input files in
mypath = 'E:\\foo\\test\\'
# ******************************************************************
startpath = os.getcwd()
os.chdir(mypath)
# find list of input files
flist = glob.glob('*_tin.txt')
elist = glob.glob('*.exe')
# this will not act as expected if there's more than one .exe file in that directory!
ex = elist[0] + ' < '
print
print 'START'
print 'Path: ', mypath
print 'Using the executable: ', ex
nin = len(flist)
print 'Found ',nin,' input files.'
print '-----'
clist = [ex + s for s in flist]
cores = cpu_count()
print 'CPU count ', cores
print '-----'
# ******************************************************
# change this to the number of processes you want to run
nproc = cores -1
# ******************************************************
pool = Pool(processes=nproc, maxtasksperchild=1) # start nproc worker processes
# mychunk = int(nin/nproc) # this didn't help
# list.reverse(clist) # neither did this, or randomizing the list
pool.map(launchCmd, clist) # launch processes
os.chdir(startpath) # return to original working directory
print 'Done'
最佳答案
进程是否有可能试图写入一个公共(public)文件?在 Linux 下,它可能会正常工作,破坏数据但不会减慢速度;但在 Windows 下,一个进程可能会获取文件,而所有其他进程可能会挂起,等待文件可用。
如果您用一些使用 CPU 但不写入磁盘的愚蠢任务替换您的实际任务列表,问题会重现吗?例如,您可能有计算某个大文件的 md5sum 的任务;一旦文件被缓存,其他任务将是纯 CPU,然后单行输出到标准输出。或者计算一些昂贵的函数或其他东西。
关于python - 在 python 中使用 map.pool 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6835628/
我正在使用 boost.pool,但我不知道何时使用 boost::pool<>::malloc和 boost::pool<>::ordered_malloc ? 所以, boost::pool<>:
我目前正在尝试从 anaconda 中的 spy 控制台运行并行代码。我相信问题可能出在我的计算机不允许 anaconda 控制 CPU 核心上,但我不知道如何解决这个问题。 另一个有趣的点是,当我运
在了解 Python 的 multiprocessing 包(对于 Python 3.4 )时,我注意到 multiprocessing.Pool 是在类 BaseContext 中定义的 上下文.p
我有这样的程序: from multiprocessing import Pool import time def f(x): # I make a heavy code here to take t
我有一个模块 A,它通过获取数据并将其发送到模块 B、C、D 等进行分析,然后将它们的结果结合在一起来执行基本的 map/reduce。 但是模块 B、C、D 等似乎不能自己创建多处理池,否则我得到
所以我有一个脚本可以连接到大约 700 个设备并执行一系列命令,然后退出。我开始使用 Multiprocessing.Pool 和 Pool.map 来减少脚本的运行时间,并允许我同时登录多个设备。
在下面的链接中有对 Pool 类的 map 方法的解释。 它似乎阻塞直到结果准备好。这意味着不需要执行 pool.close(); pool.join() 在运行 pool.map 之后,但是它在 t
context 是 class multiprocessing.pool.Pool 构造函数中的可选参数。 Documentation只说: context can be used to specif
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: What's the difference between sending -release or -dra
不确定这是否是正确的论坛。 libvirt 页面链接在这里。如果这需要张贴在不同的地方请告诉我。 virsh pool-define-as 和 create-as 有什么区别?阅读 virsh 的手册
谁能告诉我Spring Cloud Feign Client是否提供或支持Http连接池,如果可以,那么如何配置诸如池大小的设置?我似乎在官方文档中找不到此内容。谢谢你。 最佳答案 通过调查,我将尝试
我在尝试运行 Flask 应用程序时遇到了一些困难。我收到以下导入错误: File "/db/mysql_utils.py", line 2, in import mysql.conne
我有一个 Node 项目,在其中使用 pg-pool 库。我已在我的依赖项中包含以下内容: "@types/pg-pool": "0.0.3", "pg": "^7.3.0", "pg-format"
在 python 2 中,multiprocessing.dummy.Pool 和 multiprocessing.pool.ThreadPool 之间有什么区别吗?源代码似乎暗示它们是相同的。 最佳
这个问题在这里已经有了答案: Concurrent.futures vs Multiprocessing in Python 3 (6 个答案) 关闭 5 年前。 请给我解释一下这两个类有什么区别?
multiprocessing 的文档states以下关于Pool.join() : Wait for the worker processes to exit. One must call clos
我找到了一些资源,但我不确定我是否理解。 我找到的一些资源是: http://help.sap.com/saphelp_nw70/helpdata/en/fc/eb2ff3358411d1829f00
我的 Javafx 应用程序抛出许多非法状态异常,我尚未能够在源中跟踪触发器。 任何人都可以指导我导致此问题的原因以及我应该在哪里查找原因。我很难在这里展示一些代码,因为我不知道是什么原因造成的。 任
参见下面的示例和执行结果: #!/usr/bin/env python3.4 from multiprocessing import Pool import time import os def in
我目前有一个连接到我的主数据库的开放池,它运行良好。但是现在,我想为另一个数据库打开一个新池。我完全按照设置第一个池的方式设置了新池,显然我编辑了数据库名称等。加载 setupHikari() 方法时
我是一名优秀的程序员,十分优秀!