gpt4 book ai didi

python - 如何将值传递给 Popen.subprocess 内的方法参数?

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:26 24 4
gpt4 key购买 nike

这是我的主要 python 脚本:

import time
import subprocess
def main():
while(True):
a=input("Please enter parameter to pass to subprocess:")
subprocess.Popen(args="python child.py")
print(f"{a} was started")
time.sleep(5)
if __name__ == '__main__':
main()

这是名为 child.py 的 python 子脚本:

def main(a):
while(True):
print(a)

if __name__ == '__main__':
main(a)

如何给子进程中的参数a传值?

最佳答案

你需要使用命令行参数,像这样;

import time
import subprocess

def main():
while(True):
a=input("Please enter parameter to pass to subprocess:")
subprocess.Popen(["python", "child.py", a])
print(f"{a} was started")
time.sleep(5)

if __name__ == '__main__':
main()

child .py:

import sys

def main(a):
while(True):
print(a)

if __name__ == '__main__':
a = sys.argv[1]
main(a)

关于python - 如何将值传递给 Popen.subprocess 内的方法参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52478339/

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