Skip to content

Commit

Permalink
Add trio based Endpoint implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pipermerriam committed Jun 24, 2019
1 parent b1b6184 commit 37b33c0
Show file tree
Hide file tree
Showing 23 changed files with 1,501 additions and 20 deletions.
30 changes: 29 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,24 @@ jobs:
- image: circleci/python:3.6
environment:
TOXENV: py36-core-asyncio
py36-core-trio:
<<: *common
docker:
- image: circleci/python:3.6
environment:
TOXENV: py36-core-trio
py37-core-asyncio:
<<: *common
docker:
- image: circleci/python:3.7
environment:
TOXENV: py37-core-asyncio
py37-core-trio:
<<: *common
docker:
- image: circleci/python:3.7
environment:
TOXENV: py37-core-trio
py36-examples:
<<: *common
docker:
Expand All @@ -93,12 +105,24 @@ jobs:
- image: circleci/python:3.6
environment:
TOXENV: py36-snappy-core-asyncio
py36-snappy-core-trio:
<<: *common
docker:
- image: circleci/python:3.6
environment:
TOXENV: py36-snappy-core-trio
py37-snappy-core-asyncio:
<<: *common
docker:
- image: circleci/python:3.7
environment:
TOXENV: py37-snappy-core-asyncio
py37-snappy-core-trio:
<<: *common
docker:
- image: circleci/python:3.7
environment:
TOXENV: py37-snappy-core-trio
workflows:
version: 2
test:
Expand All @@ -108,8 +132,12 @@ workflows:
- doctest
- lint
- py36-core-asyncio
- py37-core-asyncio
- py36-core-trio
- py36-examples
- py37-examples
- py37-core-asyncio
- py37-core-trio
- py36-snappy-core-asyncio
- py36-snappy-core-trio
- py37-snappy-core-asyncio
- py37-snappy-core-trio
12 changes: 8 additions & 4 deletions lahja/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,15 @@ def is_stopped(self) -> bool:
async def _process_incoming_messages(self) -> None:
self._running.set()

# Send the hello message
await self.send_message(Hello(self._local_name))
try:
# Send the hello message
await self.send_message(Hello(self._local_name))
# Wait for the other endpoint to identify itself.
hello = await self.conn.read_message()
except RemoteDisconnected:
self._stopped.set()
return

# Wait for the other endpoint to identify itself.
hello = await self.conn.read_message()
if isinstance(hello, Hello):
self._name = hello.name
self.logger.debug(
Expand Down
10 changes: 10 additions & 0 deletions lahja/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ class BindError(LahjaError):
"""


class LifecycleError(LahjaError):
"""
Raised when attempting to violate the lifecycle of an endpoint such as
starting an already started endpoint or starting an endpoint that has
already stopped.
"""

pass


class ConnectionAttemptRejected(LahjaError):
"""
Raised when an attempt was made to connect to an endpoint that is already connected.
Expand Down
21 changes: 21 additions & 0 deletions lahja/tools/benchmark/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from lahja.asyncio import AsyncioEndpoint
from lahja.base import BaseEndpoint
from lahja.trio import TrioEndpoint


class BaseBackend(ABC):
Expand Down Expand Up @@ -40,3 +41,23 @@ async def sleep(seconds: float) -> None:
import asyncio

await asyncio.sleep(seconds)


class TrioBackend(BaseBackend):
name = "trio"
Endpoint = TrioEndpoint

@staticmethod
def run(coro: Any, *args: Any) -> None:
# UNCOMMENT FOR DEBUGGING
# logger = multiprocessing.log_to_stderr()
# logger.setLevel(logging.INFO)
import trio

trio.run(coro, *args)

@staticmethod
async def sleep(seconds: float) -> None:
import trio

await trio.sleep(seconds)
1 change: 1 addition & 0 deletions lahja/trio/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .endpoint import TrioEndpoint # noqa: F401
Loading

0 comments on commit 37b33c0

Please sign in to comment.