[IPython-dev] Should IPython be a singleton?

Robert Kern robert.kern@gmail....
Wed Aug 26 17:45:32 CDT 2009


On 2009-08-26 17:19 PM, Brian Granger wrote:

>      > 3.  Because of severe cycles in our object graph, an InterativeShell
>      > instances can't be
>      > garbage collected.
>
>     Or rather, they *are* garbage collected and not __del__eted.
>
>
> I have spent some time with gc and muppy/pympler and as of right now, it is
> not even garbage collected.  I create and destroy an InteractiveShell
> instance
> from python (not IPython) and muppy/gc still list it in the live objects.
>
> I haven't had a change to figure out who the offending ref holder is -
> there
> are dozens of them.

When tracking down problems like this, one thing you have to be very careful 
about is not creating references via your instrumentation. It's disturbingly 
easy to do. :-)

__builtins__.__IPYTHON__ and __builtins__.__IPYTHON__active are two culprits, 
and the remaining one is a cell object. I'm not sure where he is coming from.


[~]$ python foo.py
<IPython.core.iplib.InteractiveShell object at 0x3aad90>
<IPython.core.iplib.InteractiveShell object at 0x3aad90>
40 references:
<type 'instancemethod'>
<bound method InteractiveShell.generate_output_prompt of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.shell_hook of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.show_in_pager of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.pre_prompt_hook of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.pre_runcode_hook of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.clipboard_get of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'dict'>
A dict with keys: ['app_name', 'IP', 'crash_report_fname', 
'user_message_template', 'show_crash_traceback', 'contact_email', 'bug_tracker', 
'contact_name']

<type 'instancemethod'>
<bound method InteractiveShell.dummy_handler of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.ipmagic of <IPython.core.iplib.InteractiveShell 
object at 0x3aad90>>

<type 'dict'>
A dict with keys: ['magic', 'IP', 'user_ns', 'user_global_ns', 'system', 'dbg', 
'meta', 'extensions', 'set_crash_handler', 'set_custom_exc', 'set_hook']

<type 'instancemethod'>
<bound method InteractiveShell.set_hook of <IPython.core.iplib.InteractiveShell 
object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.set_custom_exc of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.set_crash_handler of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.set_hook of <IPython.core.iplib.InteractiveShell 
object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.ipmagic of <IPython.core.iplib.InteractiveShell 
object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.ipalias of <IPython.core.iplib.InteractiveShell 
object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.ipsystem of <IPython.core.iplib.InteractiveShell 
object at 0x3aad90>>

<type 'list'>
A list of 19524 elements; probably gc.get_objects().

<type 'cell'>
A cell: <cell at 0x3aab90: InteractiveShell object at 0x3aad90>
<IPython.core.iplib.InteractiveShell object at 0x3aad90>

<type 'dict'>
A dict with keys: ['dir_stack', 'ESC_HELP', 'pycolorize', 'SyntaxTB', 
'indent_current_nsp', 'hooks', 'user_ns', 'user_global_ns', 'system', 
'_main_ns_cache', 'dir_hist', 'autoindent', 'tempfiles', 'BANNER', 
'code_to_run', 'loghead_tpl', 'ESC_PAREN', 'stdin_encoding', 'usage_min', 
'has_readline', 'ns_table', 'getoutput', 'no_alias', 'home_dir', 'filename', 
'ESC_SH_CAP', 'ns_refs_table', 'rc', 'usage', 'starting_dir', 'logger', 
'banner2', 'options_table', 'more', 'shell', 'jobs', 'auto_alias', 'api', 
'buffer', 'ESC_SHELL', 'alias_table', 'InteractiveTB', 'exit_now', 
'builtins_added', 'output_hist', 'internal_ns', 'user_config_ns', 'ESC_QUOTE2', 
'custom_exceptions', 'pager', 'sys_excepthook', 'getoutputerror', 
'esc_handlers', 'input_hist_raw', 'name', 'embedded', 'CACHELENGTH', 
'strdispatchers', 'CustomTB', '_magic_state', 'compile', 'input_hist', 'meta', 
'_user_main_module', 'ESC_MAGIC', 'ESC_QUOTE']

<type 'dict'>
A dict with keys: ['shell', 'name']

<type 'dict'>
A dict with keys: ['shell', 'name']

<type 'dict'>
A dict with keys: ['_dh', '_sh', '__builtins__', 'In', '_ip', '_ih', '__name__', 
'foo', '_oh', 'Out']

<type 'instancemethod'>
<bound method InteractiveShell.handle_auto of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.handle_auto of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.handle_auto of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.handle_magic of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.handle_help of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.handle_shell_escape of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.handle_shell_escape of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.generate_prompt of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'dict'>
A dict with keys: ['log_active', 'shell', '_i', 'log_raw_input', '_i00', '_iii', 
'loghead', 'log_output', 'logfname', '_ii', 'timestamp', '_logmode', 'logfile']

<type 'instancemethod'>
<bound method InteractiveShell.shutdown_hook of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.result_display of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.synchronize_with_editor of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.editor of <IPython.core.iplib.InteractiveShell 
object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.fix_error_editor of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.input_prefilter of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'instancemethod'>
<bound method InteractiveShell.late_startup_hook of 
<IPython.core.iplib.InteractiveShell object at 0x3aad90>>

<type 'dict'>
A dict with keys: ['obj', 'InteractiveShell', '__builtins__', '__file__', 'gc', 
'refs', '__name__', 'ref', '__doc__', 'types']


-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco



More information about the IPython-dev mailing list