首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何避免Python子进程停止执行

如何避免Python子进程停止执行
EN

Stack Overflow用户
提问于 2021-02-25 15:37:30
回答 1查看 35关注 0票数 0

我有一个处理大量文件的python程序,其中一个步骤是通过一个.JAR文件完成的,目前我有类似的东西

代码语言:javascript
复制
    for row in rows:
        try:
            subprocess.check_call(f'java -jar ffdec/ffdec.jar -export png "{out_dir}/" "{row[0]}.swf", stdout=subprocess.DEVNULL)
        except (OSError, subprocess.SubprocessError, subprocess.CalledProcessError):
            print(f"Error on {row[0]}")
            continue

这可以很好地执行os命令(我在Windows10上),并且不会在出现错误时停止。但是,有一个特定的错误会停止我的python程序的执行。我认为这是因为.jar文件并没有真正停止,仍然在后台运行,从而阻止了python继续运行。

我有没有办法在Python中调用命令并异步运行,或者在20秒超时后跳过它?我也可以编写一个Java程序来运行该过程的这一部分,但为了方便起见,我更喜欢将所有内容都放在Python上

为了以防万一,我将把停止我的程序的错误放在这里(所有其他的都会被try: except:正确捕获)

代码语言:javascript
复制
f�vr. 25, 2021 8:05:00 AM com.jpexs.decompiler.flash.console.ConsoleAbortRetryIgnoreHandler handle
GRAVE: Error occured
java.util.EmptyStackException
    at java.util.Stack.peek(Unknown Source)
    at com.jpexs.decompiler.flash.exporters.commonshape.SVGExporter.addUse(SVGExporter.java:230)
    at com.jpexs.decompiler.flash.timeline.Timeline.toSVG(Timeline.java:1043)
    at com.jpexs.decompiler.flash.exporters.FrameExporter.lambda$exportFrames$0(FrameExporter.java:216)
    at com.jpexs.decompiler.flash.RetryTask.run(RetryTask.java:41)
    at com.jpexs.decompiler.flash.exporters.FrameExporter.exportFrames(FrameExporter.java:220)
    at com.jpexs.decompiler.flash.console.CommandLineArgumentParser.parseExport(CommandLineArgumentParser.java:2298)
    at com.jpexs.decompiler.flash.console.CommandLineArgumentParser.parseArguments(CommandLineArgumentParser.java:891)
    at com.jpexs.decompiler.flash.gui.Main.main(Main.java:1972)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-25 17:03:38

在查看了深入的子流程文档后,我发现了一个名为timeout的参数:

代码语言:javascript
复制
subprocess.check_call('...', stdout=subprocess.DEVNULL, timeout=20)

它能帮我做这件事

Documentation for timeout

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66364209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档