From 955c167abbdc98dfc20b7e19cdea4e313987e421 Mon Sep 17 00:00:00 2001 From: Eugene Toder Date: Thu, 21 Mar 2024 13:14:46 -0400 Subject: [PATCH] Use asyncio.run() instead of run_until_complete() Mostly in the documentation. --- README.rst | 3 +-- asyncpg/connection.py | 16 ++++++++-------- asyncpg/pgproto | 2 +- docs/api/index.rst | 3 +-- docs/usage.rst | 7 +++---- tools/generate_type_map.py | 5 +---- 6 files changed, 15 insertions(+), 21 deletions(-) diff --git a/README.rst b/README.rst index 0d078d82..f7583a89 100644 --- a/README.rst +++ b/README.rst @@ -88,8 +88,7 @@ Basic Usage ) await conn.close() - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + asyncio.run(run()) License diff --git a/asyncpg/connection.py b/asyncpg/connection.py index e54d6df8..680f42b5 100644 --- a/asyncpg/connection.py +++ b/asyncpg/connection.py @@ -800,7 +800,7 @@ async def copy_from_table(self, table_name, *, output, ... output='file.csv', format='csv') ... print(result) ... - >>> asyncio.get_event_loop().run_until_complete(run()) + >>> asyncio.run(run()) 'COPY 100' .. _`COPY statement documentation`: @@ -869,7 +869,7 @@ async def copy_from_query(self, query, *args, output, ... output='file.csv', format='csv') ... print(result) ... - >>> asyncio.get_event_loop().run_until_complete(run()) + >>> asyncio.run(run()) 'COPY 10' .. _`COPY statement documentation`: @@ -945,7 +945,7 @@ async def copy_to_table(self, table_name, *, source, ... 'mytable', source='datafile.tbl') ... print(result) ... - >>> asyncio.get_event_loop().run_until_complete(run()) + >>> asyncio.run(run()) 'COPY 140000' .. _`COPY statement documentation`: @@ -1027,7 +1027,7 @@ async def copy_records_to_table(self, table_name, *, records, ... (2, 'ham', 'spam')]) ... print(result) ... - >>> asyncio.get_event_loop().run_until_complete(run()) + >>> asyncio.run(run()) 'COPY 2' Asynchronous record iterables are also supported: @@ -1045,7 +1045,7 @@ async def copy_records_to_table(self, table_name, *, records, ... 'mytable', records=record_gen(100)) ... print(result) ... - >>> asyncio.get_event_loop().run_until_complete(run()) + >>> asyncio.run(run()) 'COPY 100' .. versionadded:: 0.11.0 @@ -1305,7 +1305,7 @@ async def set_type_codec(self, typename, *, ... print(result) ... print(datetime.datetime(2002, 1, 1) + result) ... - >>> asyncio.get_event_loop().run_until_complete(run()) + >>> asyncio.run(run()) relativedelta(years=+2, months=+3, days=+1) 2004-04-02 00:00:00 @@ -1772,7 +1772,7 @@ async def reload_schema_state(self): ... await con.execute('LOCK TABLE tbl') ... await change_type(con) ... - >>> asyncio.get_event_loop().run_until_complete(run()) + >>> asyncio.run(run()) .. versionadded:: 0.14.0 """ @@ -2258,7 +2258,7 @@ async def connect(dsn=None, *, ... types = await con.fetch('SELECT * FROM pg_type') ... print(types) ... - >>> asyncio.get_event_loop().run_until_complete(run()) + >>> asyncio.run(run()) [>> import asyncpg, asyncio - >>> loop = asyncio.get_event_loop() >>> async def run(): ... conn = await asyncpg.connect() ... stmt = await conn.prepare('''SELECT 2 ^ $1''') ... print(await stmt.fetchval(10)) ... print(await stmt.fetchval(20)) ... - >>> loop.run_until_complete(run()) + >>> asyncio.run(run()) 1024.0 1048576.0 diff --git a/docs/usage.rst b/docs/usage.rst index 21281b6b..3d36b199 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -44,8 +44,7 @@ which provides methods to run queries and manage transactions. # Close the connection. await conn.close() - asyncio.get_event_loop().run_until_complete(main()) - + asyncio.run(main()) .. note:: @@ -344,7 +343,7 @@ shows how to instruct asyncpg to use floats instead. finally: await conn.close() - asyncio.get_event_loop().run_until_complete(main()) + asyncio.run(main()) Example: decoding hstore values @@ -369,7 +368,7 @@ be registered on a connection using :meth:`Connection.set_builtin_type_codec() result = await conn.fetchval("SELECT 'a=>1,b=>2,c=>NULL'::hstore") assert result == {'a': '1', 'b': '2', 'c': None} - asyncio.get_event_loop().run_until_complete(run()) + asyncio.run(run()) .. _hstore: https://www.postgresql.org/docs/current/static/hstore.html diff --git a/tools/generate_type_map.py b/tools/generate_type_map.py index 8fa816a4..b4e90664 100755 --- a/tools/generate_type_map.py +++ b/tools/generate_type_map.py @@ -124,10 +124,7 @@ def main(): help='PostgreSQL server user') args = parser.parse_args() - - loop = asyncio.get_event_loop() - - loop.run_until_complete(runner(args)) + asyncio.run(runner(args)) if __name__ == '__main__':