简体   繁体   English

Python子流程模块-变量范围

[英]Python subprocess module - variable scope

My python script have weird behavior. 我的python脚本有奇怪的行为。 So, I have python script A, which calls another script B many times. 所以,我有python脚本A,它多次调用另一个脚本B。 For calling B, I'm using subprocess module. 为了调用B,我正在使用子流程模块。

Snippets of script A: 脚本A的片段:

for i in range(0,10000):
    parameters = []
    parameters.append("B")
    result = subprocess.call(parameters)

Snippets of script B: 脚本B的片段:

testdata = some_logic
if testdata:
    function_1()
else:
    function_2()

So, script A will calls script B many times. 因此,脚本A将多次调用脚本B。 After some tests, I noticed that variable testdata doesn't have expected value for current running script B. Is it somehow possible in running script B, that variable testdata has value from previous call B? 经过一些测试后,我注意到变量testdata没有当前正在运行的脚本B的期望值。在运行脚本B中,以某种方式实现变量testdata的值来自于先前的调用B吗? What is scope for variable testdata in this case? 在这种情况下,变量testdata的范围是什么? Thanks for advice. 谢谢你的建议。 Cheers 干杯

subprocess.call starts a child process. subprocess.call启动一个子进程。 That process knows nothing about what is going on in A or how many times it has been called -- All it knows is what commandline arguments you've passed to it. 该过程对于A正在发生的事情或被调用了多少次一无所知-它只知道您传递给它的命令行参数是什么。 In this case, you're always calling 'B' with no commandline arguments since parameters always equals ['B'] when you call subprocess.call . 在这种情况下,您总是不使用命令行参数就调用'B' ,因为调用subprocess.callparameters总是等于['B']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM