Skip to content

tdfs4ds.utils.td_context

tdfs4ds.utils.td_context

teradataml's current context: is one open, close it with a time limit, forget it.

teradataml changed where it keeps the connection. Up to 20.0.0.10 it is a module global, teradataml.context.context.td_connection. From 20.0.0.12 the context is an object, _global_context_instance (top of _context_stack), and the module global stays None. Code that only read the global saw no connection at all on 20.0.0.12 (0.3.6.2's keep-alive never pinged, and its bounded close returned at once) while tdml.remove_context() still ran its garbage collector over the dead connection and blocked forever. Both layouts are read here, once.

tdml.remove_context() sends statements (query band reset, DROP VIEW of its temporary objects) before closing. Over a connection the network dropped while idle, the driver waits for an answer that never comes, so a close must be bounded.

logger module-attribute

logger = logging.getLogger('tdfs4ds')

CLOSE_TIMEOUT module-attribute

CLOSE_TIMEOUT = 30

_SWAPPING module-attribute

_SWAPPING = threading.Event()

_SWAP_LOCK module-attribute

_SWAP_LOCK = threading.Lock()

_ACTIVE_SWAPS module-attribute

_ACTIVE_SWAPS = 0

GENERATION module-attribute

GENERATION = 0

_NO_RETRY module-attribute

_NO_RETRY = threading.local()

POOL_SIZE module-attribute

POOL_SIZE = 8

_SAFE_POOL module-attribute

_SAFE_POOL = None

DEAD_CONTEXT_MARKERS module-attribute

DEAD_CONTEXT_MARKERS = ("'nonetype' object has no attribute 'cursor'", 'is not a valid connection pool handle', 'current teradata vantage connection context is empty', 'tdml_2054')

_SEEN_OPEN module-attribute

_SEEN_OPEN = {'v': False}

_REPAIR_LOCK module-attribute

_REPAIR_LOCK = threading.Lock()

_RETRY_INSTALLED module-attribute

_RETRY_INSTALLED = {'v': False}

context_swap

with context_swap(): around a close + create of the teradataml context.

__enter__

__enter__()

__exit__

__exit__(*exc)

swap_in_progress

swap_in_progress()

wait_for_context

wait_for_context(timeout=60.0)

Wait while a reconnect is swapping the context. True when a context is open.

_bounded

_bounded(fn, timeout, name, no_retry=False)

Run fn in a daemon thread. Returns (finished, result_or_exception).

_ctx_module

_ctx_module()

context_open

context_open()

True when teradataml holds an open connection (either layout).

abandon_context

abandon_context()

Forget the current teradataml connection without sending anything over it.

close_context_bounded

close_context_bounded(timeout=CLOSE_TIMEOUT)

Close the teradataml context, giving up after timeout seconds.

Returns 'none' (nothing open), 'closed', 'failed' (the close raised; the context is cleared anyway) or 'abandoned' (it did not return in time: the context is cleared and the stuck call is left in its daemon thread).

_live_idents

_live_idents()

_protected_records

_protected_records()

The pool records behind teradataml's own connection(s): never closed by a sweep.

_make_safe_pool_class

_make_safe_pool_class()

harden_pool

harden_pool(engine=None)

Make the teradataml engine's pool close only connections of dead threads.

Idempotent and fail-open; returns True when the pool is (now) hardened. Called after every create_context the package makes; harmless on an engine with another pool.

is_dead_context_error

is_dead_context_error(exc)

True when exc says teradataml's connection is gone (the statement did not run).

context_state

context_state()

('open' | 'dead' | 'none', detail) without sending a statement.

dead = teradataml holds a connection object whose driver connection was closed; none = no context at all.

was_open

was_open()

True once this process has had an open context (a dead one is only meaningful then).

probe_context

probe_context(timeout=20.0)

SELECT 1 through teradataml, bounded. (ok, detail); never raises.

repair_context

repair_context(reason, seen_generation=None)

Reconnect teradataml's context once, whoever asks. True when a context is open after.

Threads that fail on the same dead connection all land here: the first reconnects, the others find the generation moved and return at once. Logged at WARNING with reason whatever DISPLAY_LOGS says.

retry_if_dead_context

retry_if_dead_context(fn, *args, **kwargs)

Run fn; when it fails because the context is dead, repair it and run it once more.

install_dead_context_retry

install_dead_context_retry()

Wrap the teradataml entry points every read goes through (once).

create_context.execute_sql (tdml.execute_sql), _global_function_executor (the module-level calls), UtilFuncs._execute_query (DataFrame reads) and _execute_ddl_statement: a call that fails with a dead-context error repairs the context and runs once more. The statement never ran, so a write is never repeated. Used by the admin app; harmless elsewhere.