Skip to content

Instantly share code, notes, and snippets.

@rgbkrk
Created April 16, 2015 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgbkrk/a22b3c0093c9e7cf4fe1 to your computer and use it in GitHub Desktop.
Save rgbkrk/a22b3c0093c9e7cf4fe1 to your computer and use it in GitHub Desktop.
Using an IPython kernel with cloudpickle for a clean testing namespace
try:
from queue import Empty # Python 3
except ImportError:
from Queue import Empty # Python 2
import cloudpickle
import IPython
def remote_pickledepickle(obj):
km, client = IPython.kernel.manager.start_new_kernel()
client.execute("import pickle")
client.execute("import cloudpickle")
dump_id = client.execute("cloudpickle.dumps(pickle.loads({}))".format(cloudpickle.dumps(obj)))
winner = None
try:
while(True):
message = client.get_iopub_msg(timeout=0.1)
if(message['msg_type'] == 'execute_result' and message['parent_header']['msg_id'] == dump_id):
winner = message['content']['data']['text/plain']
except Empty:
pass
km.shutdown_kernel(now=True)
return eval(winner)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment