【问题标题】:Waiting for two subprocesses to finish but not necessarily waiting for first等待两个子进程完成但不一定要先等待
【发布时间】:2015-06-24 06:36:16
【问题描述】:

我正在编写一个创建两个新进程的程序,并且必须等待它们都完成才能继续。如何启动两个进程并让程序等待两者退出?考虑伪代码:

我目前有:

create_process("program1.exe").wait()
create_process("program2.exe").wait()

这很好,因为program2可以与program1同时运行。

create_process("program1.exe")
create_process("program2.exe").wait()

这可能是错误的,因为 program1 可能需要比 program2 更长的时间。

我对通用解决方案感兴趣,我敢打赌已经发明了算法或设计模式来处理这些问题。但是为了给这个问题添加上下文,我正在编写一个调用 pgsql2shp.exe 两次的 Python 脚本,以将两个表从数据库导出到本地机器,然后执行一个交集。该脚本使用 Python 2.7 编写,使用 subprocess.popen

【问题讨论】:

  • 那么你是怎么想象的呢?您不能保证 2 个过程将花费相同的确切时间。如果您想等待两者都完成,则总会出现一种情况,即其中一个完成得比另一个完成得早。
  • @matino 是的,我想等待两者,但我希望在等待它们之前都启动它们......所以我想我应该在两者都启动后调用 wait() 。谢谢

标签: python concurrency process language-agnostic processing-efficiency


【解决方案1】:

如何使用线程? 如果你启动几个线程,每个线程可以独立运行,当它们完成时你可以加入线程。

试试这样的代码:(这段代码被大量注释,以便您可以了解正在发生的事情)

# Import threading
import threading

# Create a handler class.
# Each instance will run in it's own independent thread
class ThreadedHandler (threading.Thread):

    # This method is called when you call threadInstance.start()
    def run(self):

        # Run your sub process and wait for it
        # How you run your process is up to you
        create_process(self.programName).wait()

# Create a new thread object
thread1 = ThreadedHandler()

# Set your program name so that when the thread is started
# the correct process is run
thread1.programName = 'program1.exe'

# Start the thread
thread1.start()

# Again, create a new thread object for the 2nd program
thread2 = ThreadedHandler()

# Set the program name
thread2.programName = 'program2.exe'

# Start the thread
thread2.start()

# At this point, each program is running independently in separate threads
# Each thread will wait for their respective sub process to complete

# Here, we join both threads. (Wait for both threads to complete)
thread1.join()
thread2.join()

# When we get here, both of our programs are finished and they both ran in parallel

【讨论】:

  • 你甚至可以使用与threading具有几乎相同API的multiprocessing
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-06
  • 2014-11-08
相关资源
最近更新 更多