linotp.lib.local module

class linotp.lib.local.LocalContainer(source_func, ident_func=<built-in function get_ident>, access_check=<function LocalContainer.<lambda>>)

Bases: object

LocalContainer works as a thread safety wrapper for objects allowing item assignment. On construction it demands a factory function that should produce an object that implements __getitem__, __setitem__ and __contains__

simple example:

LocalContainer(source_func=list) or simply LocalContainer(list)

produces an object that behaves like an empty list on start of every thread. Likewise using dict as source_func produces an object, that behaves like an empty dict, etc.

When you are using custom factory functions keep in mind, that the function itself must be thread safe. References to global variables inside the source_func will compromise thread safety.

custom function example:

>>> def init_fruits ():
>>>    return ['apple', 'banana', 'cherry']
>>> basic_fruits = LocalContainer(source_func=init_fruits)
>>> basic_fruits
['apple', 'banana', 'cherry']

Like all classes in the local module it allows to use a custom thread identity function. This can come in handy when you are using something other than python threads (e.g. greenlets). Just provide a :param ident_func (default is get_ident from the thread module)

Also you can configure an optional access_check method, that signifies if the local object may be accessed at the current time. This is used by context managers in lib.context to make sure local objects are only called inside the right context

linotp.lib.local.release_local(local)

removes the thread local data form the current local object. should be called at the end of every thread