1. <small id='aQJoR'></small><noframes id='aQJoR'>

  2. <i id='aQJoR'><tr id='aQJoR'><dt id='aQJoR'><q id='aQJoR'><span id='aQJoR'><b id='aQJoR'><form id='aQJoR'><ins id='aQJoR'></ins><ul id='aQJoR'></ul><sub id='aQJoR'></sub></form><legend id='aQJoR'></legend><bdo id='aQJoR'><pre id='aQJoR'><center id='aQJoR'></center></pre></bdo></b><th id='aQJoR'></th></span></q></dt></tr></i><div id='aQJoR'><tfoot id='aQJoR'></tfoot><dl id='aQJoR'><fieldset id='aQJoR'></fieldset></dl></div>

    <legend id='aQJoR'><style id='aQJoR'><dir id='aQJoR'><q id='aQJoR'></q></dir></style></legend><tfoot id='aQJoR'></tfoot>

      • <bdo id='aQJoR'></bdo><ul id='aQJoR'></ul>
    1. subprocess.wait() 不等待 Popen 进程完成(使用线程时)?

      subprocess.wait() not waiting for Popen process to finish (when using threads)?(subprocess.wait() 不等待 Popen 进程完成(使用线程时)?)

        <tbody id='KVULy'></tbody>
      <tfoot id='KVULy'></tfoot>
        <bdo id='KVULy'></bdo><ul id='KVULy'></ul>

          • <i id='KVULy'><tr id='KVULy'><dt id='KVULy'><q id='KVULy'><span id='KVULy'><b id='KVULy'><form id='KVULy'><ins id='KVULy'></ins><ul id='KVULy'></ul><sub id='KVULy'></sub></form><legend id='KVULy'></legend><bdo id='KVULy'><pre id='KVULy'><center id='KVULy'></center></pre></bdo></b><th id='KVULy'></th></span></q></dt></tr></i><div id='KVULy'><tfoot id='KVULy'></tfoot><dl id='KVULy'><fieldset id='KVULy'></fieldset></dl></div>
              • <small id='KVULy'></small><noframes id='KVULy'>

              • <legend id='KVULy'><style id='KVULy'><dir id='KVULy'><q id='KVULy'></q></dir></style></legend>
                本文介绍了subprocess.wait() 不等待 Popen 进程完成(使用线程时)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在使用 subprocess.Popen() 从我的 python 脚本中使用线程生成同一应用程序的多个实例以使它们同时运行时遇到一些问题.在每个线程中,我使用 popen() 调用运行应用程序,然后通过调用wait() 等待它完成.问题似乎是 wait() 调用实际上并没有等待进程完成.我尝试只使用一个线程,并在进程开始和结束时打印出文本消息.所以线程函数看起来像这样:

                I am experiencing some problems when using subprocess.Popen() to spawn several instances of the same application from my python script using threads to have them running simultaneously. In each thread I run the application using the popen() call, and then I wait for it to finish by callingwait(). The problem seems to be that the wait()-call does not actually wait for the process to finish. I experimented by using only one thread, and by printing out text messages when the process starts, and when it finishes. So the thread function would look something like this:

                def worker():
                    while True:
                        job = q.get() # q is a global Queue of jobs
                        print('Starting process %d' % job['id'])
                        proc = subprocess.Popen(job['cmd'], shell=True)
                        proc.wait()
                        print('Finished process %d' % job['id'])
                        job.task_done()
                

                但即使我只使用一个线程,它也会在出现任何已完成的过程..."消息之前打印出几条正在启动过程..."消息.是否存在 wait() 实际上不等待的情况?我有几个不同的外部应用程序(C++ 控制台应用程序),这些应用程序又会同时运行多个实例,对于其中一些,我的代码可以工作,但对于其他一些则不行.外部应用程序是否存在某些问题会以某种方式影响对 wait() 的调用?创建线程的代码如下所示:

                But even when I only use one thread, it will print out several "Starting process..." messages, before any "Finished process..." message appears. Are there any cases when wait() does not actually wait? I have several different external applications (C++ console applications), which in turn will have several instances running simultaneously, and for some of them my code works, but for others it won't. Could there be some issue with the external applications that somehow affects the call to wait()? The code for creating the threads looks something like this:

                for i in range(1):
                    t = Thread(target=worker)
                    t.daemon = True
                    t.start()
                q.join() # Wait for the queue to empty
                

                更新 1:我还应该补充一点,对于某些外部应用程序,我有时会得到 -1073471801 的返回码 (proc.returncode).例如,其中一个外部应用程序将在前两次调用 Popen 时给出该返回码,但不是最后两次(当我有四个作业时).

                Update 1: I should also add that for some of the external applications I sometimes get a return code (proc.returncode) of -1073471801. For example, one of the external applications will give that return code the first two times Popen is called, but not the last two (when I have four jobs).

                更新 2:澄清一下,现在我在队列中有四个作业,它们是四个不同的测试用例.当我运行我的代码时,对于其中一个外部应用程序,前两个 Popen 调用会生成返回码 -1073471801.但是,如果我打印 Popen 调用的确切命令,并在命令窗口中运行它,它会毫无问题地执行.

                Update 2: To clear things up, right now I have four jobs in the queue, which are four different test cases. When I run my code, for one of the external applications the first two Popen-calls generate the return code -1073471801. But if I print the exact command which Popen calls, and run it in a command window, it executes without any problems.

                解决了!我设法解决了我遇到的问题.我认为问题在于我缺乏线程编程方面的经验.我错过了这样一个事实,即当我创建我的第一个工作线程时,它们会继续存在直到 python 脚本退出.每次我将新项目放入队列时,我都会错误地创建更多工作线程(我为每个要运行的外部程序分批执行此操作).所以当我到达第四个外部应用程序时,我有四个线程同时运行,尽管我只以为我有一个.

                Solved! I managed to solve the issues I was having. I think the problem was my lack of experience in threaded programming. I missed the fact that when I had created my first worker threads, they would keep on living until the python script exits. By mistake I created more worker threads each time I put new items on the queue (I do that in batches for every external program I want to run). So by the time I got to the fourth external application, I had four threads running simultaneously even though I only thought I had one.

                推荐答案

                你也可以使用 check_call() 而不是 Popen.check_call() 等待命令完成,即使 shell=True 然后返回作业的退出代码.

                You could also use check_call() instead of Popen. check_call() waits for the command to finish, even when shell=True and then returns the exit code of the job.

                这篇关于subprocess.wait() 不等待 Popen 进程完成(使用线程时)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
              • <tfoot id='Jb9xR'></tfoot>

                  <i id='Jb9xR'><tr id='Jb9xR'><dt id='Jb9xR'><q id='Jb9xR'><span id='Jb9xR'><b id='Jb9xR'><form id='Jb9xR'><ins id='Jb9xR'></ins><ul id='Jb9xR'></ul><sub id='Jb9xR'></sub></form><legend id='Jb9xR'></legend><bdo id='Jb9xR'><pre id='Jb9xR'><center id='Jb9xR'></center></pre></bdo></b><th id='Jb9xR'></th></span></q></dt></tr></i><div id='Jb9xR'><tfoot id='Jb9xR'></tfoot><dl id='Jb9xR'><fieldset id='Jb9xR'></fieldset></dl></div>
                  • <bdo id='Jb9xR'></bdo><ul id='Jb9xR'></ul>
                    1. <legend id='Jb9xR'><style id='Jb9xR'><dir id='Jb9xR'><q id='Jb9xR'></q></dir></style></legend>

                            <tbody id='Jb9xR'></tbody>

                          <small id='Jb9xR'></small><noframes id='Jb9xR'>