首页 > 解决方案 > 规则定义中“线程”通配符的算术

问题描述

在我的工作流程中,我将两个多线程程序连接在一起。请参阅下面我的 Snakemake 文件中的规则定义。

rule do_the_thing:
    input: 'input.dat'
    output: 'output.dat'
    threads: 16
    shell: 'cmd1 --threads {threads} {input} | cmd2 --threads {threads} > {output}'

如所写,该命令将消耗 32 个线程。有没有办法可以对线程变量执行算术运算,以便(例如)每个命令只消耗一半的可用线程?

标签: pythonmultithreadingsnakemake

解决方案


有很多选择:

shell: 
    cpulimit --include-children -l {threads}00 --\
    cmd1 --threads {threads} {input} | cmd2 --threads {threads} > {output}
  • 计算参数中的线程使用情况:
threads: 16
params: lambda threads: max(1, threads//2)
shell: 
    cmd1 --threads {params.threads} {input} | cmd2 --threads {params.threads} > {output}
rule do_the_thing_first:
    output: pipe('output.pipe')
    threads: 8
    shell: 'cmd1 --threads {threads} {input} > {output}'

rule do_the_thing_second:
    input: 'output.pipe'
    output: 'output.dat'
    threads: 8
    shell: 'cmd2 --threads {threads} > {output}'

推荐阅读