Skip to content

Experimental async support (asyncio/trio/curio) #61

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

Open
wants to merge 8 commits into
base: master
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
5 changes: 4 additions & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
name: base
strategy:
matrix:
python: [ '2.7', '3.5', '3.6', '3.6', '3.7']
python: [ '2.7', '3.6', '3.6', '3.7', '3.8', '3.9']
# os: ['ubuntu-latest', 'windows-latest', 'macOs-latest']
os: ['ubuntu-latest', 'windows-latest']

Expand All @@ -20,6 +20,9 @@ jobs:
run: pip install -U setuptools wheel
- name: install
run: pip install .[dev,ci]
- name: install async requirements
if: matrix.python != '2.7'
run: pip install trio curio
- name: test
run: python -m pytest --reruns 5 tests/ --cov oscpy/ --cov-branch
- name: coveralls
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
*.c
*.pyd
*.egg-info
*.swp
*.swn
.coverage
htmlcov/
.pytest_cache
build
dist
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,29 @@ with OSCAsyncServer(port=8000) as OSC:
print("unknown address {}".format(address))
```

Server (curio)

```python
async def osc_app(address, port):
osc = OSCCurioServer(encoding='utf8')
osc.listen(address=address, port=port, default=True)

@osc.address("/example")
async def example(*values):
print(f"got {values} on /example")
await curio.sleep(4)
print("done sleeping")

@osc.address("/stop")
async def stop(*values):
print(f"time to leave!")
await osc.stop()

await osc.process()

curio.run(osc_app, '0.0.0.0', 8000)
```

Client

```python
Expand Down
33 changes: 33 additions & 0 deletions examples/asyncio_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import asyncio

from oscpy.server.asyncio_server import OSCAsyncioServer


async def osc_app(address, port):
osc = OSCAsyncioServer(encoding='utf8')
osc.listen(address=address, port=port, default=True)
sock2 = osc.listen(address=address, port=port + 1)

@osc.address("/example")
async def example(*values):
print(f"got {values} on /example")
await asyncio.sleep(4)
print("done sleeping")

@osc.address("/test")
async def test(*values):
print(f"got {values} on /test")
await asyncio.sleep(4)
print("done sleeping")

@osc.address("/stop", sock=sock2)
async def stop(*values):
print(f"time to leave!")
osc.terminate_server()

print(sock2.getsockname())
asyncio.get_event_loop().create_task(osc.process())
await osc.join_server()


asyncio.run(osc_app('localhost', 8000))
29 changes: 29 additions & 0 deletions examples/curio_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import curio

from oscpy.server.curio_server import OSCCurioServer


async def osc_app(address, port):
osc = OSCCurioServer(encoding='utf8')
osc.listen(address=address, port=port, default=True)

@osc.address("/example")
async def example(*values):
print(f"got {values} on /example")
await curio.sleep(4)
print("done sleeping")

@osc.address("/test")
async def test(*values):
print(f"got {values} on /test")
await curio.sleep(4)
print("done sleeping")

@osc.address("/stop")
async def stop(*values):
print(f"time to leave!")
await osc.stop_all()

await osc.process()

curio.run(osc_app, '0.0.0.0', 8000)
22 changes: 22 additions & 0 deletions examples/thread_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import sys
from time import sleep

from oscpy.server import OSCThreadServer

osc = OSCThreadServer(encoding='utf8')
sock = osc.listen(address='0.0.0.0', port=8000, default=True)

@osc.address('/address')
def callback(*values):
print("got values: {}".format(values))


@osc.address('/stop')
def callback(*values):
print("time to leave")
osc.stop_all()
osc.terminate_server()


# wait until the server exits
osc.join_server()
34 changes: 34 additions & 0 deletions examples/trio_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import trio

from oscpy.server.trio_server import OSCTrioServer


async def osc_app(address, port):
osc = OSCTrioServer(encoding='utf8')
await osc.listen(address=address, port=port, default=True)

@osc.address("/example")
async def example(*values):
print(f"got {values} on /example")
await trio.sleep(4)
print("done sleeping")

@osc.address("/test")
async def test(*values):
print(f"got {values} on /test")
await trio.sleep(4)
print("done sleeping")

@osc.address("/stop")
async def stop(*values):
print(f"time to leave!")
await osc.stop_all()

@osc.address("/info")
async def info():
address, port = osc.getaddress()
print(address, port)

await osc.process()

trio.run(osc_app, '0.0.0.0', 8000)
Loading