Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从线程更新python全局变量_Python_Python 2.7 - Fatal编程技术网

从线程更新python全局变量

从线程更新python全局变量,python,python-2.7,Python,Python 2.7,我有一个配置文件,需要偶尔重新加载。 重载是在线程中运行的类的方法中调用的 我曾尝试使用globals()来实现这一点,但尽管printglobals()显示他们已经更新了,但程序没有看到更改 在使用它们之前,我尝试在所有地方添加全局变量name,但仍然一无所获 在那之后,我考虑使用\uuuuuuu main\uuuuu,因为我知道这是可行的,但我的问题是,当我的变量名是从文件读取的字符串以及值时,我无法设置\uuuuuuuuu main\uuuuuu变量 这是我的配置解析器: def pars

我有一个配置文件,需要偶尔重新加载。 重载是在线程中运行的类的方法中调用的

我曾尝试使用
globals()
来实现这一点,但尽管print
globals()
显示他们已经更新了,但程序没有看到更改

在使用它们之前,我尝试在所有地方添加
全局变量name
,但仍然一无所获

在那之后,我考虑使用
\uuuuuuu main\uuuuu
,因为我知道这是可行的,但我的问题是,当我的变量名是从文件读取的字符串以及值时,我无法设置
\uuuuuuuuu main\uuuuuu
变量

这是我的配置解析器:

def parse_config(path):
  # grab config.ini
  ini_file     = open(root_path + path)
  ini_contents = ini_file.read().replace('\r\n', '\n')
  ini_file.close()
  ini_list     = filter(None, ini_contents.split("\n"))
  ini_settings = [x for x in ini_list if not x.startswith('#')]

  # loop config file lines and set variables
  for line in ini_settings:
    splits = line.split('=', 1)
    name   = splits[0].strip()
    value  = splits[1].strip()

    value = value.replace('THIS/', root_path)
    if value[0] == ':':
      value = value[1:].split(',')
    elif str(value).lower() == 'true':
      value = True
    elif str(value).lower() == 'false':
      value = False

    globals().update({name:value})
示例ini文件

# Path to folder where error logs get written
error_log_path    = THIS/logs/

# Print in console (for debugging only)
logg_print        = True

# Log to file
logg_write        = True
logg_file_path    = THIS/logs/
主要应用程序示例

class NotifyHandler():

  def __init__(self):
    m = threading.Thread(target=self.process_send)
    m.setDaemon(True)
    m.start()

  def process_send(self):
    while True:
      parse_config('config.ini')
      print logg_print
      time.sleep(10)

NotifyHandler()
while True:
  time.sleep(10)

你能添加一个.ini文件的例子,这样我就可以正确地测试你的脚本了吗?您看到了哪些输出,可以让您知道globals已更新?此外,完成后您没有关闭.ini文件,该文件在globals(Py3.4)中添加了u____________;警告注册表项。我知道它已更改,因为我在第二次调用函数后添加了
print globals()
。但是在我添加了print logg_print之后,它是不变的。另外,我在线程中运行的类的方法中进行了第二次调用。您是否需要通过适当的开始值重新加载线程,或者让线程检查更新?如果没有看到更多的代码(甚至可能看到更多的代码),我真的不确定。感谢添加示例。我确实看到了globals的更新。我想你只是需要一些方法来通知线程。忘记线程安全。一本简单的字典就足够了。因为您只读xor写,所以不需要锁。见评论。