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

how to create an async client use python #16329

Closed
HaoLiuHust opened this issue Aug 11, 2018 · 2 comments
Closed

how to create an async client use python #16329

HaoLiuHust opened this issue Aug 11, 2018 · 2 comments

Comments

@HaoLiuHust
Copy link

I want to create an async client with python, how could I do it, I can not find any example about it

@mehrdada
Copy link
Member

mehrdada commented Aug 11, 2018

The response-streaming calls are inherently asynchronous in Python, so the routeguide example should demonstrate the overall mechanics of it.

To make a response-unary call asynchronous, instead of making the call like: response = stub.SayHello(...), you can call the future method of the callable:

call_future = stub.SayHello.future(...)
call_future.add_done_callback(function_to_be_called_when_response_is_received)

for example, the adaptation of the helloworld example would be like:

from __future__ import print_function

import grpc
import time

import helloworld_pb2
import helloworld_pb2_grpc


def run():
  def process_response(call_future):
    print(call_future.result())

  channel = grpc.insecure_channel('localhost:50051')
  stub = helloworld_pb2_grpc.GreeterStub(channel)
  call_future = stub.SayHello.future(helloworld_pb2.HelloRequest(name='you'))
  call_future.add_done_callback(process_response)


if __name__ == '__main__':
    run()
    # do not shutdown the main thread to give a chance for async callbacks to 
    # receive their reponses.
    while True:
        time.sleep(100000)

@bhagatyj
Copy link

How does the server code implemented for this example ? Would be helpful if an example of that is posted as well.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 15, 2019
@lock lock bot unassigned mehrdada Jan 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants