Skip to Content
Python Cookbook
book

Python Cookbook

by Alex Martelli, David Ascher
July 2002
Intermediate to advanced
608 pages
15h 46m
English
O'Reilly Media, Inc.
Content preview from Python Cookbook

Storing Per-Thread Information

Credit: John E. Barham

Problem

You need to allocate storage to each thread for objects that only that thread can use.

Solution

Thread-specific storage is a useful pattern, and Python does not support it directly. A simple dictionary, protected by a lock, makes it pretty easy to program. For once, it’s slightly more general, and not significantly harder, to program to the lower-level thread module, rather than to the more common, higher-level threading module that Python also offers on top of it:

try:
    import thread
except:
    """ We're running on a single-threaded OS (or the Python interpreter has
    not been compiled to support threads), so return a standard dictionary. """
    _tss = {}
    def get_thread_storage(  ):
        return _tss
else:
    """ We do have threads; so, to work: """
    _tss = {}
    _tss_lock = thread.allocate_lock(  )
    def get_thread_storage(  ):
        """ Return a thread-specific storage dictionary. """
        thread_id = thread.get_ident(  ) # Identify the calling thread
        tss = _tss.get(thread_id)
        if tss is None: # First time being called by this thread
            try: # Entering critical section
                _tss_lock.acquire(  )
                _tss[thread_id] = tss = {} # Create thread-specific dictionary
            finally:
                _tss_lock.release(  )
        return tss

Discussion

The get_thread_storage function in this recipe returns a thread-specific storage dictionary. It is a generalization of the get_transaction function from ZODB, the object database underlying Zope. The returned dictionary can be used to store data that is private to ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Modern Python Cookbook - Second Edition

Modern Python Cookbook - Second Edition

Steven F. Lott
Python Cookbook, 3rd Edition

Python Cookbook, 3rd Edition

David Beazley, Brian K. Jones

Publisher Resources

ISBN: 0596001673Supplemental ContentCatalog PageErrata