File: tailoring_atexit_hooks.rst

package info (click to toggle)
pytables 3.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,972 kB
  • ctags: 16,919
  • sloc: python: 59,339; ansic: 46,596; cpp: 1,463; sh: 476; makefile: 428
file content (61 lines) | stat: -rw-r--r-- 2,052 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
:source: http://www.pytables.org/moin/UserDocuments/AtexitHooks
:revision: 2
:date: 2012-06-01 09:31:14
:author: AskJakobsen

========================
Tailoring `atexit` hooks
========================

In some situations you may want to tailor the typical messages that PyTables
outputs::

    Closing remaining open files: /tmp/prova.h5... done

The responsible of this behaviour is the :func:`tables.file.close_open_files`
function that is being registered via :func:`atexit.register` Python function.
Although you can't de-register already registered cleanup functions, you can
register new ones to tailor the existing behaviour.
For example, if you  register this function::

    def my_close_open_files(verbose):
        open_files = tables.file._open_files

        are_open_files = len(open_files) > 0

        if verbose and are_open_files:
            sys.stderr.write("Closing remaining open files:")

        if StrictVersion(tables.__version__) >= StrictVersion("3.1.0"):
            # make a copy of the open_files.handlers container for the iteration
            handlers = list(open_files.handlers)
        else:
            # for older versions of pytables, setup the handlers list from the
            # keys
            keys = open_files.keys()
            handlers = []
            for key in keys:
                handlers.append(open_files[key])

        for fileh in handlers:
            if verbose:
                sys.stderr.write("%s..." % fileh.filename)

            fileh.close()

            if verbose:
                sys.stderr.write("done")

        if verbose and are_open_files:
            sys.stderr.write("\n")

    import sys, atexit
    from distutils.version import StrictVersion
    atexit.register(my_close_open_files, False)

then, you won't get the closing messages anymore because the new registered
function is executed before the existing one.
If you want the messages back again, just set the verbose parameter to true.

You can also use the `atexit` hooks to perform other cleanup functions as well.