2
2
Lowest level connection
3
3
"""
4
4
import asyncio
5
- import functools
6
5
import json
7
6
import logging
8
7
import random
28
27
from aiobotocore .session import get_session as get_async_session
29
28
30
29
30
+ from pynamodb .async_util import wrap_secretly_sync_async_fn
31
31
from pynamodb .constants import (
32
32
RETURN_CONSUMED_CAPACITY_VALUES , RETURN_ITEM_COLL_METRICS_VALUES ,
33
33
RETURN_ITEM_COLL_METRICS , RETURN_CONSUMED_CAPACITY , RETURN_VALUES_VALUES ,
71
71
log .addHandler (logging .NullHandler ())
72
72
73
73
74
- def run_secretly_sync_async_fn (async_fn , * args , ** kwargs ):
75
- # From https://github.com/python-trio/hip/issues/1#issuecomment-322028457
76
- coro = async_fn (* args , ** kwargs )
77
- try :
78
- coro .send (None )
79
- except StopIteration as exc :
80
- return exc .value
81
- else :
82
- raise RuntimeError ("you lied, this async function is not secretly synchronous" )
83
-
84
-
85
74
class MetaTable (object ):
86
75
"""
87
76
A pythonic wrapper around table metadata
@@ -254,20 +243,15 @@ def get_exclusive_start_key_map(self, exclusive_start_key):
254
243
}
255
244
256
245
257
- def sync_wrapper (async_fn ):
258
- @functools .wraps (async_fn )
259
- def wrap (* args , ** kwargs ):
260
- return run_secretly_sync_async_fn (async_fn , * args , ** kwargs )
261
- return wrap
262
-
263
-
264
246
class ConnectionMeta (type ):
265
247
def __init__ (self , name , bases , attrs ):
266
248
super ().__init__ (name , bases , attrs )
267
249
268
250
for attr_name , attr_value in attrs .items ():
269
251
if attr_name .endswith ('_async' ) and asyncio .iscoroutinefunction (attr_value ):
270
- setattr (self , attr_name .rstrip ("_async" ), sync_wrapper (attr_value ))
252
+ wrapped_fn = wrap_secretly_sync_async_fn (attr_value )
253
+ wrapped_fn .__name__ = wrapped_fn .__name__ .rstrip ('_async' )
254
+ setattr (self , wrapped_fn .__name__ , wrapped_fn )
271
255
272
256
273
257
class Connection (metaclass = ConnectionMeta ):
@@ -370,7 +354,7 @@ async def dispatch(self, operation_name, operation_kwargs):
370
354
371
355
self .send_pre_boto_callback (operation_name , req_uuid , table_name )
372
356
373
- data = self ._make_api_call (operation_name , operation_kwargs )
357
+ data = await self ._make_api_call (operation_name , operation_kwargs )
374
358
if asyncio .iscoroutine (data ):
375
359
data = await data
376
360
@@ -395,7 +379,7 @@ def send_pre_boto_callback(self, operation_name, req_uuid, table_name):
395
379
except Exception as e :
396
380
log .exception ("pre_boto callback threw an exception." )
397
381
398
- def _make_api_call (self , operation_name , operation_kwargs ):
382
+ async def _make_api_call (self , operation_name , operation_kwargs ):
399
383
"""
400
384
This private method is here for two reasons:
401
385
1. It's faster to avoid using botocore's response parsing
@@ -422,7 +406,7 @@ def _make_api_call(self, operation_name, operation_kwargs):
422
406
prepared_request .reset_stream ()
423
407
424
408
# Create a new request for each retry (including a new signature).
425
- prepared_request = run_secretly_sync_async_fn ( self ._create_prepared_request , self .client , request_dict , operation_model )
409
+ prepared_request = await self ._create_prepared_request ( self .client , request_dict , operation_model )
426
410
427
411
# Implement the before-send event from botocore
428
412
event_name = 'before-send.dynamodb.{}' .format (operation_model .name )
@@ -694,7 +678,7 @@ async def create_table_async(
694
678
raise TableError ("Failed to create table: {}" .format (e ), e )
695
679
return data
696
680
697
- def update_time_to_live (self , table_name : str , ttl_attribute_name : str ) -> Dict :
681
+ async def update_time_to_live_async (self , table_name : str , ttl_attribute_name : str ) -> Dict :
698
682
"""
699
683
Performs the UpdateTimeToLive operation
700
684
"""
@@ -706,7 +690,7 @@ def update_time_to_live(self, table_name: str, ttl_attribute_name: str) -> Dict:
706
690
}
707
691
}
708
692
try :
709
- return self .dispatch (UPDATE_TIME_TO_LIVE , operation_kwargs )
693
+ return await self .dispatch (UPDATE_TIME_TO_LIVE , operation_kwargs )
710
694
except BOTOCORE_EXCEPTIONS as e :
711
695
raise TableError ("Failed to update TTL on table: {}" .format (e ), e )
712
696
0 commit comments