• 请不要在回答技术问题时复制粘贴 AI 生成的内容
zhangzhi0000
V2EX  ›  程序员

Poweshell 管道语句直接是多线程处理吗?如何单线程运行?

  •  1
     
  •   zhangzhi0000 · May 22, 2019 · 2885 views
    This topic created in 2553 days ago, the information mentioned may be changed or developed.
    Get-ChildItem $pwd -Filter *.mp3 | ForEach-Object -Process{
    if($_ -is [System.IO.FileInfo])
    {
    python C:\ffmpeg.py $_.name
    }
    }
    

    例如本代码,我要调用 Python 脚本处理音频文件,比如文件夹下有 10 个 MP3,执行的时候 10 线程同时开始的。

    6 replies    2019-05-23 19:48:43 +08:00
    zhangzhi0000
        1
    zhangzhi0000  
    OP
       May 22, 2019
    标题打错了,是 PowerShell
    xupefei
        2
    xupefei  
       May 22, 2019
    ffmpeg.py 里要等待 ffmpeg 进程退出。
    zhangzhi0000
        3
    zhangzhi0000  
    OP
       May 22, 2019
    @xupefei 我在 mac 上试了下,是单线程顺序执行下来,没问题,之前 win7 运行的时候多个 ffmpeg 进程同时执行,明天我再去试试
    whe
        4
    whe  
       May 23, 2019
    目测是 -Process 的锅 不是 | 的
    zhangzhi0000
        5
    zhangzhi0000  
    OP
       May 23, 2019
    @whe 亲测不是-Process 的问题,我在 mac 和 win7 都测试了不带-Process,mac 是单个进程执行,win7 是同步执行所有的进程。

    ![]( https://s2.ax1x.com/2019/05/23/VCIxsO.jpg)

    mac 和 win7 上都是目前最新的版本( 6.2.1 )

    ![]( https://s2.ax1x.com/2019/05/23/VCIvQK.jpg)
    zhangzhi0000
        6
    zhangzhi0000  
    OP
       May 23, 2019
    @whe 我知道问题在哪了,是调用的锅,用 Start-Process 带 Wait 参数就可以了。
    Start-Process -FilePath ffmpeg -ArgumentList $d -NoNewWindow -Wait
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4843 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 09:41 · PVG 17:41 · LAX 02:41 · JFK 05:41
    ♥ Do have faith in what you're doing.