3

Alright so I want to know if doing this would affect the code, and if I'm doing it correctly.

So basically, let's say in one file I have a dictionary called commands (inside a class), and in another an object of the other class is made, and the dictionary is used. During run-time, I edit the dictionary and add new functions. Now I need to reload the dictionary without having to restart the whole script (because this would affect a lot of people using my services). If I send a signal to the script (it's a socket server) that indicates that the dictionary should be reloaded. How would I re-import the module after it's already imported mid-code? and would re-importing it affect the objects made of it, or do I have to somehow reload the objects? (note that the objects contain an active socket, and I do not wish to kill that socket).

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
icecreamscoop
  • 71
  • 2
  • 10
  • are you sure it's not "reloading" automatically? If you have a reference to `foo.my_dict` in `bar.py`, and you modify `foo.my_dict`, `bar`'s output will reflect that. – Adam Smith Sep 23 '15 at 17:30
  • 2
    I'm not editing it like my_dict["key"] = "value", but I actually edit the FILE itself, that's why I need to reload it in a way or another. – icecreamscoop Sep 23 '15 at 17:48

1 Answers1

0

It is better to store the data in a database, like Redis which supports dictionary-like data. This way you can avoid the problem of reloading altogether, as the database process makes sure the fetched data is always up-to-date.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • 1
    I would, but the dictionary is str:func, and I can't store python functions in a database. – icecreamscoop Sep 23 '15 at 17:47
  • How about this: you don't store the function object, but the function source code. You can also serialize Python functions http://stackoverflow.com/a/1253813/315168 – Mikko Ohtamaa Sep 23 '15 at 17:51