clean_threadstore

privex.helpers.plugin.clean_threadstore(thread_id=None, name=None)[source]

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

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
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.