Mercurial > hg > Members > aka > jupyter_CbC_kernel
annotate jupyter_c_kernel/kernel.py @ 39:fecdf8733f3b remote/issue-4
Make sure to empty the queue so no message should be left over in it
author | Brendan Rius <brendan@omixy.com> |
---|---|
date | Mon, 16 May 2016 09:43:47 +0100 |
parents | cb587f21c8bb |
children | a98647258349 |
rev | line source |
---|---|
39
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
1 from queue import Queue |
33 | 2 from threading import Thread |
3 | |
0 | 4 from ipykernel.kernelbase import Kernel |
5 import subprocess | |
6 import tempfile | |
2
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
7 import os |
36
8acbab5a9f21
Remove stream buffering on stdout & stderr
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
35
diff
changeset
|
8 import os.path as path |
34 | 9 |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
10 |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
11 class RealTimeSubprocess(subprocess.Popen): |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
12 """ |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
13 A subprocess that allows to read its stdout and stderr in real time |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
14 """ |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
15 |
34 | 16 def __init__(self, cmd, write_to_stdout, write_to_stderr): |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
17 """ |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
18 :param cmd: the command to execute |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
19 :param write_to_stdout: a callable that will be called with chunks of data from stdout |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
20 :param write_to_stderr: a callable that will be called with chunks of data from stderr |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
21 """ |
34 | 22 self._write_to_stdout = write_to_stdout |
23 self._write_to_stderr = write_to_stderr | |
24 | |
37
aac47bc07111
Fix subprocess bufsize & master compilation
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
36
diff
changeset
|
25 super().__init__(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0) |
34 | 26 |
27 self._stdout_queue = Queue() | |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
28 self._stdout_thread = Thread(target=RealTimeSubprocess._enqueue_output, args=(self.stdout, self._stdout_queue)) |
34 | 29 self._stdout_thread.daemon = True |
30 self._stdout_thread.start() | |
31 | |
32 self._stderr_queue = Queue() | |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
33 self._stderr_thread = Thread(target=RealTimeSubprocess._enqueue_output, args=(self.stderr, self._stderr_queue)) |
34 | 34 self._stderr_thread.daemon = True |
35 self._stderr_thread.start() | |
36 | |
37 @staticmethod | |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
38 def _enqueue_output(stream, queue): |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
39 """ |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
40 Add chunks of data from a stream to a queue until the stream is empty. |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
41 """ |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
42 for line in iter(lambda: stream.read(4096), b''): |
34 | 43 queue.put(line) |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
44 stream.close() |
34 | 45 |
35 | 46 def write_contents(self): |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
47 """ |
39
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
48 Write the available content from stdin and stderr where specified when the instance was created |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
49 :return: |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
50 """ |
39
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
51 |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
52 def read_all_from_queue(queue): |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
53 res = b'' |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
54 size = queue.qsize() |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
55 while size != 0: |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
56 res += queue.get_nowait() |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
57 size -= 1 |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
58 return res |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
59 |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
60 stdout_contents = read_all_from_queue(self._stdout_queue) |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
61 if stdout_contents: |
34 | 62 self._write_to_stdout(stdout_contents) |
39
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
63 stderr_contents = read_all_from_queue(self._stderr_queue) |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
64 if stderr_contents: |
34 | 65 self._write_to_stderr(stderr_contents) |
0 | 66 |
67 | |
68 class CKernel(Kernel): | |
20 | 69 implementation = 'jupyter_c_kernel' |
0 | 70 implementation_version = '1.0' |
71 language = 'c' | |
72 language_version = 'C11' | |
2
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
73 language_info = {'name': 'c', |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
74 'mimetype': 'text/plain', |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
75 'file_extension': 'c'} |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
76 banner = "C kernel.\n" \ |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
77 "Uses gcc, compiles in C11, and creates source code files and executables in temporary folder.\n" |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
78 |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
79 def __init__(self, *args, **kwargs): |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
80 super(CKernel, self).__init__(*args, **kwargs) |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
81 self.files = [] |
36
8acbab5a9f21
Remove stream buffering on stdout & stderr
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
35
diff
changeset
|
82 mastertemp = tempfile.mkstemp(suffix='.out') |
8acbab5a9f21
Remove stream buffering on stdout & stderr
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
35
diff
changeset
|
83 os.close(mastertemp[0]) |
8acbab5a9f21
Remove stream buffering on stdout & stderr
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
35
diff
changeset
|
84 self.master_path = mastertemp[1] |
8acbab5a9f21
Remove stream buffering on stdout & stderr
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
35
diff
changeset
|
85 filepath = path.join(path.dirname(path.realpath(__file__)), '..', 'resources', 'master.c') |
37
aac47bc07111
Fix subprocess bufsize & master compilation
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
36
diff
changeset
|
86 subprocess.call(['gcc', filepath, '-std=c11', '-rdynamic', '-ldl', '-o', self.master_path]) |
2
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
87 |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
88 def cleanup_files(self): |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
89 """Remove all the temporary files created by the kernel""" |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
90 for file in self.files: |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
91 os.remove(file) |
36
8acbab5a9f21
Remove stream buffering on stdout & stderr
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
35
diff
changeset
|
92 os.remove(self.master_path) |
0 | 93 |
2
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
94 def new_temp_file(self, **kwargs): |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
95 """Create a new temp file to be deleted when the kernel shuts down""" |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
96 # We don't want the file to be deleted when closed, but only when the kernel stops |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
97 kwargs['delete'] = False |
9 | 98 kwargs['mode'] = 'w' |
2
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
99 file = tempfile.NamedTemporaryFile(**kwargs) |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
100 self.files.append(file.name) |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
101 return file |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
102 |
34 | 103 def _write_to_stdout(self, contents): |
104 self.send_response(self.iopub_socket, 'stream', {'name': 'stdout', 'text': contents}) | |
33 | 105 |
34 | 106 def _write_to_stderr(self, contents): |
107 self.send_response(self.iopub_socket, 'stream', {'name': 'stderr', 'text': contents}) | |
33 | 108 |
34 | 109 def create_jupyter_subprocess(self, cmd): |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
110 return RealTimeSubprocess(cmd, |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
111 lambda contents: self._write_to_stdout(contents.decode()), |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
112 lambda contents: self._write_to_stderr(contents.decode())) |
2
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
113 |
34 | 114 def compile_with_gcc(self, source_filename, binary_filename): |
36
8acbab5a9f21
Remove stream buffering on stdout & stderr
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
35
diff
changeset
|
115 args = ['gcc', source_filename, '-std=c11', '-fPIC', '-shared', '-rdynamic', '-o', binary_filename] |
34 | 116 return self.create_jupyter_subprocess(args) |
0 | 117 |
118 def do_execute(self, code, silent, store_history=True, | |
119 user_expressions=None, allow_stdin=False): | |
2
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
120 with self.new_temp_file(suffix='.c') as source_file: |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
121 source_file.write(code) |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
122 source_file.flush() |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
123 with self.new_temp_file(suffix='.out') as binary_file: |
34 | 124 p = self.compile_with_gcc(source_file.name, binary_file.name) |
33 | 125 while p.poll() is None: |
35 | 126 p.write_contents() |
127 p.write_contents() | |
33 | 128 if p.returncode != 0: # Compilation failed |
34 | 129 self._write_to_stderr( |
130 "[C kernel] GCC exited with code {}, the executable will not be executed".format( | |
131 p.returncode)) | |
33 | 132 return {'status': 'ok', 'execution_count': self.execution_count, 'payload': [], |
133 'user_expressions': {}} | |
0 | 134 |
36
8acbab5a9f21
Remove stream buffering on stdout & stderr
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
35
diff
changeset
|
135 p = self.create_jupyter_subprocess([self.master_path, binary_file.name]) |
33 | 136 while p.poll() is None: |
35 | 137 p.write_contents() |
138 p.write_contents() | |
33 | 139 |
140 if p.returncode != 0: | |
34 | 141 self._write_to_stderr("[C kernel] Executable exited with code {}".format(p.returncode)) |
2
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
142 return {'status': 'ok', 'execution_count': self.execution_count, 'payload': [], 'user_expressions': {}} |
7
aa54c85303b6
Remove temporary files when shutting down the kernel
Brendan Rius <brendan@omixy.com>
parents:
4
diff
changeset
|
143 |
aa54c85303b6
Remove temporary files when shutting down the kernel
Brendan Rius <brendan@omixy.com>
parents:
4
diff
changeset
|
144 def do_shutdown(self, restart): |
aa54c85303b6
Remove temporary files when shutting down the kernel
Brendan Rius <brendan@omixy.com>
parents:
4
diff
changeset
|
145 """Cleanup the created source code files and executables when shutting down the kernel""" |
aa54c85303b6
Remove temporary files when shutting down the kernel
Brendan Rius <brendan@omixy.com>
parents:
4
diff
changeset
|
146 self.cleanup_files() |