|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# async_gather.py (Section 17.1) |
| 3 | +# ----------------------------------------------------------------------------- |
| 4 | + |
| 5 | +# ----------------------------------------------------------------------------- |
| 6 | +# Copyright (c) 2025, Oracle and/or its affiliates. |
| 7 | +# |
| 8 | +# This software is dual-licensed to you under the Universal Permissive License |
| 9 | +# (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License |
| 10 | +# 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose |
| 11 | +# either license. |
| 12 | +# |
| 13 | +# If you elect to accept the software under the Apache License, Version 2.0, |
| 14 | +# the following applies: |
| 15 | +# |
| 16 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 17 | +# you may not use this file except in compliance with the License. |
| 18 | +# You may obtain a copy of the License at |
| 19 | +# |
| 20 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 21 | +# |
| 22 | +# Unless required by applicable law or agreed to in writing, software |
| 23 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 24 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | +# See the License for the specific language governing permissions and |
| 26 | +# limitations under the License. |
| 27 | +# ----------------------------------------------------------------------------- |
| 28 | + |
| 29 | +import asyncio |
| 30 | +import oracledb |
| 31 | +import db_config |
| 32 | + |
| 33 | +# Number of coroutines to run |
| 34 | +CONCURRENCY = 5 |
| 35 | + |
| 36 | +# Maximum connection pool size |
| 37 | +POOLSIZE = 5 |
| 38 | + |
| 39 | +# Query the unique session identifier/serial number combination of a connection |
| 40 | +SQL = """select unique current_timestamp as ct, sid||'-'||serial# as sidser |
| 41 | + from v$session_connect_info |
| 42 | + where sid = sys_context('userenv', 'sid')""" |
| 43 | + |
| 44 | + |
| 45 | +# Show the unique session identifier/serial number of each connection that the |
| 46 | +# pool opens |
| 47 | +async def init_session(connection, requested_tag): |
| 48 | + res = await connection.fetchone(SQL) |
| 49 | + print(res[0].strftime("%H:%M:%S.%f"), "- init_session SID-SERIAL#", res[1]) |
| 50 | + |
| 51 | + |
| 52 | +# The coroutine simply shows the session identifier/serial number of the |
| 53 | +# connection returned by the pool.acquire() call |
| 54 | +async def query(pool): |
| 55 | + async with pool.acquire() as connection: |
| 56 | + await connection.callproc("dbms_session.sleep", [1]) |
| 57 | + res = await connection.fetchone(SQL) |
| 58 | + print(res[0].strftime("%H:%M:%S.%f"), "- query SID-SERIAL#", res[1]) |
| 59 | + |
| 60 | + |
| 61 | +async def main(): |
| 62 | + |
| 63 | + pool = oracledb.create_pool_async( |
| 64 | + user=db_config.user, |
| 65 | + password=db_config.pw, |
| 66 | + dsn=db_config.dsn, |
| 67 | + min=1, |
| 68 | + max=POOLSIZE, |
| 69 | + session_callback=init_session, |
| 70 | + ) |
| 71 | + |
| 72 | + coroutines = [query(pool) for i in range(CONCURRENCY)] |
| 73 | + |
| 74 | + await asyncio.gather(*coroutines) |
| 75 | + |
| 76 | + await pool.close() |
| 77 | + |
| 78 | + |
| 79 | +asyncio.run(main()) |
0 commit comments