Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit 649478b

Browse files
author
Albin Stjerna
committed
Implement async contest manager interface on AsyncElasticsearch
Features: - updated README with the new syntax (first example) - test code in a separate file, because it has to use Python 3.5+ syntax
1 parent 9a603a3 commit 649478b

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

README

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@ Example::
1111

1212
import asyncio
1313
from elasticsearch_async import AsyncElasticsearch
14+
hosts = ['localhost', 'other-host']
1415

15-
client = AsyncElasticsearch(hosts=['localhost', 'other-host'])
16-
17-
@asyncio.coroutine
18-
def print_info():
19-
info = yield from client.info()
20-
print(info)
16+
async def print_info():
17+
async with AsyncElasticsearch(hosts=hosts) as client:
18+
print(await client.info())
2119

2220
loop = asyncio.get_event_loop()
2321
loop.run_until_complete(print_info())
24-
loop.run_until_complete(client.transport.close())
2522
loop.close()
2623

2724

elasticsearch_async/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
from .transport import AsyncTransport
2-
from .connection import AIOHttpConnection
1+
import asyncio
32

43
from elasticsearch import Elasticsearch
54

5+
from .connection import AIOHttpConnection
6+
from .transport import AsyncTransport
7+
8+
69
class AsyncElasticsearch(Elasticsearch):
710
def __init__(self, hosts=None, transport_class=AsyncTransport, **kwargs):
811
super().__init__(hosts, transport_class=transport_class, **kwargs)
12+
13+
@asyncio.coroutine
14+
def __aenter__(self):
15+
return self
16+
17+
@asyncio.coroutine
18+
def __aexit__(self, _exc_type, _exc_val, _exc_tb):
19+
yield from self.transport.close()

test_elasticsearch_async/test_transport.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
from elasticsearch_async import AsyncElasticsearch
66

7+
78
@mark.asyncio
89
def test_sniff_on_start_sniffs(server, event_loop, port, sniff_data):
910
server.register_response('/_nodes/_all/http', sniff_data)
1011

11-
client = AsyncElasticsearch(port=port, sniff_on_start=True, loop=event_loop)
12+
client = AsyncElasticsearch(
13+
port=port, sniff_on_start=True, loop=event_loop)
1214

1315
# sniff has been called in the background
1416
assert client.transport.sniffing_task is not None
@@ -21,10 +23,15 @@ def test_sniff_on_start_sniffs(server, event_loop, port, sniff_data):
2123
assert 'http://node1:9200' == connections[0].host
2224
yield from client.transport.close()
2325

26+
2427
@mark.asyncio
2528
def test_retry_will_work(port, server, event_loop):
26-
client = AsyncElasticsearch(hosts=['not-an-es-host', 'localhost'], port=port, loop=event_loop, randomize_hosts=False)
29+
client = AsyncElasticsearch(
30+
hosts=['not-an-es-host', 'localhost'],
31+
port=port,
32+
loop=event_loop,
33+
randomize_hosts=False)
2734

2835
data = yield from client.info()
29-
assert {'body': '', 'method': 'GET', 'params': {}, 'path': '/'} == data
36+
assert {'body': '', 'method': 'GET', 'params': {}, 'path': '/'} == data
3037
yield from client.transport.close()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import asyncio
2+
import sys
3+
4+
from pytest import mark
5+
6+
from elasticsearch_async import AsyncElasticsearch
7+
8+
9+
@mark.skipif(sys.version_info < (3, 5), reason="async with is Python 3.5+")
10+
@mark.asyncio
11+
async def test_with_notation_works(port, server, event_loop):
12+
async with AsyncElasticsearch(
13+
hosts=['localhost'], port=port, loop=event_loop) as client:
14+
info = await client.info()
15+
assert info

0 commit comments

Comments
 (0)