【发布时间】:2014-10-25 04:25:17
【问题描述】:
我需要对我的数据运行几百万次相对较慢的外部程序。这个外部程序称为 RNAup,这是一个用于确定两个 RNA 之间结合能的程序。在许多情况下,每个 RNA-RNA 对需要 10 到 15 分钟。这对于在几百万行数据上顺序运行来说太慢了,所以我决定通过尽可能并行运行程序来加快这个过程。但是,它仍然太慢了。下面是我如何并行化它的使用:
import subprocess
import multiprocessing as mult
import uuid
def energy(seq, name):
for item in seq:
item.append([]) # adding new list to house the energy information
stdin = open("stdin" + name + ".in", "w")
stdin.write(item)
stdin.close()
stdin = open("stdin" + name + ".in", "r") # bug: this line is required to prevent bizarre results. maybe to slow down something? time.sleep()ing is no good, you must access this specific file for some reason!
stdout = open("stdout" + name + "out", "w")
subprocess.call("RNAup < stdin" + name + ".in > stdout" + name + ".out", shell=True) # RNAup call slightly modified for brevity and clarity of understanding
stdout.close()
stdout = open("stdout" + name + ".out", "r")
for line in stdout:
item[-1].append(line)
stdout.close()
return seq
def intermediate(seq):
name = str(uuid.uuid4()) # give each item in the array a different ID on disk so as to not have to bother with mutexes or any kind of name collisions
energy(seq, name)
PROCESS_COUNT = mult.cpu_count() * 20 # 4 CPUs, so 80 processes running at any given time
mult.Pool(processes=PROCESS_COUNT).map(intermediate, list_nucleotide_seqs)
如何显着提高程序的速度? (顺便说一句,我会接受涉及将部分、大部分或全部程序转移到 C 的答案。)现在,我需要半年时间才能完成我所有的数据,这根本无法接受,我需要一些让我的程序更快的方法。
【问题讨论】:
-
PROCESS_COUNT = mult.cpu_count() * 20这是一个非常糟糕的主意。您的计算机一次只能执行cpu_count()CPU 密集型任务,因此启动 80 个进程仅意味着浪费大量内存并迫使您的操作系统进行上下文切换,以便为所有 80 个进程提供 CPU 时间。跨度> -
我知道,但不幸的是我没有看到还有什么可以做的,并希望它会以某种方式让它更快。当你只并行运行四个时,它也太慢了。
-
" 只并行运行四个也太慢了。" - 启动超过合理数量的任务不会神奇地让它更快。
标签: python c multiprocessing