import ipyparallel as ipp
rc = ipp.Cluster(n=4).start_and_connect_sync()
rc.wait_for_engines(4)
rc.activate()
Using existing profile dir: '/Users/minrk/.ipython/profile_default' Starting 4 engines with <class 'ipyparallel.cluster.launcher.LocalEngineSetLauncher'>
0%| | 0/4 [00:00<?, ?engine/s]
<DirectView all>
%%px --local
import psutil
from threading import Thread
class MemInfo:
"""Simple memory info sampler
Runs in a background thread,
reports highest value
- start() starts samplnig
"""
def __init__(self, interval=0.1):
self._done = False
self._peak = -1
self.interval = interval
self.process = psutil.Process()
self._thread = None
def start(self):
if self._thread:
self.stop()
self._peak = -1
self._done = False
self._thread = Thread(target=self.sample, daemon=True, name="memory sampler")
self._thread.start()
def sample(self):
self._peak = max(self._peak, self.process.memory_info().rss / float(2 ** 20))
def collect_samples(self):
self.sample()
while not self._done:
time.sleep(self.interval)
self.sample()
def stop(self):
self._done = True
if self._thread:
self._thread.join()
self._thread = None
self.sample()
return self._peak
mem_info = MemInfo()
# not really necessary, just here for interactive development,
# unregister previous instance
try:
ip.events.unregister("pre_run_cell", start_sampling)
ip.events.unregister("post_run_cell", stop_sampling)
except NameError:
pass
def start_sampling():
mem_info.start()
def stop_sampling():
rss = mem_info.stop()
if rss > 0:
print(f"Memory usage: {rss:.0f} MB")
ip.events.register("pre_run_cell", start_sampling)
ip.events.register("post_run_cell", stop_sampling)
Memory usage: 72 MB [stdout:0] Memory usage: 68 MB [stdout:1] Memory usage: 68 MB [stdout:2] Memory usage: 68 MB [stdout:3] Memory usage: 68 MB Memory usage: 72 MB
%px pass
[stdout:0] Memory usage: 69 MB
[stdout:1] Memory usage: 68 MB
[stdout:2] Memory usage: 68 MB
[stdout:3] Memory usage: 68 MB
Memory usage: 72 MB
%%px
import numpy as np
a = np.random.random((10, 1024, 1024))
[stdout:1] Memory usage: 148 MB
[stdout:0] Memory usage: 149 MB
[stdout:3] Memory usage: 148 MB
[stdout:2] Memory usage: 148 MB
Memory usage: 72 MB
%px pass
[stdout:1] Memory usage: 148 MB
[stdout:3] Memory usage: 148 MB
[stdout:0] Memory usage: 149 MB
[stdout:2] Memory usage: 149 MB
Memory usage: 72 MB