Wxpython learning] using the pubsub mechanism to update views

Source: Internet
Author: User

Wxpython learning] using the pubsub mechanism to update views

Recently, I was busy working on a tool to generate xml packets for testing. In this tool, I have several panels that exist at the same time. One panel is used to manage data dictionaries. It can be edited. Other panels contain a display of these data dictionaries and cannot be edited. They reflect the same data source. At the same time, this tool can also import the transaction table according to the transaction message, and add each transaction field to the data dictionary (if this field does not exist in the data dictionary, the data dictionary may change. You can switch between panels. One problem I face is how to update different windows (or views) when the data dictionary changes.

Two possible scenarios are given below. Panel A is used to maintain the data dictionary. On the one hand, panel B can display the data dictionary and import the transaction information to modify the data dictionary. Panel C can also display data dictionaries.

Scenario 1: After panel A maintains the data dictionary, it notifies the corresponding UI components in panel B and Panel C to perform the refresh process.

 

Scenario 2: After panel B imports transaction information, it modifies the data dictionary. On the one hand, it must notify its own component that displays the data dictionary, in addition, you must notify the corresponding UI components of Panel A and Panel C to update them.

 

Previously, I could think of the following methods:

1. Direct call Method

When the data dictionary of Panel A changes, I take the initiative to call the corresponding components of panel B and Panel C to refresh the processing method. After panel B changes the data dictionary, panel B calls the refresh method of the corresponding components of Panel A and Panel C. However, this method requires you to know which components need to be refreshed, And you can obtain the object method for performing the refresh action. When an application has many UI elements, it is not easy to obtain the target object. And the code looks ugly.

2. Send events

You can consider using the event mechanism to directly pass the event loop of the GUI to the specified window, which is similar to the first one.

I saw the pubsub function in wxpython, which can solve my problem very well.

3. Publish/subscribe mechanism (pubsub)

Pubsub is the publishing and subscription mechanism, and you may have heard of it. First, I have a public publishing channel where anyone can publish information about a topic. In this way, I can develop topics to describe changes in certain States. It can be initiated by the changing party. Then there are some subscribers who will process these changes. They can receive topics they are interested in. When there is information, they can perform their respective processing. Here is just to explain the general principles. Different environments are quite different in implementation. For example, it can be used in a network environment, and here I am interested in how to do it within an application.

Wxpython provides this mechanism. The source code is in Wx. Lib. pubsub. py, which provides instructions for use and complete test cases and code. In fact, the idea is still called directly, but it is encapsulated well and you cannot feel it. To put it simply, it implements the following:

Topic Publishing

You can call publisher () to obtain a singleton object (only one instance object ). Call sendmessage (topic, message = none) of the object to publish the topic. You can also include a message. No.

Information subscription

Call publisher () to obtain the Publisher Object, and then call the subscribe (callable, topic) method of this object to subscribe to a topic. A topic can be a hierarchical structure. In this case, tuple is used to store the topic, for example ('subobject1', 'subject2 '). A subscriber can subscribe to multiple topics at the same time.

In this way, when an event occurs, the sendmessage () topic is called to publish the changes first. Then, the subscriber receives such an event and calls it for corresponding processing.

The advantage is that the publisher does not have to worry about the views to be updated, but it only sends notifications. Subscribers do not have to worry about who released the service. They can process the service as long as there is a call.

Taking a closer look at the implementation of pubsub, we actually use the python feature to import the pubsub module, and the information of this module will become global information. You can use publisher () to obtain the unique global object. Then, each subscription process puts a function, class method, or instance that can be called into this global object. The subscriber is organized into a tree structure based on the topic. (And the node seems to be a weak reference, which saves some resources and doesn't understand it too well .) When sendmessage () is executed, it searches for the published topic in the topic tree of the object, finds the matched topic, retrieves its subscription method, and then executes it one by one. In this case, this pubsub is not an asynchronous call, but a direct call. It just gives you a bit of feeling. It is actually called in sendmessage. Therefore, this method requires that an object method, function, or executable object be provided for execution during subscription. At least one parameter is required to receive messages. Therefore, it requires the number of parameters of the subscriber function.

Why is the topic tree mentioned above? The topic can be divided into two levels, for example: ('subobject',) ('subobject', 'Sport. If both subscriber subscribe, the subscriber will be called twice when the sending topic is ('subobject', 'Sport. Because ('subobject', 'Sport ') also matches ('subobject ',).

Below is a simple piece of code:

Import wx. Lib. pubsub as pubsub # import module
...
Class:
Def _ init __():
Publisher = pubsub. Publisher ()
Publisher. subscribe (self. onsubscribe, 'metadata _ Update ')
Def onsubscribe (self, message ):
Print 'a message'
...

Class B:
Def _ init __():
Publisher = pubsub. Publisher ()
Publisher. subscribe (self. onsubscribe, 'metadata _ Update ')
Def onsubscribe (self, message ):
Print 'B message'
...

Both A and B have subscribed to the same topic.

Publisher = pubsub. Publisher ()
Publisher. sendmessage ('metadata _ Update ')

In this way, a topic is published. In this way, the methods of object A and object B will be called. If there are many objects, they will be called in sequence. However, these operations are all automatic and you cannot feel them.

Pubsub can easily simplify the relationship between programs.

For more details, we recommend that you take a look at the example after the source code, some of which are more complex calls.

If you do not think it is good, you can use the global object to implement such a pubsub function. However, the last note is that there are other pubsub processing methods, especially in network communication, which may be asynchronous, unlike this, it simply calls directly (because it is implemented in a program ). The event loop mechanism can also be used, for example, the publisher maintains the subscriber's handle. When publishing a topic, locate all the subscriber handles and send a message each time. As long as you figure out the truth, there are actually a lot of implementation methods. This is simple and easy to implement. However, it is not a built-in Python module, but provided in wxpython. If you are interested, you can transform it or write a simple one for other projects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.