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 manage remote servers in batch in Fabric

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you how to manage remote servers in batches in Fabric. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Fabric combines multiple servers through Group. The difference lies in the two subclasses derived from the fabric.group.Group base class (parent class): SerialGroup (* hosts, * * kwargs): ThreadingGroup (* hosts, * * kwargs): perform operations concurrently.

Let's take a look at this base class:

I folded some useless information, and the more noteworthy contents are as follows: Group inherits list, so it can extend (). The run () method that sets up the core of connection for the incoming server does not write the implementation. The intention is to leave the subclass to implement the final _ _ enter__ () and _ _ exit__ () to implement the context manager.

With this base class, the next step is to look at the implementation of SerialGroup and ThreadingGroup.

The SerialGroup class is simple and implements only one run () method. Because the class has established a connection for all host and saved it during initialization, you just need to use the for loop to take it out in turn and execute the run () method of Connection.

Here you can see a very practical development technique: when creating a class, let it inherit the built-in data structures (such as list, dict), so that you can directly use self.append (), self.extend (), self.update () and other methods to save critical information to "yourself", and then "for xxx in self" when you take it out, so as not to create a temporary list or dict, and avoid passing it back and forth in the parameters.

GroupResult and GroupException are the handling of execution results and exceptions, which are not the focus of our attention, which are skipped here.

Next, look at ThreadingGroup, which also has only one run () method:

ExceptionHandlingThread is a class that inherits threading.Thread, which is a way to create multithreading. The method executed by each thread mainly does two things: execute the run () method of connection, and store the results of successful execution in the queue.

Then the results of successful execution and the results of exceptions are stored in results respectively.

Therefore, Fabric uses threading multithreading to achieve concurrency. Web requests are IO-intensive, and multithreading is a good way to do so.

At this point, we have a preliminary answer to the question we asked at the beginning: Fabric encapsulates two kinds of Group to manage servers in batches, in which the serial mode uses a simple for loop, while the concurrent mode uses threading multithreading.

However, by analyzing the implementation code of these two kinds of Group (and the practice used), we can also find the defect of Fabric: Group only implements the run () method, but Connection's put (), get (), sudo () and other methods are not available, which means that when managing a server cluster in this way, you can only execute shell commands on it. Every time the run () method is called, it waits for all hosts to finish execution before it returns the result, which means that the host that finishes first is blocked. More fatally, if an exception occurs while one of the hosts is executing, the entire run () method throws an exception, which means that every time you use the run () method, you need to catch an exception. The run () method supports the execution of a single shell command, but the state of the command is not passed. Suppose you run the cd command in a run () method to cut to the A directory (not the root directory), and then create a file in the next run () method. The end result is that the file is not in the A directory, but in the default directory. The solution is to connect multiple commands with "&", which is a little troublesome.

The above is how to manage remote servers in batches in Fabric. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report