python - 如何将值传递给 Popen.subprocess 内部的方法参数?
问题描述
这是我的主要 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()
孩子.py:
import sys
def main(a):
while(True):
print(a)
if __name__ == '__main__':
a = sys.argv[1]
main(a)
推荐阅读
- c++ - 如何使用 for 循环遍历函数/类中的数组
- embedded - 这个 PIC16F877A HCSR04 示例有什么问题?
- solidity - LP Staking solidity 合约:我应该设置什么奖励率?
- php - 如何在 Yii 2.0.43 版和 bootstrap4 中启用 select2 小部件的搜索控件
- haskell - 该怎么做,我可以在 Haskell 中导入 Data.List.Split 模块
- r - 从邻接矩阵中快速计算任意一对节点之间的共享邻居数
- css - Bootstrap 网格系统 1 行两列成 2 行
- node.js - 如何在 mongodb 中获取具有唯一发件人的文档?
- ios - 将 Apple Sign In 与 React Native Firebase 和 Firestore 一起使用时,如何检查用户是否存在?
- javascript - JSON 中的 C3 条形图 - 可以显示 x 和值键之外的 json 属性吗?