Danny Steffy
03/21/2023, 5:21 PMjamie
03/21/2023, 6:12 PMwith duckdb_resource.connect() as con:
# do stuff
# do more stuff outside the with statement
and then after the with
bock is exited, the section of the connect
function after the yield
is executedDanny Steffy
03/21/2023, 6:32 PMconnect
method and that yields my actual database connection, and then after the yield, do my cleanup work? That would probably work for our usecaseDanny Steffy
03/21/2023, 6:43 PM@resource
def sql_server_round_robin(init_context):
def factory(scoring_pipe_label):
@contextmanager
def connect():
cnxn = get_scoring_server(init_context, scoring_pipe_label)
yield cnxn[1]
cleanup_dispatch(cnxn[0])
return factory
Danny Steffy
03/21/2023, 6:47 PMjamie
03/21/2023, 7:16 PMDanny Steffy
03/21/2023, 7:22 PMDanny Steffy
03/23/2023, 3:08 PMjamie
03/23/2023, 3:12 PM@contextmanager
def managed_resource(*args, **kwds):
# Code to acquire resource, e.g.:
resource = acquire_resource(*args, **kwds)
try:
yield resource
finally:
# Code to release resource, e.g.:
release_resource(resource)
some helpful docs https://docs.python.org/3/library/contextlib.html#contextlib.contextmanagerDanny Steffy
03/23/2023, 3:19 PM