MCPcopy Index your code
hub / github.com/cloudpipe/cloudpickle / subprocess_pickle_string

Function subprocess_pickle_string

tests/testutils.py:43–80  ·  view source on GitHub ↗

Retrieve pickle string of an object generated by a child Python process Pickle the input data into a buffer, send it to a subprocess via stdin, expect the subprocess to unpickle, re-pickle that data back and send it back to the parent process via stdout for final unpickling. >>> te

(input_data, protocol=None, timeout=TIMEOUT, add_env=None)

Source from the content-addressed store, hash-verified

41
42
43def subprocess_pickle_string(input_data, protocol=None, timeout=TIMEOUT, add_env=None):
44 """Retrieve pickle string of an object generated by a child Python process
45
46 Pickle the input data into a buffer, send it to a subprocess via
47 stdin, expect the subprocess to unpickle, re-pickle that data back
48 and send it back to the parent process via stdout for final unpickling.
49
50 >>> testutils.subprocess_pickle_string([1, 'a', None], protocol=2)
51 b'\x80\x02]q\x00(K\x01X\x01\x00\x00\x00aq\x01Ne.'
52
53 """
54 # run then pickle_echo(protocol=protocol) in __main__:
55
56 # Protect stderr from any warning, as we will assume an error will happen
57 # if it is not empty. A concrete example is pytest using the imp module,
58 # which is deprecated in python 3.8
59 cmd = [sys.executable, "-W ignore", __file__, "--protocol", str(protocol)]
60 cwd, env = _make_cwd_env()
61 if add_env:
62 env.update(add_env)
63 proc = Popen(
64 cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=cwd, env=env, bufsize=4096
65 )
66 pickle_string = dumps(input_data, protocol=protocol)
67 try:
68 comm_kwargs = {}
69 comm_kwargs["timeout"] = timeout
70 out, err = proc.communicate(pickle_string, **comm_kwargs)
71 if proc.returncode != 0 or len(err):
72 message = "Subprocess returned %d: " % proc.returncode
73 message += err.decode("utf-8")
74 raise RuntimeError(message)
75 return out
76 except TimeoutExpired as e:
77 proc.kill()
78 out, err = proc.communicate()
79 message = "\n".join([out.decode("utf-8"), err.decode("utf-8")])
80 raise RuntimeError(message) from e
81
82
83def subprocess_pickle_echo(input_data, protocol=None, timeout=TIMEOUT, add_env=None):

Calls 3

dumpsFunction · 0.90
_make_cwd_envFunction · 0.85
joinMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…