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

Allow Channel objects to be created outside of async contexts #162

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 10 additions & 6 deletions grpclib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ def __init__(

self._host = host
self._port = port
self._loop = loop or asyncio.get_event_loop()
self._loop = loop
self._path = path
self._codec = codec
self._status_details_codec = status_details_codec
Expand Down Expand Up @@ -703,12 +703,13 @@ def _protocol_factory(self) -> H2Protocol:
return H2Protocol(Handler(), self._config, self._h2_config)

async def _create_connection(self) -> H2Protocol:
loop = self._loop or asyncio.get_running_loop()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked your previous version of setting self._loop property here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, so I will do this, but be aware that this will mean that the following code in ipython will fail (and yes, this is absolutely something I want to do when debugging):

>>>	channel = Channel(...)
...
>>>	async with channel as ch:
...		  client = FooBase(ch)
...		  r1 = await client.bar(...)
...
>>>	async with channel as ch:
...		  client = FooBase(ch)
...		  r2 = await client.bar(...)  # this will fail because we still hold the previous loop
...

When you move to 0.5 you'll want to grab the loop explicitly at the callsite (using asyncio.get_running_loop or, even better, just use the asyncio global methods directly). This emulates doing that. By storing the loop, you're keeping a closed loop around which is not intended behaviour for asyncio.

I don't think there are any benefits in doing it the way we did it before except maybe slightly cleaner code.

Can you confirm this is what you want?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange but I can't reproduce your error, even with stock grpclib with no changes.

Python 3.9.13 (main, May 24 2022, 21:28:31)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from grpclib.client import Channel

In [2]: from helloworld.helloworld_pb2 import HelloRequest

In [3]: from helloworld.helloworld_grpc import GreeterStub

In [4]: channel = Channel('127.0.0.1', 50051)

In [5]: async with channel as ch:
   ...:     client = GreeterStub(ch)
   ...:     r1 = await client.SayHello(HelloRequest(name="Dr. Strange"))
   ...:

In [6]: async with channel as ch:
   ...:     client = GreeterStub(ch)
   ...:     r2 = await client.SayHello(HelloRequest(name="Dr. Strange"))
   ...:

In [7]: r1
Out[7]: message: "Hello, Dr. Strange!"

In [8]: r2
Out[8]: message: "Hello, Dr. Strange!"

Which version of IPython do you have?

BTW, as I said before you don't have to use async with to use a channel, like this:

In [4]: channel = Channel('127.0.0.1', 50051)

In [5]: greeter = GreeterStub(channel)

In [6]: await greeter.SayHello(HelloRequest(name='Dr. Strange'))
Out[6]: message: "Hello, Dr. Strange!"

In [7]: await greeter.SayHello(HelloRequest(name='Dr. Strange'))
Out[7]: message: "Hello, Dr. Strange!"

This is fine for IPython sessions or if you want to close the channel manually.

if self._path is not None:
_, protocol = await self._loop.create_unix_connection(
_, protocol = await loop.create_unix_connection(
self._protocol_factory, self._path, ssl=self._ssl,
)
else:
_, protocol = await self._loop.create_connection(
_, protocol = await loop.create_connection(
self._protocol_factory, self._host, self._port,
ssl=self._ssl,
)
Expand Down Expand Up @@ -792,11 +793,14 @@ def __del__(self) -> None:
if self._protocol is not None:
message = 'Unclosed connection: {!r}'.format(self)
warnings.warn(message, ResourceWarning)
if self._loop.is_closed():
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Placing assert self._loop is not None before using self._loop property will do the trick for mypy, this is probably the simplest way of telling to mypy that variable was initialised. If self._protocol is not None then also self._loop is not None.

try:
loop = self._loop or asyncio.get_running_loop()
except RuntimeError:
# There is no loop to handle exception on.
return
else:
if not loop.is_closed():
self.close()
self._loop.call_exception_handler({'message': message})
loop.call_exception_handler({'message': message})

async def __aenter__(self) -> 'Channel':
return self
Expand Down