You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Nextflow exec块中定义的变量作为process输出返回?

问题

我在Nextflow流水线里用Groovy写了一个process,想在exec块里执行逻辑,并把里面定义的变量作为process的输出返回。示例代码如下:

process my_process {
  output:
    val(myList), emit: my_process_output

  exec:
    def myList = []
    myList.append(1)
    myList.append(2)
    myList.append(3)
}

workflow {
  my_process()
  my_process.out.my_process_output.view()
}

但流水线在Tower上运行时返回错误:

Downloading plugin nf-google@1.4.5
N E X T F L O W  ~  version 22.10.6
Pulling <repo> ...
downloaded from https://github.com/<repo>.git
Launching `https://github.com/<repo>` [<run_name>] DSL2 - revision: <commit SHA>
WARN: Process 'my_process' cannot be executed by 'google-lifesciences' executor -- Using 'local' executor instead
WARN: Local executor only supports default file system (unless Fusion is enabled) -- Check work directory: gs://<org>/scratch/<uid>
WARN: Tower request field `workflow.revision` exceeds expected size | offending value: `<git branch>`, size: 42 (max: 40)
Monitor the execution with Nextflow Tower using this URL: https://tower.<my_org>/orgs/<URL>
[01/31785c] Submitted process > my_process
[01/31785c] NOTE: Process `my_process` failed -- Error is ignored
WARN: Tower request field `workflow.revision` exceeds expected size | offending value: `ss-3597/compare-basespace-and-gcp-filesize`, size: 42 (max: 40)
Saving cache: .nextflow/cache/<sha> => gs://<org>

请问怎么把exec块中定义/计算的变量作为process的输出返回?

解决方法

核心问题是exec块内用def定义的局部变量无法被输出定义捕获,Nextflow要求输出关联的变量必须处于process的顶层作用域。

修改方案非常直接:去掉exec块里myList的def关键字,让变量成为process级别的全局变量,这样输出定义就能正确获取它的值:

process my_process {
  output:
    val(myList), emit: my_process_output

  exec:
    myList = []  // 去掉def,变量提升至process作用域
    myList.append(1)
    myList.append(2)
    myList.append(3)
}

workflow {
  my_process()
  my_process.out.my_process_output.view()
}

补充说明

  • Nextflow的process中,def声明的变量是exec块的局部变量,输出定义无法访问该作用域内的变量;去掉def后,变量会被绑定到process顶层作用域,输出就能正常关联。
  • 你看到的Tower警告里,还有两个环境相关问题:
    1. google-lifesciences执行器不支持native execution模式(exec块属于该模式),因此自动 fallback到local执行器
    2. local执行器默认不支持GCS这类远程文件系统,除非启用Nextflow Fusion

如果需要在GCP环境稳定运行这类纯内存逻辑的process,建议启用Fusion;如果逻辑允许,也可以改用script块通过文件传递变量,但前者更适配当前场景。

内容的提问来源于stack exchange,提问作者ebosi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 17:08:18