Getting started

Make sure you have both pip and at least version 3.7 of Python before starting. ASAB uses the new async/await syntax, so earlier versions of python won’t work.

  1. Install ASAB:
$ pip3 install asab
  1. Create a file called main.py with the following code:
#!/usr/bin/env python3
import asab

class MyApplication(asab.Application):
    async def main(self):
        print("Hello world")

if __name__ == '__main__':
    app = MyApplication()
    app.run()
  1. Run the server:
$ python3 main.py
Hello world

You are now successfully runinng an ASAB application server.

  1. Stop the application by Control-C.

Note: The ASAB is designed around a so-called event loop. It is meant primarily for server architectures. For that reason, it doesn’t terminate and continue running and serving eventual requests.

Going into details

#!/usr/bin/env python3

ASAB application uses a Python 3.7+. This is specified a by hashbang line at the very begginig of the file, on the line number 1.

import asab

ASAB is included from as asab module via an import statement.

class MyApplication(asab.Application):

Every ASAB Application needs to have an application object. It is a singleton; it means that the application must create and operate precisely one instance of the application. ASAB provides the base Application class that you need to inherit from to implement your custom application class.

async def main(self):
    print("Hello world")

The Application.main() method is one of the application lifecycle methods, that you can override to implement desired application functionality. The main method is a coroutine, so that you can await any tasks etc. in fully asynchronous way. This method is called when ASAB application is executed and initialized. The lifecycle stage is called “runtime”.

In this example, we just print a message to a screen.

if __name__ == '__main__':
    app = MyApplication()
    app.run()

This part of the code is executed when the Python program is launched. It creates the application object and executes the Application.run() method. This is a standard way of how ASAB application is started.

Next steps

Check out tutorials about how to build ASAB based web server.