@@ -107,43 +107,24 @@ async def aprocess_notify(self, connected_callback=None):
107107 async for notify in connection .notifies ():
108108 yield notify .channel , notify .payload
109109
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 :
111111 connection = await self .get_connection ()
112112 channel = self .get_publish_channel (channel )
113113
114114 async with connection .cursor () as cur :
115- if not payload :
115+ if not message :
116116 await cur .execute (f'NOTIFY { channel } ;' )
117117 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 } ' )
119121
120122 async def aclose (self ) -> None :
121123 if self ._connection :
122124 await self ._connection .close ()
123125 self ._connection = None
124126
125127
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-
147128class SyncBroker (PGNotifyBase ):
148129 def __init__ (
149130 self ,
@@ -177,11 +158,49 @@ def publish_message(self, channel: Optional[str] = None, message: str = '') -> N
177158 channel = self .get_publish_channel (channel )
178159
179160 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 } ;' )
181165
182- logger .debug (f'Sent pg_notify message to { channel } ' )
166+ logger .debug (f'Sent pg_notify message of { len ( message ) } chars to { channel } ' )
183167
184168 def close (self ) -> None :
185169 if self ._connection :
186170 self ._connection .close ()
187171 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