简体   繁体   English

两个python程序之间的通信

[英]Communication between two python programs

please help me understand why this doesn't work and how to change it.请帮助我理解为什么这不起作用以及如何更改它。 I basically want to be able to query values of a var of program one from program two.我基本上希望能够从程序二中查询程序一的 var 的值。 So here are the most basic programs:所以这里是最基本的程序:

P1: P1:

import time

gscore = 0

def get_info():
    return gscore


def main():
    global gscore
    score = 0

    while score <10:
        time.sleep(1)
        score +=1
        gscore = score
        print(score)

if __name__ == '__main__':
    main()

P2: P2:

from functest import get_info
print(get_info())

The structure may seem a bit weird but basically I have an existing small python game and want to be able to query the score which is why I adapted it in this way.结构可能看起来有点奇怪,但基本上我有一个现有的小型 python 游戏并且希望能够查询分数,这就是我以这种方式对其进行调整的原因。 So P1 obviously counts to ten but P2 always gets 0 as a return value.所以 P1 显然计数到 10 但 P2 总是得到 0 作为返回值。 I feel like I'm making a really stupid mistake here... Thx for help :)我觉得我在这里犯了一个非常愚蠢的错误......感谢帮助:)

When you import a module in Python you are not executing the content of it.当你在 Python 中import一个模块时,你并不是在执行它的内容。 if __name__ == "__main__": is there so that your Python files can act as either reusable modules, or as standalone programs. if __name__ == "__main__":是否可以让您的 Python 文件充当可重用模块或独立程序。

If you want to execute the main() function you'll need to explicitly call it.:如果要执行main()函数,则需要显式调用它。:

from functest import main, get_info
main() # calling the main function in your functest file
print(get_info()) # calling the get_info function

this will return the value you are looking for.这将返回您正在寻找的值。

Now, main will be called if you execute the functest.py file:现在, main如果执行functest.py文件将被称为:

$ python functest.py
  #=> 9

if __name__ == '__main__' is only executed when you run your code form a shell and not when you import your program from another program. if __name__ == '__main__'仅在您从 shell 运行代码时执行,而不是在从另一个程序导入程序时执行。 To fix this you can modify your program as:要解决此问题,您可以将程序修改为:

In you P1 modify get_info to this:在您 P1 中,将 get_info 修改为:

def get_info():
    global gscore
    return gscore

In your P2 do this:在您的 P2 中执行以下操作:

from functest import main, get_info
main()
print(get_info())

Also, note that there are better way of doing what you are doing like using a class instead of creating a global variable.另外,请注意,有更好的方法来做你正在做的事情,比如使用类而不是创建全局变量。

class Score(object):
   def __init__(self, gscore=0):
      self.gscore = gscore

   def get_info(self):
      return self.gscore

   def increment(self, score=0):
      while score < 10:
         score +=1
      self.gscore = score

When you import a function of another program, you are importing the functionality of the program, not the actual instance of the program.当您导入另一个程序的功能时,您是在导入该程序的功能,而不是该程序的实际实例。 What I mean by that is that your program only counts to 10 while it's running, but 10 isn't part of the code, it's a result.我的意思是你的程序在运行时只计数到 10,但 10 不是代码的一部分,它是结果。

What importing it allows you to do is run the bit of code you imported (In your case, running the get_info code with the gscore = 0 being the gscore that is returned, however, this is a distinct instance of your other program, so even if gscore is increasing in another program that you're running with the main method, your second program has it's own gscore that is 0 . Importing is more for when you have a sort of library of functions in a program that you want access to.导入它允许你做的是运行你导入的代码(在你的情况下,运行get_info代码, gscore = 0是返回的gscore ,但是,这是你其他程序的一个不同实例,所以即使如果gscore在您使用 main 方法运行的另一个程序中增加,则您的第二个程序有它自己的gscore0 。当您想要访问的程序中有某种函数库时,导入更多。

What you want is to be able to, run time, read a value that the other program has generated.您想要的是能够在运行时读取其他程序生成的值。 This can't be done with python variables, but it can be done in many other ways.这不能用 python 变量来完成,但可以通过许多其他方式来完成。 A common way is to write the score to a file, then read the file in your other program.一种常见的方法是将乐谱写入文件,然后在其他程序中读取该文件。 If you need information on how to read and write files, read this guide here .如果您需要有关如何读取和写入文件的信息,请在此处阅读本指南

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

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