Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring back asyncio examples #187

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions examples/asyncio/z_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#
# Copyright (c) 2022 ZettaScale Technology
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# ZettaScale Zenoh Team, <zenoh@zettascale.tech>
#

import asyncio
import sys
import time
import argparse
import json
import zenoh
from zenoh import config


async def main():
# --- Command line argument parsing --- --- --- --- --- ---
parser = argparse.ArgumentParser(
prog='z_put',
description='zenoh put example')
parser.add_argument('--mode', '-m', dest='mode',
choices=['peer', 'client'],
type=str,
help='The zenoh session mode.')
parser.add_argument('--connect', '-e', dest='connect',
metavar='ENDPOINT',
action='append',
type=str,
help='Endpoints to connect to.')
parser.add_argument('--listen', '-l', dest='listen',
metavar='ENDPOINT',
action='append',
type=str,
help='Endpoints to listen on.')
parser.add_argument('--key', '-k', dest='key',
default='/demo/example/zenoh-python-put',
type=str,
help='The key expression matching resources to delete.')
parser.add_argument('--config', '-c', dest='config',
metavar='FILE',
type=str,
help='A configuration file.')

args = parser.parse_args()
conf = zenoh.config_from_file(
args.config) if args.config is not None else zenoh.Config()
if args.mode is not None:
conf.insert_json5(zenoh.config.MODE_KEY, json.dumps(args.mode))
if args.connect is not None:
conf.insert_json5(zenoh.config.CONNECT_KEY, json.dumps(args.connect))
if args.listen is not None:
conf.insert_json5(zenoh.config.LISTEN_KEY, json.dumps(args.listen))
key = args.key

# zenoh-net code --- --- --- --- --- --- --- --- --- --- ---

# initiate logging
zenoh.init_logger()

print("Openning session...")
session = await zenoh.async_open(conf)

print("Deleting resources matching '{}'...".format(key))
await session.delete(key)

await session.close()

asyncio.run(main())
94 changes: 94 additions & 0 deletions examples/asyncio/z_get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#
# Copyright (c) 2022 ZettaScale Technology
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# ZettaScale Zenoh Team, <zenoh@zettascale.tech>
#

import asyncio
import sys
import time
import argparse
import json
import zenoh
from zenoh import config, QueryTarget


async def main():
# --- Command line argument parsing --- --- --- --- --- ---
parser = argparse.ArgumentParser(
prog='z_get',
description='zenoh get example')
parser.add_argument('--mode', '-m', dest='mode',
choices=['peer', 'client'],
type=str,
help='The zenoh session mode.')
parser.add_argument('--connect', '-e', dest='connect',
metavar='ENDPOINT',
action='append',
type=str,
help='Endpoints to connect to.')
parser.add_argument('--listen', '-l', dest='listen',
metavar='ENDPOINT',
action='append',
type=str,
help='Endpoints to listen on.')
parser.add_argument('--selector', '-s', dest='selector',
default='/demo/example/**',
type=str,
help='The selection of resources to query.')
parser.add_argument('--target', '-t', dest='target',
choices=['ALL', 'BEST_MATCHING',
'ALL_COMPLETE', 'NONE'],
default='ALL',
type=str,
help='The target queryables of the query.')
parser.add_argument('--config', '-c', dest='config',
metavar='FILE',
type=str,
help='A configuration file.')

args = parser.parse_args()
conf = zenoh.config_from_file(
args.config) if args.config is not None else zenoh.Config()
if args.mode is not None:
conf.insert_json5(zenoh.config.MODE_KEY, json.dumps(args.mode))
if args.connect is not None:
conf.insert_json5(zenoh.config.CONNECT_KEY, json.dumps(args.connect))
if args.listen is not None:
conf.insert_json5(zenoh.config.LISTEN_KEY, json.dumps(args.listen))
selector = args.selector
target = {
'ALL': QueryTarget.All(),
'BEST_MATCHING': QueryTarget.BestMatching(),
'ALL_COMPLETE': QueryTarget.AllComplete(),
'NONE': QueryTarget.No()}.get(args.target)

# zenoh-net code --- --- --- --- --- --- --- --- --- --- ---

# initiate logging
zenoh.init_logger()

print("Openning session...")
session = await zenoh.async_open(conf)

print("Sending Query '{}'...".format(selector))
replies = await session.get(selector, target=target)
for reply in replies:
if isinstance(reply.sample, zenoh.Sample):
print(">> Received ('{}': '{}')"
.format(reply.sample.key_expr, reply.sample.payload.decode("utf-8")))
else:
print(">> Received (ERROR: '{}')"
.format(reply.sample.payload.decode("utf-8")))

await session.close()

asyncio.run(main())
104 changes: 104 additions & 0 deletions examples/asyncio/z_get_parallel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#
# Copyright (c) 2022 ZettaScale Technology
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# ZettaScale Zenoh Team, <zenoh@zettascale.tech>
#

import asyncio
import sys
import time
import argparse
import json
import zenoh
from zenoh import config, QueryTarget


async def main():
# --- Command line argument parsing --- --- --- --- --- ---
parser = argparse.ArgumentParser(
prog='z_get_parallel',
description='zenoh parallel get example')
parser.add_argument('--mode', '-m', dest='mode',
choices=['peer', 'client'],
type=str,
help='The zenoh session mode.')
parser.add_argument('--connect', '-e', dest='connect',
metavar='ENDPOINT',
action='append',
type=str,
help='Endpoints to connect to.')
parser.add_argument('--listen', '-l', dest='listen',
metavar='ENDPOINT',
action='append',
type=str,
help='Endpoints to listen on.')
parser.add_argument('--selector', '-s', dest='selector',
default='/demo/example/**',
type=str,
help='The selection of resources to query.')
parser.add_argument('--target', '-t', dest='target',
choices=['ALL', 'BEST_MATCHING',
'ALL_COMPLETE', 'NONE'],
default='ALL',
type=str,
help='The target queryables of the query.')
parser.add_argument('--config', '-c', dest='config',
metavar='FILE',
type=str,
help='A configuration file.')

args = parser.parse_args()
conf = zenoh.config_from_file(
args.config) if args.config is not None else zenoh.Config()
if args.mode is not None:
conf.insert_json5(zenoh.config.MODE_KEY, json.dumps(args.mode))
if args.connect is not None:
conf.insert_json5(zenoh.config.CONNECT_KEY, json.dumps(args.connect))
if args.listen is not None:
conf.insert_json5(zenoh.config.LISTEN_KEY, json.dumps(args.listen))
selector = args.selector
target = {
'ALL': QueryTarget.All(),
'BEST_MATCHING': QueryTarget.BestMatching(),
'ALL_COMPLETE': QueryTarget.AllComplete(),
'NONE': QueryTarget.No()}.get(args.target)

# zenoh-net code --- --- --- --- --- --- --- --- --- --- ---

# initiate logging
zenoh.init_logger()

print("Openning session...")
session = await zenoh.async_open(conf)

async def do_query(sleep_time):
print("Sending Query '{}?(sleep={})'...".format(selector, sleep_time))
replies = await session.get("{}?(sleep={})".format(selector, sleep_time), target=target)
for reply in replies:
if isinstance(reply.sample, zenoh.Sample):
print(">> Received ('{}': '{}')"
.format(reply.sample.key_expr, reply.sample.payload.decode("utf-8")))
else:
print(">> Received (ERROR: '{}')"
.format(reply.sample.payload.decode("utf-8")))

start = time.time()
await asyncio.gather(
asyncio.create_task(do_query(1)),
asyncio.create_task(do_query(2)),
asyncio.create_task(do_query(3)),
)
end = time.time()
print(f'Time: {end-start:.2f} sec')

await session.close()

asyncio.run(main())
71 changes: 71 additions & 0 deletions examples/asyncio/z_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#
# Copyright (c) 2022 ZettaScale Technology
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# ZettaScale Zenoh Team, <zenoh@zettascale.tech>
#

import asyncio
import sys
import time
import argparse
import json
import zenoh
from zenoh import config


async def main():
# --- Command line argument parsing --- --- --- --- --- ---
parser = argparse.ArgumentParser(
prog='z_info',
description='zenoh info example')
parser.add_argument('--mode', '-m', dest='mode',
choices=['peer', 'client'],
type=str,
help='The zenoh session mode.')
parser.add_argument('--connect', '-e', dest='connect',
metavar='ENDPOINT',
action='append',
type=str,
help='Endpoints to connect to.')
parser.add_argument('--listen', '-l', dest='listen',
metavar='ENDPOINT',
action='append',
type=str,
help='Endpoints to listen on.')
parser.add_argument('--config', '-c', dest='config',
metavar='FILE',
type=str,
help='A configuration file.')

args = parser.parse_args()
conf = zenoh.config_from_file(
args.config) if args.config is not None else zenoh.Config()
if args.mode is not None:
conf.insert_json5(zenoh.config.MODE_KEY, json.dumps(args.mode))
if args.connect is not None:
conf.insert_json5(zenoh.config.CONNECT_KEY, json.dumps(args.connect))
if args.listen is not None:
conf.insert_json5(zenoh.config.LISTEN_KEY, json.dumps(args.listen))
# zenoh-net code --- --- --- --- --- --- --- --- --- --- ---

# initiate logging
zenoh.init_logger()

print("Openning session...")
session = await zenoh.async_open(conf)

info = await session.info()
for key in info:
print("{} : {}".format(key, info[key]))

await session.close()

asyncio.run(main())
Loading