# Buggy code
def run_module(self):
    import runpy

    code = "run_module(modname, run_name='__main__')"
    global_dict = {"run_module": runpy.run_module, "modname": self.options.module}
    sys.argv = [self.options.module] + self.command[:]
    sys.path.append(os.getcwd())
    return self.run_code(code, global_dict)


# Traceback (_pickle.PicklingError)
viztracer --log_multiprocess -m sushy.indexer
date=2020-12-06T12:49:12.350 pid=8953 level=WARNING filename=models.py:129:init_db msg="virtual tables may not be indexed"
date=2020-12-06T12:49:12.360 pid=8953 level=INFO filename=indexer.py:258:<module> msg="Scanning /home/rcarmo/Sites/taoofmac.com/space, static rendering is False"
Loading finish
Loading finish
Saving report to /home/rcarmo/Sync/Development/taoofmac-engine/viztracer_multiprocess_tmp/result_8988.json ...
Saving report to /home/rcarmo/Sync/Development/taoofmac-engine/viztracer_multiprocess_tmp/result_8989.json ...
Dumping trace data to json, total entries: 17, estimated json file size: 2.0KiB
Dumping trace data to json, total entries: 17, estimated json file size: 2.0KiB
Report saved.
Report saved.
Traceback (most recent call last):
File "/home/rcarmo/.pyenv/versions/3.6.9/bin/viztracer", line 8, in <module>
sys.exit(main())
File "/home/rcarmo/.pyenv/versions/3.6.9/lib/python3.6/site-packages/viztracer/main.py", line 378, in main
success, err_msg = ui.run()
File "/home/rcarmo/.pyenv/versions/3.6.9/lib/python3.6/site-packages/viztracer/main.py", line 205, in run
return self.run_module()
File "/home/rcarmo/.pyenv/versions/3.6.9/lib/python3.6/site-packages/viztracer/main.py", line 270, in run_module
return self.run_code(code, global_dict)
File "/home/rcarmo/.pyenv/versions/3.6.9/lib/python3.6/site-packages/viztracer/main.py", line 256, in run_code
exec(code, global_dict)
File "<string>", line 1, in <module>
File "/home/rcarmo/.pyenv/versions/3.6.9/lib/python3.6/runpy.py", line 208, in run_module
return _run_code(code, {}, init_globals, run_name, mod_spec)
File "/home/rcarmo/.pyenv/versions/3.6.9/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/rcarmo/Sync/Development/taoofmac-engine/sushy/indexer.py", line 260, in <module>
pool_indexer(STORE_PATH)
File "/home/rcarmo/Sync/Development/taoofmac-engine/sushy/indexer.py", line 240, in pool_indexer
for res in pool.imap(pool_worker, gen_pages(path), chunksize=10):
File "/home/rcarmo/.pyenv/versions/3.6.9/lib/python3.6/multiprocessing/pool.py", line 320, in <genexpr>
return (item for chunk in result for item in chunk)
File "/home/rcarmo/.pyenv/versions/3.6.9/lib/python3.6/multiprocessing/pool.py", line 735, in next
raise value
File "/home/rcarmo/.pyenv/versions/3.6.9/lib/python3.6/multiprocessing/pool.py", line 424, in _handle_tasks
put(task)
File "/home/rcarmo/.pyenv/versions/3.6.9/lib/python3.6/multiprocessing/connection.py", line 206, in send
self._send_bytes(_ForkingPickler.dumps(obj))
File "/home/rcarmo/.pyenv/versions/3.6.9/lib/python3.6/multiprocessing/reduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function pool_worker at 0x7f39b6d6a488>: attribute lookup pool_worker on __main__ failed
Loading finish
Saving report to /home/rcarmo/Sync/Development/taoofmac-engine/viztracer_multiprocess_tmp/result_8953.json ...
Dumping trace data to json, total entries: 503870, estimated json file size: 57.7MiB
Report saved.
Saving report to /home/rcarmo/Sync/Development/taoofmac-engine/result.html ...
Dumping trace data to json, total entries: 503904, estimated json file size: 57.7MiB
Generating HTML report
Report saved.

# Fixed code
def run_module(self):
    import runpy

    code = "run_module(modname, alter_sys=True)"
    global_dict = {"run_module": runpy.run_module, "modname": self.options.module}
    sys.argv = [self.options.module] + self.command[:]
    sys.path.append(os.getcwd())
    return self.run_code(code, global_dict)