reload() does not appear to reload the module

时光怂恿深爱的人放手 提交于 2019-12-24 14:15:18

问题


I have a script that writes some parameters to a config file dynamically and I need to call some functions from a linked module based on the updated parameters. However, when I call reload() on the config file, sometimes I see no change.

The following code snippet will explain the scenario:

import options
import os
import someothermodule

def reload_options():
    global options
    options = reload(options)

def main():
    print dir(options)

    # do some work to get new value of the parameter
    new_value = do_some_work()

    with open('./options.py', 'w') as fd_out:
        fd_out.write('NEW_PARAMETER = %d\n' % (new_value,))  # write

        fd_out.flush()
        os.fsync(fd_out.fileno())

    reload_options()
    print dir(options)

    someothermodule.call_some_func()

if __name__ == '__main__':
    main()

Sometimes (this does not occur always), same data is printed at both print statements, which meant that NEW_PARAMETER never showed up. I suspected this is because the file is not getting flushed to the disk, so I added flush() and fsync() statements, but they do not seem to help.

Can anybody help me diagnose the issue?


回答1:


The problem is likely to do with the files having the same creation date. See this SO question : Python's imp.reload() function is not working?

I was able to get this code working by inserting a sleep statement:

   # replace NEW_PARAMETER in options.py with numbers in the range 0-9
   for ii in range(10):
        new_value = ii

        # Sleep here to let the system clock tick over
        time.sleep(1)

        with open('./options.py', 'w') as fd_out:
            fd_out.write('NEW_PARAMETER = %d\n' % (new_value,))  # write                                                 
            fd_out.flush()
            os.fsync(fd_out.fileno())

        reload_options()
        print ii,options.NEW_PARAMETER 



回答2:


Rather than relying on reload, why not just add/modify the attribute on the module directly for current use as well as output it to file for future use?

import options
import os
import someothermodule

def main():
    # do some work to get new value of the parameter
    new_value = do_some_work()

    # assign value for now
    options.NEW_PARAMETER = new_value

    # store value for later
    with open('./options.py', 'w') as fd_out:
        fd_out.write('NEW_PARAMETER = {}'.format(new_value))

    print dir(options)

    someothermodule.call_some_func()


来源:https://stackoverflow.com/questions/22952026/reload-does-not-appear-to-reload-the-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!