python - 对for循环内的python子进程感到困惑

标签 python multithreading queue

我正在尝试使用 python 自动化一些大数据文件处理。

处理过程是链式的,即 script1 写入一个文件,然后由 script2 处理,然后 script2 的输出由 script3 等处理。

我在线程上下文中使用子进程模块。

我有一个类可以创建链式脚本的元组 (“scr1.sh”,“scr2.sh”,“scr3.sh”)。

然后另一个类使用类似的调用

for script in scriplist:
    subprocess.call(script)

我的问题是,在这个 for 循环中,每个脚本是否仅在 subprocess.call(script1) 返回成功的 retcode 之后调用?

或者是因为我使用的是 subprocess.call,所以这三个脚本都被依次调用,在不使用“ sleep ”或“等待”的情况下,我想确保第二个脚本仅在第一个脚本结束后启动.

编辑:pydoc 说 “subprocess.call(*popenargs, **kwargs) 运行带参数的命令。等待命令完成,然后返回 returncode 属性。”

因此,在 for 循环(上图)中,它是否会在迭代到下一个脚本之前等待每个重编码。

我是线程新手。我在此处附加了运行分析的类的精简代码。 subprocess.call 循环是此类的一部分。

class ThreadedDataProcessor(Thread):
            def __init__(self, in_queue, out_queue):
                # Uses Queue 
                Thread.__init__(self)
                self.in_queue = in_queue
                self.out_queue = out_queue
            def run(self):
                while True:
                    path = self.in_queue.get()
                    if path is None:
                        break
                    myprocessor = ProcessorScriptCreator(path)
                    scrfiles = myprocessor.create_and_return_shell_scripts()

                for index,file in enumerate(scrfiles):
                    subprocess.call([file])
                    print "CALLED%s%s" % (index,file) *5
                #report(myfile.describe())
                #report("Done %s" %  path)
                self.out_queue.put(path) 
                in_queue = Queue()

最佳答案

循环将连续调用每个脚本,等待其完成,然后调用下一个脚本,无论上一个调用成功或失败。您可能想说:

try:
  map(subprocess.check_call, script_list)
except Exception, e:
  # failed script

每次调用 run 时都会启动一个新线程,并在 run 完成时结束。您可以在一个线程中使用子进程迭代脚本。

您应该确保每个线程中的每组调用不会影响其他线程的其他调用。例如,尝试同时从多个线程中的脚本调用读取和写入同一文件。

关于python - 对for循环内的python子进程感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4029782/

相关文章:

python - 基本反向传播实现不起作用

python - 在 Python 中实现 Adams Bashforth Moulton 方法

java - 如何并行运行任务并在第一个任务失败时等待第二个任务,否则回复并让第二个任务运行

c# - 在 C# 中使用 BackgroundWorker 的线程问题

c# - 并行处理队列的好策略是什么?

Java Azure ServiceBusService listQueues() 上限为 100 个队列?

python - sphinx - 如何更改文档样式表

python - Pandas OHLCV 转 JSON 格式

printf 中的 C 段错误 - dl-lookup.c

arrays - Ruby 队列到数组