Application of Python in webgame (I)

Source: Internet
Author: User
Tags virtualenv

Note: This article is based on a video of my report on PyCon China in Shanghai in December 4, 2011. There are many spoken languages, but there is not much nonsense.

(About four and a half minutes ago, I will introduce myself.) Today's keynote speech was actually inspired by two people. One is Professor Hong Qiang Ning Hong, he made an application called "Python on Web 2010" in. You can see that today I use this title for reference directly from him, and I don't even have a new mini-engine. This slide is very good, so I have provided a URL for you to take a look. Then there was Shen Yan, who wrote a slide in 2010, namely "Python programming art". This slide has a great influence on the style of my Python code. He mainly talks about the programming philosophy that should be followed when using Python, that is, how to be more Pythonic. These two slide were the two best slides I met in 2010 at an event in the Pearl River Delta technology salon.

Today, based on my project, I want to introduce the technology and tool for webgame server development. Most of the content is used in past or present projects. A small part of the content will be used in projects later, not when I am doing a promotion. If you choose these technologies, I am not responsible and at your own risk. Therefore, this is because of the project, higher than the project (SHARE), because I will do some ideas and experiments in the project process, then the results of these thoughts and experiments are reflected in this speech.

(Project Introduction) Well, this is what we mentioned just now, "The world is prosperous". It is an online game with a horizontal scroll, you can see that the map is moved from left to right. You can make friends in the main city, interact with netizens, or create a strange copy. Its host is now on the operating platform 0505u.com, and the server is fully developed using Python without a line of C code. It has been developed since July (early ). This is my recent project. Because the theme of the speech will talk about some previous things, I will introduce another project-web board game. It is actually a counterfeit product of QQ games. It is provided on my own website for access, and its server side is completely developed using Python, it took about two or three months for development, and then there were some sporadic maintenance. It is a semi-finished product, not a commercially available product.

Next, we will introduce the related development from the library perspective. First, what do you think of the "location" of Python in the project? Script languages are often used in games, such as LUA, the most commonly used scripting language. Generally, online games use C ++ to write a host, it calls multiple LUA scripts to complete a project (business logic ). This is a common (SCRIPT) form of LUA projects. Python itself is the subject of Python, there are some C/C ++ write extensions to solve some specific problems, and some business logic written in Python, generally, online games in Python are structured like this (see ).

 

For a large Python project, see if it is like this: first, there is an entry main file. py, then there are several business logic files (file1.py, file2.py), and then there are some self-made or company-used libraries (lib1/lib2/lib3) at the same layer ), are all projects in this structure?

 

This is wrong. However, this is the case with several projects I 've experienced, including those I have seen before. Then I think the structure of large and medium-sized Python projects should be as follows:

 

It is an entry file, and then write some business logic (file1.py/file2.py. It may be a bit strange, isn't it smaller? Yes, in fact, I advocate that lib should be placed in site-packages. That is, when we do this, the Library is the library. You must separate it and do not mix it with the business logic. But do you think it is necessary to make it so complicated? I also made money using the method I just made. The project is also launched. In fact, doing so has some advantages, and the most important thing is that it is not complex.

 

In fact, it is just to write a setup. py, that is, to write a setup. py with distutils, and then you can package and install it. Setup. py has two key points. The first is how to avoid handwriting setup. py, and the second is how to create a namespace package. A namespace package has a prefix of abu, followed by the rpc package name. In this way, we can create a database named abu. db, including our own business logic, such as abu. qipai. This is common in zope projects. To build such a structure, I will introduce you to paster. Because it has already been installed with pypi, you can use pip/easy_install to install it. It provides full-stack support for project creation, installation, testing, deployment, and operation.

 

You can take a look at its help. It can be followed by many commands, such as creating a project, running a project, and generating configuration files. Many projects use it to build their own functions, such as pylons, turbogears, and zopeskel. If you want to run the service through paster, as today's daema (ZoomQuiet) said about supervisor, it is actually a thing that I despise. It is not easy to use. You can give it a try, in addition, I also highly recommend start-stop-daemon. In any case, I don't think it is necessary to write the daemon. With pastedeploy, you can run your applications in daemon or other ways. The above is a brief introduction to paster. Next, let's take a look at its basic usage.

To create an application or a library, you must first have a template. You can use the -- list-template parameter of the create sub-command to view available templates in the current environment. For example, if you have installed the basic_package, you can use the-t parameter to specify the template, you can follow the project name to create a project package. Paster will ask some questions, such as the version. You only need to fill in or use the default value. After the inquiry is completed, you will get the setup. py and the corresponding directory structure and related files, you can use it immediately, without handwriting, because setup. there are many parameters for calling the setup function in py, and these parameters also support multiple forms. It is very difficult to understand them clearly (so do not hand-write them if you can do so ). The above describes how to avoid handwriting setup. py. Next we will talk about how to create a namespace package. Create a namespace package. You can first install pbp. skels through pypi. Pbp. skels has many templates to accelerate the creation of applications such as namespace packages, saving valuable time. After installation, you can see an additional pbp_package:

 

Then, you can use the-t parameter to specify the template to create a namespace package with vertices. Namespace can effectively separate your code from others' code. Next is an in-depth topic, I do not expand here, interested friends can read this wiki (http://lucasmanual.com/mywiki/PythonPaste ), it tells you how to compile Your Own Dashboard for paster and extend its commands, because paster can even allow you to add your own commands.

(Go back to setup. py), and use it to support a full range of projects. For example, if you use the develop sub-name during development, you can avoid installing each change. You can also use test for testing, bdist/sdist to pass the deployment package, register to register at pypi server, and upload the release package to pypi server with upload.

With the support of these tools, you can easily separate the code in the form of a library and place it where the library should be, rather than mixing it with the business logic code, this also facilitates code deployment on the production server. You can even build your own cheese shop, namely, pypi server. You can build a company that uses cheese shop to make it easy for your colleagues to use your project. Especially for large companies like Netease in my former East house, sometimes I want to promote something to my colleagues. My colleagues say that it is difficult for me to use your things, for example, it is difficult to isolate data through the Intranet, so we can solve it through self-built cheese shop.

(Another best practice is) every time a software package is developed, there should be a clean and pure environment. Virtualenv helps you build a pure environment. When a project is released, do not use the python environment of the system. Instead, create a virtualenv directory for each project and run it using python in virtualenv. Professor Hong introduced his slides last year. You can refer to them. His slides are very detailed and rigorous, in addition, he introduced things that pythoner should know. I learned a lot from him.

Next, let's talk about plug-ins. First, what are the differences between plug-ins and libraries? In order to better talk about this topic, I had a specific search, the results showed that stackoverflow has a good explanation (http://stackoverflow.com/a/2792342): plug-ins expand the capabilities of large applications; the library is a series of subprograms or classes to help your development. Therefore, libraries and plug-ins have different concepts, so I will introduce them separately today. (For example, in life), an umbrella is a plug-in. When holding an umbrella, people will have the ability to "prevent rain. (Back to software development), we take the card and board project as an example. You can see a variety of games in it:

 

There are landlords, five games, and chess. These can be seen as "functions" of (software ". Next, let's go to the background and check whether it feels a little different:

 

This card and board project is run by an independent process on every table. For example, if I play chess with another person, we will have a separate process to serve us. The process uses desk/main. as the entry point, py can be followed by different parameters. If it is followed by xiangqi, it loads the plug-in of the chess business logic and becomes a chess server. The parameter is doudizhu, then, it loads the landlord plug-in and becomes a landlord server. This provides good scalability for developing business logic programmers, supports permission isolation, and reduces the development technology of business logic through encapsulation, it allows relatively elementary programs to develop business logic, and even outsourcing will not expose too much details. Taking this chess and card project as an example, developers who develop business logic (gameplay) do not need to understand network editing, databases, and multithreading, because it does not need to call the original network, database, and multi-thread interfaces during development, because they are all functions provided by the host, they only implement the interface (or Protocol) defined by the host ). This is also done through setuptools:

 

The doudizhu directory contains setup. py, And the game_impl.py file is very important. In the game_impl.py file, it implements the business logic, that is, the interface defined by the host. Next, let's take a look at setup. in The py file, you can see that the heaviest of the two rows is: You can see that a qipaionweb is defined. in the games section, there is a configuration item named doudizhu. Its value is doudizhu. game_impl: GameImpl. This value is a class and can also be a function.

 

Next, let's talk about how to load the plug-in. You can refer to the following code:

 

As you can see, the get_game_impl_class function accepts a game_name parameter, which is actually a parameter passed in by the command line mentioned above, such as xiangqi and doudizhu. Then, use pkg_resources.load_entry_point to get the content defined by the entry_points parameter in setup. py, Which is class GameImpl here, so it will be available after being instantiated. In host, in addition to defining interfaces, we also provide some business encapsulation and general functions, such as communicating with the upper-level process (that is, the room process). For example, each game has a certain number of people, uniformly manage timer related to business. First, the game is implemented based on the definition of the interface, that is, the implementation of business logic. Business programmers do not have access to the network, databases, and single-threaded programming environments, which greatly reduces the development difficulty. For plug-ins, you can also refer to the trac component architecture. Friends who like OOD should be able to learn a lot from it, and I have learned a lot from it myself, for example, its component manager, component, extension point, and interface declaration. The trac architecture is slightly different from what I just mentioned. I am talking about a very simple version, because card games do not need to be as highly scalable as the trac project.

 

This is the two images in the trac document. there is a component manager in core, which corresponds to many components, and the component has many extension points. Each extension point will implement a certain interface, which is probably like this architecture. Its wiki contains detailed documents that can be accessed online. This plug-in mechanism is not the only one. You can even design and implement it by yourself. For example, the ulipad written by limodou in this morning's speech has implemented a plug-in mechanism. It is a plug-in mechanism implemented with mixin.

Games (servers) are CPU-intensive, I/O-intensive applications ...... (To be continued)

 

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.