让python等待subprocess.call完成命令 [英] Making python wait until subprocess.call has finished its command

查看:623
本文介绍了让python等待subprocess.call完成命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,此问题来自

So this question is following on from this (Read comments aswell as that is the path I took). I just want the first call robocopy to finish executing before moving on to the rest of the code. As I want the second robocopy to just skip all of the files as they have already been copied over. However what is happening is that the rest of the script will run (ie starts the second robocopy) while the first robocopy is copying over the files. Below is the code:

call(["start", "cmd", "/K", "RoboCopy.exe", f"{self.srcEntry.get()}", f"{self.dstEntry.get()}", "*.*", "/E", "/Z", "/MT:8"], stdout=PIPE, shell=True) 
temp2 = Popen(["RoboCopy.exe", f"{self.srcEntry.get()}", f"{self.dstEntry.get()}", "*.*", "/E", "/Z"], stdout=PIPE, stdin=PIPE, shell=True)

在复制大文件时,该问题很明显.我正在考虑放置一个睡眠功能,该功能取决于要复制的文件的总大小.但是,由于文件将通过网络传输,因此未考虑上传/下载速度.

Issue is noticeable when copying over large files. I'm thinking about putting in a sleep function which is dependent upon the total size of the files to be copied over. However this doesn't take into account upload/download speeds as the files will be transferred over a network.

推荐答案

我发现的结果:感谢QuantumChris.我发现robocopy从终端返回并返回我的脚本,尽管我使用了subprocess.run,它应该暂停我的脚本直到运行完成.我通过检查文件是否已复制到目标文件夹,然后再进行第二次robocopy来阻止第二次robocopy的运行.问题是,如果最后一个文件很大,则os.path.isfile()会检测到文件 WHILE ,仍在复制该文件.因此,它会使用第二个robocopy,但是第二个robocopy不会检测到最后一个文件,因此尝试将文件复制过来,但是发现它无法访问该文件,因为该文件已被使用(由第一个robocopy),因此它等待30秒后再试一次. 30秒后,便可以访问文件并进行复制了.我现在想做的是使我的最后一个文件为空的虚拟文件,我不关心它是否被复制两次,因为它是空的. Robocopy似乎是按照ASCII顺序复制文件的.所以我将文件命名为~~~~~ .txt:D

What I've found out: Thanks to QuantumChris. I've found out that robocopy returns from the terminal and back into my script although I've used subprocess.run which should have paused my script until it had finished running. I'm stalling the second robocopy from running by checking if the files have been copied over to the destination folder before progressing with the second robocopy. The issue is that if the last file is big then os.path.isfile() detects the file WHILE it is still being copied over. So it engages the second robocopy however the second robocopy doesn't detect that last file and so attempts to copy the file over but recognises that it can't access the file as it is already in use (by the first robocopy) so it waits 30 secs before trying again. After the 30 secs it is able to access the file and copies it over. What I would like to do now is to make my last file an empty dummy file which I don't care about it being copied twice as it is empty. Robocopy seems to copy over the files according to ASCII order. So I've named the file ~~~~~.txt :D

这篇关于让python等待subprocess.call完成命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆