I don't really understand what's going on in your code, but you seem
to be calling engine.connect() inside your "do_connect" event handler.
I would expect that to trigger another "do_connect" event, which in
turn will call engine.connect() again, which will trigger another
"do_connect" event, and so on. I'm surprised the application gets as
far as it does. Maybe the exception handler inside receive_do_connect
is allowing it to stumble on.

Simon

On Mon, Mar 21, 2022 at 4:51 AM Srinu Chp <chpsr...@gmail.com> wrote:
>
> Hello Team,
>
> I tried to create a standalone application POC for sqlalchemy to db 
> connection. When I registered do_connect event, I see event is triggered 
> couple of times for one call:
> sqlalchemy_connection.py
>
> import os
> import cx_Oracle
> from sqlalchemy import create_engine
> from sqlalchemy import event
>
> cx_Oracle.init_oracle_client(lib_dir=os.environ.get("HOME") + 
> "/Downloads/instantclient_19_8")
> SQLALCHEMY_CONN = "test"
> count = 0
> engine = None
> def connect_db(pwd):
> global count
> global engine
> print(SQLALCHEMY_CONN)
> username = "ADMIN"
> password = pwd
> dsn = "pydidb_high"
> engine = create_engine(
> f'oracle://{username}:{password}@{dsn}/?encoding=UTF-8&nencoding=UTF-8', 
> max_identifier_length=128)
> setup_event_handlers(engine)
>
> def setup_event_handlers(engine):
> @event.listens_for(engine, 'do_connect')
> def receive_do_connect(dialect, conn_rec, cargs, cparams):
> print("inside do_connect")
> global count
> try:
> with engine.connect() as conn:
> print("inside do_connect try block")
> print(conn.scalar("select sysdate from dual"))
> count += 2
> except Exception as e:
> print("inside do_connect except block")
> count += 1
>
> def db_connect_test():
> print(engine)
> with engine.connect() as conn:
> print(conn.scalar("select sysdate from dual"))
>
> gevent_sync.py
>
> import gevent
> import random
> import sqlalchemy_connection
>
> def task(pid):
> gevent.sleep(random.randint(0,2)*0.001)
> print('Task %s done' % pid)
> sqlalchemy_connection.connect_db(**********)
> sqlalchemy_connection.db_connect_test()
>
> def synchronous():
> for i in range(1,2):
> task(i)
> # sqlalchemy_connection.connect_db(**********)
>
> def asynchronous():
> threads = [gevent.spawn(task, i) for i in range(2)]
> gevent.joinall(threads)
>
> print('Synchronous:')
> synchronous()
> print('count %s ' % sqlalchemy_connection.count)
>
> # print('Asynchronous:')
> # asynchronous()
> # print('count %s' % sqlalchemy_connection.count)
>
> Output:
>
> Synchronous:
> Task 1 done
> test
> Engine(oracle://ADMIN:***@pydidb_high/?encoding=UTF-8&nencoding=UTF-8)
> inside do_connect
> inside do_connect
> inside do_connect
> inside do_connect
> inside do_connect
> inside do_connect
> inside do_connect
> inside do_connect
> inside do_connect
> inside do_connect
> inside do_connect
> inside do_connect
> inside do_connect
> inside do_connect
> inside do_connect
> inside do_connect except block
> inside do_connect try block
> 2022-03-21 04:39:47
> inside do_connect try block
> 2022-03-21 04:39:49
> inside do_connect try block
> 2022-03-21 04:39:51
> inside do_connect try block
> 2022-03-21 04:39:54
> inside do_connect try block
> 2022-03-21 04:39:56
> inside do_connect try block
> 2022-03-21 04:39:59
> inside do_connect try block
> 2022-03-21 04:40:01
> inside do_connect try block
> 2022-03-21 04:40:04
> inside do_connect try block
> 2022-03-21 04:40:09
> inside do_connect try block
> 2022-03-21 04:40:15
> inside do_connect try block
> 2022-03-21 04:40:17
> inside do_connect try block
> 2022-03-21 04:40:19
> inside do_connect try block
> 2022-03-21 04:40:21
> inside do_connect try block
> 2022-03-21 04:40:24
> 2022-03-21 04:40:26
> count 29
>
> highly appreciate any inputs.
> Regards,
> Pydi
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
> ---
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/08096636-06c4-478f-a54d-0bc8f71db414n%40googlegroups.com.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/CAFHwexcS1oydycUH6VXuapQSGVJVEM1oTqR9xF133dbTGODt0w%40mail.gmail.com.

Reply via email to