我尝试使用Python的多处理模块并行运行多个样本的分析。我使用pool.map_async
在参数元组元组(crispr_analysis
)上生成函数(称为zipped_args
)。 zipped_args
中的每个元组都不为空,因为这可能导致多处理挂起。完成池后,它会挂起并无法继续执行脚本的其余部分。我知道crispr_analysis
在创建输出文件时会完成(使用with
语句生成,因此它们会正确关闭);我可以浏览所说的文件,它们已经完成了。我从未看到用于对结果进行排序的调试消息,程序永远不会终止。
try:
# Use map_async and get with a large timeout
# to allow for KeyboardInterrupts to be caught
# and handled with the try/except
timeout = max((9999, 600 * len(fastq_list)))
logging.debug("Setting timeout to %s seconds", timeout)
res = pool.map_async(crispr_analysis, zipped_args) # type: multiprocessing.pool.MapResult
pool.close()
results = res.get(timeout)
except (KeyboardInterrupt, ExitPool) as error: # Handle ctrl+c or custom ExitPool
pool.terminate()
# pool.join()
if isinstance(error, KeyboardInterrupt): # ctrl+c
sys.exit('\nkilled')
elif isinstance(error, ExitPool): # My way of handling SystemExits
sys.exit(error.msg)
else: # Shouldn't happen, but you know...
raise
except:
pool.terminate(); pool.join()
raise
else:
pool.join()
try:
logging.debug("Sorting results into alignments and summaries")
sort_start = time.time() # type: float
alignments, summaries = zip(*results) # type: Tuple[Tuple[alignment.Alignment]], Tuple[Dict[str, Any]]
logging.debug("Sorting results took %s seconds", round(time.time() - sort_start, 3))
except ExitPool as error: # Handle ExitPool calls for single-threaded map
sys.exit(error.msg)
有没有人知道为什么多处理挂起以及如何解决它?
额外信息:
crispr_analysis
返回一个元组和字典,每个都是空的或根据输入有一些长度pool.join()
语句,但无济于事ExitPool
是一个错误,我抛出整个池代替SystemExit
s;多处理通常吞下SystemExit
s,但我希望它们冒泡main
)此分析程序是从易于安装的条目脚本调用的,其中入口点是启动多处理池的main
函数
#!/usr/bin/python
# EASY-INSTALL-ENTRY-SCRIPT:
'EdiTyper==1.0.0','console_scripts','EdiTyper'
__requires__ = 'EdiTyper==1.0.0'
import sys
from pkg_resources import load_entry_point
if __name__ == '__main__':
sys.exit(
load_entry_point('EdiTyper==1.0.0', 'console_scripts', 'EdiTyper')()
)