Mercurial > hg > Members > aka > jupyter_CbC_kernel
comparison jupyter_c_kernel/kernel.py @ 38:cb587f21c8bb
Rename Jupytersubprocess and comment it
author | Brendan Rius <brendan@omixy.com> |
---|---|
date | Sat, 30 Apr 2016 23:05:43 +0100 |
parents | aac47bc07111 |
children | fecdf8733f3b |
comparison
equal
deleted
inserted
replaced
37:aac47bc07111 | 38:cb587f21c8bb |
---|---|
5 import subprocess | 5 import subprocess |
6 import tempfile | 6 import tempfile |
7 import os | 7 import os |
8 import os.path as path | 8 import os.path as path |
9 | 9 |
10 class JupyterSubprocess(subprocess.Popen): | 10 |
11 class RealTimeSubprocess(subprocess.Popen): | |
12 """ | |
13 A subprocess that allows to read its stdout and stderr in real time | |
14 """ | |
15 | |
11 def __init__(self, cmd, write_to_stdout, write_to_stderr): | 16 def __init__(self, cmd, write_to_stdout, write_to_stderr): |
17 """ | |
18 :param cmd: the command to execute | |
19 :param write_to_stdout: a callable that will be called with chunks of data from stdout | |
20 :param write_to_stderr: a callable that will be called with chunks of data from stderr | |
21 """ | |
12 self._write_to_stdout = write_to_stdout | 22 self._write_to_stdout = write_to_stdout |
13 self._write_to_stderr = write_to_stderr | 23 self._write_to_stderr = write_to_stderr |
14 | 24 |
15 super().__init__(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0) | 25 super().__init__(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0) |
16 | 26 |
17 self._stdout_queue = Queue() | 27 self._stdout_queue = Queue() |
18 self._stdout_thread = Thread(target=JupyterSubprocess._enqueue_output, args=(self.stdout, self._stdout_queue)) | 28 self._stdout_thread = Thread(target=RealTimeSubprocess._enqueue_output, args=(self.stdout, self._stdout_queue)) |
19 self._stdout_thread.daemon = True | 29 self._stdout_thread.daemon = True |
20 self._stdout_thread.start() | 30 self._stdout_thread.start() |
21 | 31 |
22 self._stderr_queue = Queue() | 32 self._stderr_queue = Queue() |
23 self._stderr_thread = Thread(target=JupyterSubprocess._enqueue_output, args=(self.stderr, self._stderr_queue)) | 33 self._stderr_thread = Thread(target=RealTimeSubprocess._enqueue_output, args=(self.stderr, self._stderr_queue)) |
24 self._stderr_thread.daemon = True | 34 self._stderr_thread.daemon = True |
25 self._stderr_thread.start() | 35 self._stderr_thread.start() |
26 | 36 |
27 @staticmethod | 37 @staticmethod |
28 def _enqueue_output(contents, queue): | 38 def _enqueue_output(stream, queue): |
29 for line in iter(lambda: contents.read(4096), b''): | 39 """ |
40 Add chunks of data from a stream to a queue until the stream is empty. | |
41 """ | |
42 for line in iter(lambda: stream.read(4096), b''): | |
30 queue.put(line) | 43 queue.put(line) |
31 contents.close() | 44 stream.close() |
32 | 45 |
33 def write_contents(self): | 46 def write_contents(self): |
47 """ | |
48 Write the available content from stdin and stderr where specifid you the instance was created | |
49 :return: | |
50 """ | |
34 try: | 51 try: |
35 stdout_contents = self._stdout_queue.get_nowait() | 52 stdout_contents = self._stdout_queue.get_nowait() |
36 except Empty: | 53 except Empty: |
37 pass | 54 pass |
38 else: | 55 else: |
85 | 102 |
86 def _write_to_stderr(self, contents): | 103 def _write_to_stderr(self, contents): |
87 self.send_response(self.iopub_socket, 'stream', {'name': 'stderr', 'text': contents}) | 104 self.send_response(self.iopub_socket, 'stream', {'name': 'stderr', 'text': contents}) |
88 | 105 |
89 def create_jupyter_subprocess(self, cmd): | 106 def create_jupyter_subprocess(self, cmd): |
90 return JupyterSubprocess(cmd, | 107 return RealTimeSubprocess(cmd, |
91 lambda contents: self._write_to_stdout(contents.decode()), | 108 lambda contents: self._write_to_stdout(contents.decode()), |
92 lambda contents: self._write_to_stderr(contents.decode())) | 109 lambda contents: self._write_to_stderr(contents.decode())) |
93 | 110 |
94 def compile_with_gcc(self, source_filename, binary_filename): | 111 def compile_with_gcc(self, source_filename, binary_filename): |
95 args = ['gcc', source_filename, '-std=c11', '-fPIC', '-shared', '-rdynamic', '-o', binary_filename] | 112 args = ['gcc', source_filename, '-std=c11', '-fPIC', '-shared', '-rdynamic', '-o', binary_filename] |
96 return self.create_jupyter_subprocess(args) | 113 return self.create_jupyter_subprocess(args) |
97 | 114 |