-
Notifications
You must be signed in to change notification settings - Fork 92
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
base: master
Are you sure you want to change the base?
Conversation
Is it expected that people will want to re-use channels? In that case this will cause errors if someone tries to re-use it. In that case we should use an ephemeral |
I think that the whole point in removing Only in tests I know that this is not true and now also in your example with IPython. Your PR LGTM, only |
@@ -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() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
@@ -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(): |
There was a problem hiding this comment.
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
.
Fixes #161