V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
frmongo
V2EX  ›  Python

Python 多进程问题

  •  
  •   frmongo · 2020-12-02 14:09:48 +08:00 · 1959 次点击
    这是一个创建于 1235 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在一个 linux 主机上,我想同时开始播放音乐和录音(有麦克风),期望录音结束,音乐自动停止。 现在只需要录音 20 秒,下面的代码录音结束后,音乐不会停止,怎么写可以实现目的呢

    # coding:utf-8
    
    from multiprocessing import Process as process
    import os
    
    def play_music():
        cmd1 = "aplay muisc.wav"
        os.system(cmd1)
    
    def record():
        cmd1 = "arecord -r 16000 -t wav -d 20 -v out.wav"
        os.system(cmd1)
    
    if __name__ == '__main__':
        
        p1 = process(target=play_music)
        p1.daemon=True
        p1.start()
        record()
        p1.terminate()
        p1.join()
    
    9 条回复    2020-12-04 17:32:38 +08:00
    ruanimal
        1
    ruanimal  
       2020-12-02 14:16:02 +08:00
    直接用 subprocess.popen 就好了啊,用啥 multiprocessing 。。。
    jkhere
        2
    jkhere  
       2020-12-02 15:27:49 +08:00
    不是很好实现,子进程开始执行后,主进程就无法精确控制子进程了。
    假设你子进程开始后,主进程开始计时,到时间后杀死子进程,这样也只是粗略的控制时间。
    mingl0280
        3
    mingl0280  
       2020-12-02 15:48:26 +08:00 via Android
    先开始放音乐,再启动录音,录音返回以后( system 有返回值吧) kill 掉放音……
    ysc3839
        4
    ysc3839  
       2020-12-02 15:56:21 +08:00 via Android
    @ruanimal 我也觉得应该直接使用 subprocess.popen 。play_music 就只是调用 shell 启动个新进程,不如直接自己启动新进程,没必要绕一圈。
    ruanimal
        5
    ruanimal  
       2020-12-02 16:38:30 +08:00
    @ysc3839 意思是用 subprocess.popen 替换 multiprocessing 和 os.system
    ysc3839
        6
    ysc3839  
       2020-12-02 17:17:35 +08:00 via Android
    @ruanimal 是的,我就是想表达这个意思。
    jkhere
        8
    jkhere  
       2020-12-04 17:31:15 +08:00
    给你个 demo,用 python 的协程吧:

    import asyncio

    async def eternity():
    # do your job
    while True:
    print('doing!')

    async def main():
    # Wait for at most 20 second
    try:
    await asyncio.wait_for(eternity(), timeout=20.0)
    except asyncio.TimeoutError:
    print('timeout! job done')

    asyncio.run(main())
    jkhere
        9
    jkhere  
       2020-12-04 17:32:38 +08:00
    ```python
    import asyncio

    async def eternity():
    # do your job
    while True:
    print('doing!')

    async def main():
    # Wait for at most 20 second
    try:
    await asyncio.wait_for(eternity(), timeout=20.0)
    except asyncio.TimeoutError:
    print('timeout! job done')

    asyncio.run(main())
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2862 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 13:10 · PVG 21:10 · LAX 06:10 · JFK 09:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.