Code Monkey home page Code Monkey logo

aiogrpc's Introduction

aiogrpc

Build Status PyPI

asyncio wrapper for grpc.io. Currently, only client-side interfaces are supported.

Interfaces

aiogrpc has exactly the same interface as grpc.io (https://grpc.io/grpc/python/grpc.html#module-grpc), except:

  • All callbacks are called with call_soon_threadsafe() to run in the same thread with the event loop
  • All futures are asyncio futures (i.e. awaitable)
  • All blocking interfaces (__call__ for unary methods, code(), details(), ...) become coroutines
  • Stream input/output are async iterators
  • close() method returns a asyncio future (use await channel.close()) (since v1.4)

Usage

  1. Compile your proto file with protoc to generate stub files

  2. Use the stub files with aiogrpc.Channel, instead of grpc.Channel:

    from aiogrpc import insecure_channel
    import asyncio
    from mystub import MyStub
    
    channel = insecure_channel('ipv4:///127.0.0.1:8080')
    mystub = MyStub(channel)
    
    async def test_call():
        return await mystub.mymethod(...)
    
    async def test_call_stream():
        async for v in mystub.my_stream_method(...):
            ...

Balancing

Same as the original grpc.io, balancing (fail over) between multiple servers are supported:

  1. Use multiple IPv4 or IPv6 addresses for balancing - use target like: "ipv4:///1.2.3.4:9999,1.2.3.5:9999,1.2.3.6:9999" or "ipv6:///[1::2]:9999,[1::3]:9999,[1::4]:9999"

  2. Specify ONE DNS name which resolves to multiple addresses. GRPC will balance between the resolved addresses, and try to update the resolving result in interval. Example: "dns:///myserver:9999"

Notice that you cannot mix up IPv4, IPv6 and DNS addresses; only addresses with the same type can be balanced. Also, when using DNS target, only one DNS name can be specified.

The default load balancing strategy is "pick_first" (fail-over). Set "grpc.lb_policy_name" option to "round_robin" for round-robin load balancing: channel = insecure_channel('ipv4:///1.2.3.4:9999,1.2.3.5:9999,1.2.3.6:9999', [('grpc.lb_policy_name', 'round_robin')])

Copyright Notice

All code are published under Apache 2.0 License

Some code and docstrings are modified from grpc.io (https://grpc.io/grpc/python/grpc.html#module-grpc)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.