繁体   English   中英

如何实现线程化以在python中运行两个bash shell命令?

[英]How to implement threading to run two bash shell commands in python?

我必须记录一个wav文件,同时还要用sox分析它。 我正在为该操作使用fifo类型文件。

因此,在这里我需要同时启动2个线程,但是即使我使用这些线程,也无法实现我想做的事情。 总是先执行,然后再执行。 我希望它们并行运行,以便我可以做一些事情。

#this should be in one thread
def test_wav(self):
    """ analyze the data """
    bashCommand = "sox {} -n stat".format(self.__rawfile)
    while self.__rec_thread.is_alive():
        process = subprocess.Popen(bashCommand.split(),stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        wav_output = process.communicate()[1] #sox outputs the details in stderr
        #do something and return

#this should be in another thread
def record_wav(self):
    bashCommand = "arecord -d 10 -c 2 -r 48000 -f S32_LE > {}".format(self.__rawfile)
    pid = subprocess.Popen(bashCommand.split())
    pid.wait()
    if pid.returncode != 0:
        raise RecordException("Failed while recording with error {}".format(pid.returncode))

我尝试了以下代码使它们成为线程,但失败了(总是先执行,然后再执行另一个。我希望它们并行执行,以便我可以做一些事情)。 from threading import Thread

self.__rec_thread = Thread(target = self.record_wav())
amp_thread = Thread(target = self.test_wav())
self.__rec_thread.start()
amp_thread.start()

编辑:首先,它完全执行记录(由于选项-d 10最少需要10秒),然后再执行测试wav函数。 就像一个接一个地叫他们。

... target = self.record_wav() ...

正在调用 record_wav() :它立即执行,并且直到record_wav()完成,程序才继续执行。 您几乎总是希望将一个函数(或方法)对象传递给target= ,几乎从不希望执行该函数/方法的结果。 因此,只需删除括号即可:

... target = self.record_wav ...

如果您可能使用python3,则可以使用asyncio以goroutines方式运行shell命令。

import asyncio
import sys

async def execute(command, cwd=None, shell=True):
    process = await asyncio.create_subprocess_exec(*command,
                                                   stdout=asyncio.subprocess.PIPE,
                                                   stderr=asyncio.subprocess.PIPE,
                                                   cwd=cwd,
                                                   shell=shell)
    std_out, std_err = await process.communicate()

    error = std_err.decode().strip()
    result = std_out.decode().strip()
    print(result)
    print(error)
    return result


if sys.platform == "win32":
    loop = asyncio.ProactorEventLoop()
    asyncio.set_event_loop(loop)
else:
    loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(
        asyncio.gather(execute(["bash", "-c", "echo hello && sleep 2"]), execute(["bash", "-c", "echo ok && sleep 1"])))
except Exception as e:
    raise e
finally:
    loop.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM