Skip to content

Commit

Permalink
docs: stream requests async example
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed Aug 22, 2019
1 parent 0f935cd commit 0ea1090
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
8 changes: 7 additions & 1 deletion docs/asynchronous.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ the same example of using the synchronization method can be found `here <payment

.. literalinclude:: ../examples/payment_async.py
:language: python
:linenos:
:linenos:

The following example helps you listen to multiple endpoints asynchronously.

.. literalinclude:: ../examples/stream_requests_async.py
:language: python
:linenos:
42 changes: 42 additions & 0 deletions examples/stream_requests_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import asyncio

from stellar_sdk import AiohttpClient, Server

HORIZON_URL = "https://horizon.stellar.org"


async def payments():
async with Server(HORIZON_URL, AiohttpClient()) as server:
async for payment in server.payments().cursor(cursor="now").stream():
print(f"Payment: {payment}")


async def effects():
async with Server(HORIZON_URL, AiohttpClient()) as server:
async for effect in server.effects().cursor(cursor="now").stream():
print(f"Effect: {effect}")


async def operations():
async with Server(HORIZON_URL, AiohttpClient()) as server:
async for operation in server.operations().cursor(cursor="now").stream():
print(f"Operation: {operation}")


async def transactions():
async with Server(HORIZON_URL, AiohttpClient()) as server:
async for transaction in server.transactions().cursor(cursor="now").stream():
print(f"Transaction: {transaction}")


async def listen():
await asyncio.gather(
payments(),
effects(),
operations(),
transactions()
)


if __name__ == '__main__':
asyncio.run(listen())

0 comments on commit 0ea1090

Please sign in to comment.