我有一个Python程序,该程序设计为systemd守护程序,并在程序收到Vertical Nav
信号时重新读取其配置文件。这些配置文件的解析内容保存在全局列表中。程序的整体结构如下:
SIGHUP
如上所述,我正在使用全局列表类型变量def read_confs(signalNumber, frame):
# Set the conf_files variable, print messages about
# received signal, etc and finally call the create_threads()
create_threads(conf_files)
def create_threads(conf_files):
global threads
threads = []
# Read the conf files and build the "threads" array.
return threads
def main():
global threads
threads = create_threads(conf_files)
while True:
# Process the "threads" list, call other functions, and endlessly create
# sub-processes based on "threads" list.
if __name__ == "__main__":
signal.signal(signal.SIGHUP, read_confs)
main()
,该变量在程序启动时和每次程序接收到threads
时都会填充。似乎工作正常,但正如我所读的,使用全局变量可能很危险。像这样使用全局变量可以吗?有没有更优雅/更稳健的方法来达到相同的结果?