qi.Application API

Introduction

Application initializes the qi framework, it extract –qi-* commandline arguments and initialise various options accordingly. It also provides facilities to connect the main session. This ease the creation of console applications that connects to a session or want to be standalone.

You can pass the following arguments to your application to control the session:

  • –qi-url : address of the session to connect to
  • –qi-listen-url : address on which the session will listen
  • –qi-standalone : make a standalone session (use –qi-listen-url to set the listen url)

By default the session url is set to “tcp://127.0.0.1:9559” and the listen url to “tcp://0.0.0.0:0”.

If raw is specified there wont be a session embedded into the application and you are free to create and connect a session yourself if needed.

if autoExit is set to True, a session disconnection will quit the program. Set autoExit to False to inhibit this behavior. By default autoExit is True.

Application will parse your arguments and catch –qi-* arguments.

For a normal application you have to call start to let the Session embedded into the application connect. If you want to handle –help yourself you can do that before the application starts avoiding a useless connection to the session.

Reference

class qi.Application
__init__(args=None, autoExit=True, url=None, raw=False)
Parameters:
  • args – the list of arguments (if None sys.argv is used)
  • autoExit – By default the Application quit on session disconnection, set to False to avoid this behavior (default to True)
  • url – The default value to use for qi-url (default to tcp://127.0.0.1:9559)
  • raw – If set to True this does not include a Session into the Application. (advanced user only)

initialise the Application

start()

start the Application. Once start is called, your Application is fully working. The session is connected and available.

stop()

stop the Application. (unblock an application previously launched with run)

run()

block until stop is called. This calls start if it was not already called.

session

return the current session.

url

Url given to the session to connect to.

Deprecated

class qi.ApplicationSession

Deprecated since version 2.0.1.

Use qi.Application instead.

Examples

Simple example that lists all ALMemory keys:

import qi
import sys
from pprint import pprint

if __name__ == "__main__":
    app = qi.Application(sys.argv)

    # start the eventloop
    app.start()

    almemory = app.session.service("ALMemory")

    pprint(almemory.getDataListName())

    #no app.run() needed because we want to exit once getDataListName returns

If you put the content of this script in a listmemory.py file, then you can:

#connect to tcp://127.0.0.1:9559
$ python monscript.py

#connect to tcp://192.168.0.42:9559
$ python monscript.py --qi-url=tcp://192.168.0.42:9559

Simple example that exports a service:

import qi
import sys

class Foo:

    def bar(self):
        print("bar")

if __name__ == "__main__":
    app = qi.Application(sys.argv)

    # start the session
    app.start()

    app.session.registerService("foo", Foo())

    app.run()   # will exit when the connection is over

If you put the content of this script in a foo.py file, then you can:

#connect to tcp://127.0.0.1:9559
$ python foo.py

#connect to tcp://192.168.0.42:9559
$ python foo.py --qi-url=tcp://192.168.0.42:9559

#run a standalone session
$ python foo.py --qi-standalone

#run a standalone session on tcp://0.0.0.0:10000
$ python foo.py --qi-standalone --qi-listen-url="tcp://0.0.0.0:10000"

#let's use qicli to introspect the service
$ qicli info foo --qi-url=tcp://127.0.0.1:10000