@@ -14,25 +14,23 @@ def __init__(
1414 iothub_connection_string ,
1515 eventhub_connection_string ,
1616 eventhub_consumer_group ,
17- event_loop = None ,
1817 executor = None ,
1918 ):
20- self ._event_loop = event_loop or asyncio .get_event_loop ()
2119 self ._owns_executor = executor is None
2220 self ._executor = executor or concurrent .futures .ThreadPoolExecutor ()
2321 self ._inner_object = ServiceHelperSync (
2422 iothub_connection_string , eventhub_connection_string , eventhub_consumer_group
2523 )
2624
25+ async def _run_in_executor (self , func , * args ):
26+ loop = asyncio .get_running_loop ()
27+ return await loop .run_in_executor (self ._executor , func , * args )
28+
2729 def set_identity (self , device_id , module_id ):
2830 return self ._inner_object .set_identity (device_id , module_id )
2931
3032 async def set_desired_properties (self , desired_props ):
31- return await self ._event_loop .run_in_executor (
32- self ._executor ,
33- self ._inner_object .set_desired_properties ,
34- desired_props ,
35- )
33+ return await self ._run_in_executor (self ._inner_object .set_desired_properties , desired_props )
3634
3735 async def invoke_method (
3836 self ,
@@ -41,8 +39,7 @@ async def invoke_method(
4139 connect_timeout_in_seconds = None ,
4240 response_timeout_in_seconds = None ,
4341 ):
44- return await self ._event_loop .run_in_executor (
45- self ._executor ,
42+ return await self ._run_in_executor (
4643 self ._inner_object .invoke_method ,
4744 method_name ,
4845 payload ,
@@ -55,28 +52,25 @@ async def send_c2d(
5552 payload ,
5653 properties ,
5754 ):
58- return await self ._event_loop .run_in_executor (
59- self ._executor , self ._inner_object .send_c2d , payload , properties
60- )
55+ return await self ._run_in_executor (self ._inner_object .send_c2d , payload , properties )
6156
6257 async def wait_for_eventhub_arrival (self , message_id , timeout = 60 ):
63- return await self ._event_loop .run_in_executor (
64- self ._executor ,
58+ return await self ._run_in_executor (
6559 self ._inner_object .wait_for_eventhub_arrival ,
6660 message_id ,
6761 timeout ,
6862 )
6963
7064 async def get_next_reported_patch_arrival (self , block = True , timeout = 240 ):
71- return await self ._event_loop .run_in_executor (
72- self ._executor ,
65+ return await self ._run_in_executor (
7366 self ._inner_object .get_next_reported_patch_arrival ,
7467 block ,
7568 timeout ,
7669 )
7770
7871 async def shutdown (self ):
79- await self ._event_loop . run_in_executor ( self . _executor , self ._inner_object .shutdown )
72+ await self ._run_in_executor ( self ._inner_object .shutdown )
8073
8174 if self ._owns_executor :
8275 self ._executor .shutdown (wait = True )
76+ return
0 commit comments