X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=examples%2Flivemodel.py;h=7f184c37676525fca33d543102dc26092f95f321;hb=0d70179f2088da28e47bcad569366fb3dddd6e6d;hp=2330a58f24f3dce851da89df5b61701e197651a9;hpb=fe2d2f1a5ef2453359858481929a2526ea1a3c5c;p=osm%2FN2VC.git diff --git a/examples/livemodel.py b/examples/livemodel.py index 2330a58..7f184c3 100644 --- a/examples/livemodel.py +++ b/examples/livemodel.py @@ -1,22 +1,30 @@ -import asyncio +""" +This example: -from juju.model import Model -from juju.client.connection import Connection +1. Connects to the current model +2. Watches the model and prints all changes +3. Runs forever (kill with Ctrl-C) +""" +import asyncio -loop = asyncio.get_event_loop() -conn = loop.run_until_complete(Connection.connect_current()) +from juju.model import Model -def on_model_change(delta, old, new, model): +async def on_model_change(delta, old, new, model): print(delta.entity, delta.type, delta.data) print(old) print(new) print(model) + async def watch_model(): - model = Model(conn) + model = Model() + await model.connect_current() + model.add_observer(on_model_change) - await model.watch() -loop.run_until_complete(watch_model()) + +loop = asyncio.get_event_loop() +loop.create_task(watch_model()) +loop.run_forever()