函数中的Subprocess.call()不会暂停调用该函数的脚本

2024-06-17 10:20:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我看了看,找到了解决办法,试了试,得到了同样的结果。我尝试使用Popen.wait()run()call()。正如其他用户所建议的,我还尝试将命令作为字符串列表传递。没用。子流程调用没有给出错误,因此这不是问题所在

下面是函数:

def blast(file):
    command = f'blastn -query {output_path}fasta_files/{file} -db {db_path} -max_hsps 1 -max_target_seqs 40 -num_threads 4 -evalue 1e-5 ' \
              f'-out {output_path}blast/{file[:-2]}txt -outfmt "6 qseqid sseqid pident staxids sskingdoms qstart qend ' \
              f'qlen length sstart send slen evalue mismatch gapopen bitscore stitle"'
    subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).wait()

下面是对函数的调用:

import blastn
from process_blast_output import *
from remove_false_sequences import *
import os

directory = '/some/path/'


if __name__ == '__main__':
    for file in os.listdir(directory + 'fasta_files'):
        if 'btcaA1_trimmed' in file:
            blastn.blast(f'{file}') # That's where the function is called
            dataframe = get_dataframe(directory + f'blast/{file[:-2]}txt')
            dataframe = get_taxonomic_data(dataframe)
            delete_false_hits(fasta_to_dictionary(dataframe), directory + f'fasta_files/{file[:-2]}fa')

我没有传递字符串,而是尝试传递列表:

subprocess.Popen(['blastn', '-query', f'{output_path}fasta_files/{file}', '-db', f'{db_path}', '-max_hsps', '1',
                  '-max_target_seqs', '40', '-num_threads', '4', '-evalue', '1e-5', '-out',
                  f'{output_path}blast/{file[:-2]}txt', '-outfmt', "6 qseqid sseqid pident staxids sskingdoms "
                                                                   "qstart qend qlen length sstart send slen evalue"
                                                                   " mismatch gapopen bitscore stitle"],
                 stdout=subprocess.PIPE).wait()

Tags: pathimportdataframeoutputdbfilesdirectorymax
1条回答
网友
1楼 · 发布于 2024-06-17 10:20:32

可能实际的问题是您设置了stdout=subprocess.PIPE,但是忽略了输出。如果要放弃任何输出,请使用stdout=subprocess.DEVNULL;如果希望允许子进程正常写入标准输出,只需根本不设置stdout

是否使用shell=True(以及由shell要分析的单个字符串组成的第一个参数)与是否等待子流程无关(在这种情况下,第一个参数应该是正确标记的字符串列表)

通常应该避免Popen,默认情况下它不会等待subprocess.run()及其遗留兄弟{}等确实在等待外部子流程

一般来说,如果可以的话,可能会避免shell=True

def blast(file):
    subprocess.run(
        ['blastn', '-query,' f'{output_path}fasta_files/{file}',
          '-db', db_path, '-max_hsps', '1', '-max_target_seqs', '40',
          '-num_threads', '4', '-evalue', '1e-5 ',
          '-out', f'{output_path}blast/{file[:-2]}txt',
          '-outfmt' "6 qseqid sseqid pident staxids sskingdoms qstart qend "
                    "qlen length sstart send slen evalue mismatch gapopen "
                    "bitscore stitle"],
    stdout=subprocess.DEVNULL, check=True)

您创建的子流程将被等待,但当然它仍然可能创建了自己的分离子流程,如果子流程对调用方隐藏了这一点,Python就不能直接等待

顺便说一句,你的if __name__ == '__main__'代码应该很简单;如果您将所有有用的代码都放在这个块中,那么不管怎样,该文件都不可能对import放入另一个脚本有用,因此整个__name__检查是毫无意义的。这样做的目的是让你可以说

def useful_code():
    # lots of code here

if __name__ == '__main__':
    useful_code()

现在,如果您python scriptname.py,那么__name__将是__main__,因此对useful_code()的调用将立即执行。但是如果你import scriptname(假设你已经设置好了可以这样做的东西,使用正确的sys.path等等)不会导致useful_code立即运行;相反,调用方决定是否以及何时实际运行此函数(或者模块中的其他函数,如果它包含多个函数)

更进一步地说,f'{file}'只是一种非常笨拙的方式来表示file(或者str(file),如果变量还不是字符串)

相关问题 更多 >