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 realize hot code update

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The main content of this article is "how to achieve hot code update", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to achieve hot code update"!

Monkey patch

Monkey patch (monkey patch) you should have heard of, this is a technique to add and modify code at run time, without changing the source code.

Json serialization is a common operation, which can be done in Python:

Import json json.dumps (some_data)

Ujson is another json serialization implementation written in pure C, which is more efficient than the json module in the standard library and uses the same:

Import ujson ujson.dumps (some_data)

So, what if you want to replace all json operations in the entire program with ujson?

It is certainly not possible to reference ujson directly, because the program may refer to a third-party class library, and we certainly do not want to change the third-party code. Take an api implemented by the flask framework as an example

From flask import Flask, jsonify app = Flask (_ _ name__) @ app.route ('/') def some_api (): return jsonify (some_data)

The jsonify function is used to respond to json data. It calls the standard library json module to json serialize the data, but flask is not developed by us.

Fortunately, taking advantage of the dynamic nature of the Python execution process, we can replace the relevant function implementation of the json module at run time. Next, we write the patch_json function to replace the dumps and loads functions:

Import json import ujson def patch_json () json.dumps = ujson.dumps json.loads = ujson.loads patch_json ()

In this way, as long as the patch_json function executes successfully, the dumps and loads functions in the json module are replaced with the ujson version. Even if the subsequent import from the json module, the final result is the ujson version!

It is important to note that the json module attribute is introduced directly before the patch_json call and will not be controlled by patch_json:

Import json from json import dumps patch_json () # executes the original version of the json module instead of the ujson version dumps (some_data) # executes the ujson version json.dumps (some_data)

As a result, many programs that apply monkey patches perform replacement logic at the beginning to ensure that similar phenomena do not occur.

Monkey patches have a wide range of applications and are generally used to replace class library implementations or to mock in unit tests. For example, greenlet uses monkey patches to replace blocked library functions with non-blocking versions:

Import gevent.monkey gevent.monkey.patch_all ()

Since monkey patches may affect the readability of the code, improper application may lead to some strange problems, so you can't abuse them.

At this point, I believe that everyone has a deeper understanding of "how to achieve hot code updates". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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