Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to do the hot update of Python in the game

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to update Python in the game. I hope you will get something after reading this article. Let's discuss it together.

Principle:

1. Standard import

We all know that Python provides import to import a standard python module, load the module into memory, and add it to sys.modules. But multiple import the same module will only import the name into the current Local namespace, that is, a module will not be loaded repeatedly, so it is not possible to rely on this feature to be hot. This road is impassable. Please think differently.

2.reload function

The reload () function reloads the imported module, which seems to be a hot update to Python's code. However, the native reload function of python is too simple to support the hot update requirements of the game for several main reasons: the module reloaded by reload will not replace the old version of the module, that is, the old module that has been referenced cannot be updated also because it cannot be referenced by the old object, using from. Import... After the module referenced by the method cannot update reloas (m), the instance object of class and its derived class still uses the old class definition. At the same time, when loading the module fails, there is no rollback mechanism, resulting in the need to re-import the module, so, combined with the hot update requirements of the game, customize the appropriate reload. The purpose of the new custom reload is to enable the program to dynamically load the changed code without the end of the original program. The main purpose is to achieve the following two points: improve development efficiency and repair emergency BUG without restarting the game

Achieve:

The core requirement of a hot update is to have the python interpreter execute the latest code while ensuring that there are no problems with other associated modules. Refreshing method defined in function,class is relatively easy to implement, but for refreshing variables defined in module, variables defined in class, and newly added member variables, a unified convention is required. Therefore, in the process of implementing hot updates, we need to consider code updates and data updates. Here is a list of the features of the new reload:

1. Update code definition (function/method/static_method/class_method) does not update data (all types except the code definition are treated as data) the reload_module interface is specified in module, and the reload_class interface is specified in class. Data updates are handled manually in these two interfaces, and there are more conventions and interfaces to complete the contents of the replacement function object.

# update the contents of the old function object with the new function object, and keep the address of the function object unchanged def update_function (oldobj, newobj, depth=0): setattr (oldobj, "func_code", newobj.func_code) setattr (oldobj, "func_defaults", newobj.func_defaults) setattr (oldobj, "func_doc", newobj.func_doc)

two。 Replace the contents of the class

# update the old class content with the new class content, and keep the address of the old class def _ update_new_style_class (oldobj, newobj, depth): handlers = get_valid_handlers () for k, v in newobj.__dict__.iteritems (): # if the new key is not in the old class Added if k not in oldobj.__dict__: setattr (oldobj, k, v) _ log ("[A]% s:% s"% (k, _ S (v), depth) continue oldv = oldobj.__dict__ [k] # if the key object type is different between the old and new class The object if type (oldv)! = type (v): _ log ("[RD]% s:% s"% (k, _ S (oldv)) that retains the old class) Depth) continue # updates the objects currently supported for update v_type = type (v) handler = handlers.get (v_type) if handler: _ log ("[U]% s:% s"% (k, _ S (v), depth) handler (oldv, v) Depth + 1) # because it is to directly change the content of oldv So no more setattr. Else: _ log ("[RC]% s:% s:% s"% (k, type (oldv), _ S (oldv)), depth) # calls the reload_class interface of the convention Replacement logic for handling class variables object_list = gc.get_referrers (oldobj) for obj in object_list: # only those of the same type are instance objects of the class if obj.__class__.__name__! = oldobj.__name__: continue if hasattr (obj, "x_reload_class"): obj.x_reload_class ()

3.staticmethod

Def _ update_staticmethod (oldobj, newobj, depth): # A staticmethod object whose sm.__get__ (object) is the function object oldfunc = oldobj.__get__ (object) newfunc = newobj.__get__ (object) update_function (oldfunc, newfunc, depth)

4.classmethod

Def _ update_classmethod (oldobj, newobj, depth): oldfunc = oldobj.__get__ (object). Im_func newfunc = newobj.__get__ (object). Im_func update_function (oldfunc, newfunc, depth)

The update of the module is also similar, do not paste one by one, but improve on the basis of the original reload, for the module hot update, but also agreed on a reload_module interface, you can customize the update of data. Add some use cases below:

Def x_reload_class (self): after a hot update, each instance of the re-object executes this function because the replacement of the old and new objects will not call the constructor again, so it is necessary to perform initialization logic on the hot updated class objects to deal with the new and old variables. Function execution environment repair "" self._new_var = 5000 # initialization of new variables self.runLogic () # New repair logic after reading this article, I believe you have a certain understanding of "Python in the game how to hot update processing", if you want to know more related knowledge, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report