Menu
Introduction
Getting Started
Developer guide
Release
Architecture
CLI
Technical documentations
Version
Publication date

Mar 9, 2022

Confidentiality
Public
Reactions
1
Share

ThreadDb

Use this class to create a long-running thread that can use the db.

Class that extends threading.Thread to allow to use the db in the thread. It resets the db connection once when the thread starts, not on every operation.

This is useful for background threads that need to continuously access the database, like monitoring or periodic tasks.

Example: def my_continuous_task(): while True: # This function runs in a separate thread with clean db connection results = MyModel.select() # ... do work ... time.sleep(interval)

# Start the thread
thread = ThreadDb(target=my_continuous_task)
thread.daemon = True
thread.start()

:param Thread: threading.Thread :type Thread: type

Functions
__init__

Initialize the ThreadDb

group : Any
reserved for future extension
target : Any
the callable object to be invoked by the run() method
name : str | None
the thread name
daemon : bool | None
whether the thread is a daemon
getName

Return a string used for identification purposes only.

This method is deprecated, use the name attribute instead.

is_alive

Return whether the thread is alive.

This method returns True just before the run() method starts until just after the run() method terminates. See also the module function enumerate().

isDaemon

Return whether this thread is a daemon.

This method is deprecated, use the daemon attribute instead.

join

Wait until the thread terminates.

This blocks the calling thread until the thread whose join() method is called terminates -- either normally or through an unhandled exception or until the optional timeout occurs.

When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). As join() always returns None, you must call is_alive() after join() to decide whether a timeout happened -- if the thread is still alive, the join() call timed out.

When the timeout argument is not present or None, the operation will block until the thread terminates.

A thread can be join()ed many times.

join() raises a RuntimeError if an attempt is made to join the current thread as that would cause a deadlock. It is also an error to join() a thread before it has been started and attempts to do so raises the same exception.

timeout : Any
run

Override the run method to reset db connections once before running the target function

setDaemon

Set whether this thread is a daemon.

This method is deprecated, use the .daemon property instead.

daemonic : Any
setName

Set the name string for this thread.

This method is deprecated, use the name attribute instead.

name : Any
start

Start the thread's activity.

It must be called at most once per thread object. It arranges for the object's run() method to be invoked in a separate thread of control.

This method will raise a RuntimeError if called more than once on the same thread object.

Shine Logo
Technical bricks to reuse or customize

Have you developed a brick?

Share it to accelerate projects for the entire community.