简体   繁体   English

如何访问while循环中正在更新的变量

[英]How to access a variable being updated in a while loop

I am trying to access a variable that is being constantly updated in a while loop in a different file. 我试图访问一个变量,该变量在另一个文件的while循环中不断更新。 Here is the code I used for testing: 这是我用于测试的代码:

# file1    
import time
x = 0
while True:
    x += 1
    time.sleep(2.0)

# file2
from file1 import x
print x

When I run file2, it starts the while loop from the beginning. 当我运行file2时,它将从头开始while循环。 I would like to access one instance of x. 我想访问x的一个实例。 For example, if x=10, I would want file2 to print 10. Is this possible? 例如,如果x = 10,我希望file2打印10。这可能吗?

I'm not quite sure what you mean. 我不太确定你的意思。 Are you wanting file1 to run, and be incrementing the value of "x" every 2 seconds, indefinitely and that when you run file2 at any time, it pulls the current value of "x" from the program/python instance running "file1"? 您是否要运行file1,并无限期地每2秒增加“ x”的值,并且在任何时候运行file2时,它都会从运行“ file1”的程序/ python实例中提取“ x”的当前值?

If so, this is not how you would approach it. 如果是这样,那么您就不会采用这种方式。 With file2 you are pulling the set variable x=0 from file1. 使用file2时,将从file1中拉出设置变量x = 0。 What you need to do is have some form of IPC (Inter-Process Communication) so that file2 can access the value of "x" from file1. 您需要做的是使用某种形式的IPC(进程间通信),以便file2可以从file1访问“ x”的值。 You can do this a multitude of ways, including shared memory, a key/value store program like redis or memcached, a database, etc. 您可以通过多种方式执行此操作,包括共享内存,键值/值存储程序(例如redis或memcached),数据库等。

If you want to do it via redis or memcached, simply run redis, use the redis library for Python, and call the .incr method for the key "x" every 2 seconds. 如果要通过redis或memcached进行操作,只需运行redis,将redis库用于Python,然后每2秒为密钥“ x”调用.incr方法。 Then, when you run file2, call the .get method for key "x" and you will get the current value. 然后,当您运行file2时,为键“ x”调用.get方法,您将获得当前值。 When file1 is running, it will continue to increment x; 当file1运行时,它将继续递增x; when it's not, it won't and will effectively freeze. 如果不是,它将不会并且将有效冻结。 However, redis will keep the last known value in memory for the key "x". 但是,redis会将密钥“ x”的最后一个已知值保留在内存中。

To do it with a database, you can implement a mySQL database/table and increase the value of "x" in a key column in a table every 2 seconds. 要对数据库执行此操作,可以实现mySQL数据库/表,并每2秒增加表的键列中“ x”的值。 You'd have to look at the mySQL libraries for Python. 您必须查看用于Python的mySQL库。

To do it with shared memory, look at the shared memory functions for Python. 要使用共享内存,请查看Python的共享内存功能。

There are also many other ways to share data. 还有许多其他共享数据的方式。 You could, simply, write the value of "x" to a file every 2 seconds by opening it, writing the new value, flushing and closing it. 您只需打开,写入新值,刷新并关闭它,就可以每2秒将“ x”的值写入文件。 Then simply have file2 read that file. 然后只需让file2读取该文件即可。 Of course with this you then have the issue of race conditions where it reads the file before its updated and gets a stale value, all dependent on the priority of the OS' filesystem writes for that file at that time from that process. 当然,这会带来竞争状况的问题,即在文件更新之前读取文件并获得陈旧的值,这全都取决于该进程当时操作系统对该文件写入文件的优先级。

You can try the following. 您可以尝试以下方法。 First, as there's and infinite loop, importing file1 will block, so you should run the loop in a thread. 首先,由于存在无限循环,因此file1的导入将被阻止,因此您应该在线程中运行循环。 And second you can wrap the integer being incremented in a list (or any other kind of objects), so you can use the reference to its current value (otherwise you will be importing a value not a reference): 其次,您可以将要递增的整数包装在列表(或任何其他类型的对象)中,以便可以使用对其当前值的引用(否则将导入一个值而不是引用):

# file1    
import time
import threading

x = [0]

def update_var(var):
    while True:
        var[0] += 1
        time.sleep(2.0)

threading.Thread(target=update_var, args=(x,)).start()

# file2
from file1 import x
print x[0]

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

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