gpt4 book ai didi

python - 等到嵌套的 python 脚本结束,然后再继续当前的 python 脚本

转载 作者:太空狗 更新时间:2023-10-30 02:48:41 25 4
gpt4 key购买 nike

我有一个调用另一个 python 脚本的 python 脚本。在另一个 python 脚本中,它会生成一些线程。如何让调用脚本等待被调用脚本完全运行完毕?

这是我的代码:

while(len(mProfiles) < num):
print distro + " " + str(len(mProfiles))
mod_scanProfiles.main(distro)
time.sleep(180)
mProfiles = readProfiles(mFile,num,distro)
print "yoyo"

如何等到 mod_scanProfiles.main() 和所有线程都完成? (我现在使用 time.sleep(180) 但它不是一个好的编程习惯)

最佳答案

您想修改 mod_scanProfiles.main 中的代码以阻塞,直到它的所有线程都完成。

假设您在该函数中调用 subprocess.Popen 只需执行以下操作:

# in mod_scanPfiles.main:
p = subprocess.Popen(...)
p.wait() # wait until the process completes.

如果您当前没有在等待您的线程结束,您还需要调用 Thread.join ( docs ) 来等待它们完成。例如:

# assuming you have a list of thread objects somewhere
threads = [MyThread(), ...]
for thread in threads:
thread.start()
for thread in threads:
thread.join()

关于python - 等到嵌套的 python 脚本结束,然后再继续当前的 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11883903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com