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:
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.
qi.
Application
¶__init__
(args=None, autoExit=True, url=None, raw=False)¶Parameters: |
|
---|
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.
qi.
ApplicationSession
¶Deprecated since version 2.0.1.
Use qi.Application instead.
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