@@ -107,43 +107,24 @@ async def aprocess_notify(self, connected_callback=None):
107
107
async for notify in connection .notifies ():
108
108
yield notify .channel , notify .payload
109
109
110
- async def apublish_message (self , channel : Optional [str ] = None , payload = None ) -> None :
110
+ async def apublish_message (self , channel : Optional [str ] = None , message : str = '' ) -> None :
111
111
connection = await self .get_connection ()
112
112
channel = self .get_publish_channel (channel )
113
113
114
114
async with connection .cursor () as cur :
115
- if not payload :
115
+ if not message :
116
116
await cur .execute (f'NOTIFY { channel } ;' )
117
117
else :
118
- await cur .execute (f"NOTIFY { channel } , '{ payload } ';" )
118
+ await cur .execute (f"NOTIFY { channel } , '{ message } ';" )
119
+
120
+ logger .debug (f'Sent pg_notify message of { len (message )} chars to { channel } ' )
119
121
120
122
async def aclose (self ) -> None :
121
123
if self ._connection :
122
124
await self ._connection .close ()
123
125
self ._connection = None
124
126
125
127
126
- class ConnectionSaver :
127
- def __init__ (self ):
128
- self ._connection = None
129
-
130
-
131
- connection_save = ConnectionSaver ()
132
-
133
-
134
- def connection_saver (** config ):
135
- """
136
- This mimics the behavior of Django for tests and demos
137
- Philosophically, this is used by an application that uses an ORM,
138
- or otherwise has its own connection management logic.
139
- Dispatcher does not manage connections, so this a simulation of that.
140
- """
141
- if connection_save ._connection is None :
142
- config ['autocommit' ] = True
143
- connection_save ._connection = SyncBroker .create_connection (** config )
144
- return connection_save ._connection
145
-
146
-
147
128
class SyncBroker (PGNotifyBase ):
148
129
def __init__ (
149
130
self ,
@@ -177,11 +158,49 @@ def publish_message(self, channel: Optional[str] = None, message: str = '') -> N
177
158
channel = self .get_publish_channel (channel )
178
159
179
160
with connection .cursor () as cur :
180
- cur .execute ('SELECT pg_notify(%s, %s);' , (channel , message ))
161
+ if message :
162
+ cur .execute ('SELECT pg_notify(%s, %s);' , (channel , message ))
163
+ else :
164
+ cur .execute (f'NOTIFY { channel } ;' )
181
165
182
- logger .debug (f'Sent pg_notify message to { channel } ' )
166
+ logger .debug (f'Sent pg_notify message of { len ( message ) } chars to { channel } ' )
183
167
184
168
def close (self ) -> None :
185
169
if self ._connection :
186
170
self ._connection .close ()
187
171
self ._connection = None
172
+
173
+
174
+ class ConnectionSaver :
175
+ def __init__ (self ) -> None :
176
+ self ._connection : Optional [psycopg .Connection ] = None
177
+ self ._async_connection : Optional [psycopg .AsyncConnection ] = None
178
+
179
+
180
+ connection_save = ConnectionSaver ()
181
+
182
+
183
+ def connection_saver (** config ) -> psycopg .Connection :
184
+ """
185
+ This mimics the behavior of Django for tests and demos
186
+ Philosophically, this is used by an application that uses an ORM,
187
+ or otherwise has its own connection management logic.
188
+ Dispatcher does not manage connections, so this a simulation of that.
189
+ """
190
+ if connection_save ._connection is None :
191
+ config ['autocommit' ] = True
192
+ connection_save ._connection = SyncBroker .create_connection (** config )
193
+ return connection_save ._connection
194
+
195
+
196
+ async def async_connection_saver (** config ) -> psycopg .AsyncConnection :
197
+ """
198
+ This mimics the behavior of Django for tests and demos
199
+ Philosophically, this is used by an application that uses an ORM,
200
+ or otherwise has its own connection management logic.
201
+ Dispatcher does not manage connections, so this a simulation of that.
202
+ """
203
+ if connection_save ._async_connection is None :
204
+ config ['autocommit' ] = True
205
+ connection_save ._async_connection = await AsyncBroker .create_connection (** config )
206
+ return connection_save ._async_connection
0 commit comments