1
1
from __future__ import annotations
2
2
3
3
import asyncio
4
+ import typing
4
5
from concurrent .futures import Future
5
6
from typing import Union , List , Optional , Coroutine
6
7
10
11
TopicWriterError ,
11
12
PublicWriterInitInfo ,
12
13
PublicWriteResult ,
13
- Message ,
14
+ Message , TopicWriterClosedError ,
14
15
)
15
16
16
17
from .topic_writer_asyncio import WriterAsyncIO
17
- from .._topic_common .common import _get_shared_event_loop , TimeoutType
18
+ from .._topic_common .common import _get_shared_event_loop , TimeoutType , CallFromSyncToAsync
18
19
19
20
20
21
class WriterSync :
21
- _loop : asyncio . AbstractEventLoop
22
+ _caller : CallFromSyncToAsync
22
23
_async_writer : WriterAsyncIO
23
24
_closed : bool
24
25
@@ -33,80 +34,79 @@ def __init__(
33
34
self ._closed = False
34
35
35
36
if eventloop :
36
- self . _loop = eventloop
37
+ loop = eventloop
37
38
else :
38
- self ._loop = _get_shared_event_loop ()
39
+ loop = _get_shared_event_loop ()
40
+
41
+ self ._caller = CallFromSyncToAsync (loop )
39
42
40
43
async def create_async_writer ():
41
44
return WriterAsyncIO (driver , settings )
42
45
43
- self ._async_writer = asyncio .run_coroutine_threadsafe (
44
- create_async_writer (), self ._loop
45
- ).result ()
46
+ self ._async_writer = self ._caller .safe_call_with_result (create_async_writer (), None )
46
47
47
48
def __enter__ (self ):
48
49
return self
49
50
50
51
def __exit__ (self , exc_type , exc_val , exc_tb ):
51
52
self .close ()
52
53
53
- def _call (self , coro ):
54
+ def close (self , * , flush : bool = True , timeout : typing . Union [ int , float , None ] = None ):
54
55
if self ._closed :
55
- raise TopicWriterError ( "writer is closed" )
56
+ return
56
57
57
- return asyncio . run_coroutine_threadsafe ( coro , self ._loop )
58
+ self ._closed = True
58
59
59
- def _call_sync (self , coro : Coroutine , timeout ):
60
- f = self ._call (coro )
61
- try :
62
- return f .result (timeout )
63
- except TimeoutError :
64
- f .cancel ()
65
- raise
60
+ self ._caller .safe_call_with_result (self ._async_writer .close (flush = flush ), timeout )
66
61
67
- def close (self , flush : bool = True ):
62
+ def _check_closed (self ):
68
63
if self ._closed :
69
- return
64
+ raise TopicWriterClosedError ()
70
65
71
- self ._closed = True
66
+ def async_flush (self ) -> Future :
67
+ self ._check_closed ()
72
68
73
- # for no call self._call_sync on closed object
74
- asyncio .run_coroutine_threadsafe (
75
- self ._async_writer .close (flush = flush ), self ._loop
76
- ).result ()
69
+ return self ._caller .unsafe_call_with_future (self ._async_writer .flush ())
77
70
78
- def async_flush (self ) -> Future :
79
- if self ._closed :
80
- raise TopicWriterError ("writer is closed" )
81
- return self ._call (self ._async_writer .flush ())
71
+ def flush (self , * , timeout = None ):
72
+ self ._check_closed ()
82
73
83
- def flush (self , timeout = None ):
84
- self ._call_sync (self ._async_writer .flush (), timeout )
74
+ return self ._caller .unsafe_call_with_result (self ._async_writer .flush (), timeout )
85
75
86
76
def async_wait_init (self ) -> Future [PublicWriterInitInfo ]:
87
- return self ._call (self ._async_writer .wait_init ())
77
+ self ._check_closed ()
78
+
79
+ return self ._caller .unsafe_call_with_future (self ._async_writer .wait_init ())
88
80
89
- def wait_init (self , timeout : Optional [TimeoutType ] = None ) -> PublicWriterInitInfo :
90
- return self ._call_sync (self ._async_writer .wait_init (), timeout )
81
+ def wait_init (self , * , timeout : TimeoutType = None ) -> PublicWriterInitInfo :
82
+ self ._check_closed ()
83
+
84
+ return self ._caller .unsafe_call_with_result (self ._async_writer .wait_init (), timeout )
91
85
92
86
def write (
93
87
self ,
94
88
messages : Union [Message , List [Message ]],
95
- timeout : Union [ float , None ] = None ,
89
+ timeout : TimeoutType = None ,
96
90
):
97
- self ._call_sync (self ._async_writer .write (messages ), timeout = timeout )
91
+ self ._check_closed ()
92
+
93
+ self ._caller .safe_call_with_result (self ._async_writer .write (messages ), timeout )
98
94
99
95
def async_write_with_ack (
100
96
self ,
101
97
messages : Union [Message , List [Message ]],
102
98
) -> Future [Union [PublicWriteResult , List [PublicWriteResult ]]]:
103
- return self ._call (self ._async_writer .write_with_ack (messages ))
99
+ self ._check_closed ()
100
+
101
+ return self ._caller .unsafe_call_with_future (self ._async_writer .write_with_ack (messages ))
104
102
105
103
def write_with_ack (
106
104
self ,
107
105
messages : Union [Message , List [Message ]],
108
106
timeout : Union [float , None ] = None ,
109
107
) -> Union [PublicWriteResult , List [PublicWriteResult ]]:
110
- return self ._call_sync (
108
+ self ._check_closed ()
109
+
110
+ return self ._caller .unsafe_call_with_result (
111
111
self ._async_writer .write_with_ack (messages ), timeout = timeout
112
112
)
0 commit comments