Skip to content

BUG: Pyinstrument causes pickling error #109

@tcdejong

Description

@tcdejong

Description
I'm using pyinstrument to profile a simulation. The simulation consists of a singleton class Simulation that handles ticking and similar state logic, and when it finished it pickles self for inspection later. This is required because many different (fully independent) simulations are run in parallel, and analysis only happens after completion.

Specifically, after the simulation stops it runs these lines:

        # Save pickled version of self to file
        with open(self.out_dir / f'finished_{self.config["name"]}.pickle', 'wb') as file:
                pickle.dump(self, file)

Right now I'm profiling execution of a single Simulation without the multiprocessing. Pyinstrument works fine if I comment out the pickle.dump and replace it with pass. The error only occurs when running the script with pyinstrument, not during regular execution.

Traceback:

Traceback (most recent call last):
  File "c:\users\tcdej\appdata\local\programs\python\python38\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\tcdej\appdata\local\programs\python\python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\tcdej\AppData\Local\Programs\Python\Python38\Scripts\pyinstrument.exe\__main__.py", line 7, in <module>
  File "c:\users\tcdej\appdata\local\programs\python\python38\lib\site-packages\pyinstrument\__main__.py", line 137, in main
    exec_(code, globs, None)
  File "simulation.py", line 540, in <module>
    sim1 = Simulation(config1).run()
  File "simulation.py", line 396, in run
    self.finish()
  File "simulation.py", line 257, in finish
    pickle.dump(self, file)
_pickle.PicklingError: Can't pickle <class '__main__.Simulation'>: attribute lookup Simulation on __main__ failed

I currently don't have time to write a minimum viable product, but will update this issue later.

System details:

  • Pyinstrument v3.2.0
  • Python 3.8.5 (standard CPython)
  • Windows 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions