|
| 1 | +"""Small example Asynchronous OSC TCP client |
| 2 | +
|
| 3 | +This program listens for incoming messages in one task, and |
| 4 | +sends 10 random values between 0.0 and 1.0 to the /filter address, |
| 5 | +waiting for 1 seconds between each value in a second task. |
| 6 | +""" |
| 7 | + |
| 8 | +import argparse |
| 9 | +import asyncio |
| 10 | +import random |
| 11 | +import sys |
| 12 | + |
| 13 | +from pythonosc import tcp_client |
| 14 | + |
| 15 | + |
| 16 | +async def get_messages(client): |
| 17 | + async for msg in client.get_messages(60): |
| 18 | + print(msg) |
| 19 | + |
| 20 | + |
| 21 | +async def send_messages(client): |
| 22 | + for x in range(10): |
| 23 | + r = random.random() |
| 24 | + print(f"Sending /filter {r}") |
| 25 | + await client.send_message("/filter", r) |
| 26 | + await asyncio.sleep(1) |
| 27 | + |
| 28 | + |
| 29 | +async def init_main(): |
| 30 | + parser = argparse.ArgumentParser() |
| 31 | + parser.add_argument("--ip", default="127.0.0.1", help="The ip of the OSC server") |
| 32 | + parser.add_argument( |
| 33 | + "--port", type=int, default=5005, help="The port the OSC server is listening on" |
| 34 | + ) |
| 35 | + parser.add_argument( |
| 36 | + "--mode", |
| 37 | + default="1.1", |
| 38 | + help="The OSC protocol version of the server (default is 1.1)", |
| 39 | + ) |
| 40 | + args = parser.parse_args() |
| 41 | + |
| 42 | + async with tcp_client.AsyncSimpleTCPClient( |
| 43 | + args.ip, args.port, mode=args.mode |
| 44 | + ) as client: |
| 45 | + async with asyncio.TaskGroup() as tg: |
| 46 | + tg.create_task(get_messages(client)) |
| 47 | + tg.create_task(send_messages(client)) |
| 48 | + |
| 49 | + |
| 50 | +if sys.version_info >= (3, 7): |
| 51 | + asyncio.run(init_main()) |
| 52 | +else: |
| 53 | + # TODO(python-upgrade): drop this once 3.6 is no longer supported |
| 54 | + event_loop = asyncio.get_event_loop() |
| 55 | + event_loop.run_until_complete(init_main()) |
| 56 | + event_loop.close() |
0 commit comments