clean_threadstore

privex.helpers.plugin.clean_threadstore(thread_id=None, name=None, clean_all: bool = False)bool[source]

Remove the per-thread instance storage in __STORE, usually called when a thread is exiting.

Can also be used to clear a certain key in all thread stores, or completely clear every key from every thread store.

Example:

>>> def some_thread():
...     r = get_redis()
...     print('doing something')
...     print('cleaning up...')
...     clean_threadstore()       # With no arguments, it cleans the thread store for the thread that called it.
>>> t = threading.Thread(target=some_thread)
>>> t.start()
>>> t.join()

Usage outside of a thread:

>>> t = threading.Thread(target=some_thread)
>>> t.start()
>>> thread_id = t.ident                      # Get the thread ID for the started thread
>>> t.join()                                 # Wait for the thread to finish
>>> if thread_id is not None:                # Make sure the thread ID isn't None
...     clean_threadstore(thread_id)         # Cleanup any leftover instances, if there are any.
...

Removing an individual item from thread store:

>>> def some_thread():
...     r = get_redis()
...     print('doing something')
...     print('cleaning up...')
...     clean_threadstore(name='redis')   # Delete only the key 'redis' from the thread store

Removing an individual item from the thread store for ALL thread ID’s:

>>> # Remove the redis instance from every thread store
>>> clean_threadstore(name='redis', clean_all=True)

Clearing the entire thread store for every thread ID:

>>> clean_threadstore(clean_all=True)
Parameters
  • thread_id – The ID of the thread (usually from threading.get_ident()) to clean the storage for. If left as None, will use the ID returned by threading.get_ident().

  • name – If specified, then only the key name will be deleted from the thread store, instead of the entire thread store.

  • clean_all (bool) – (default: False) If True - when name is non-empty - that key will be removed from every thread ID’s thread store - while if name is empty, every single thread ID’s thread store is cleared.