Skip to content

Commit 7336b80

Browse files
committed
some PEP8 and code deduplication
1 parent 7a82c76 commit 7336b80

File tree

5 files changed

+29
-23
lines changed

5 files changed

+29
-23
lines changed

gpt_code_ui/kernel_program/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ def get_logger():
1515
logger = logging.getLogger(__name__)
1616
if "DEBUG" in os.environ:
1717
logger.setLevel(logging.DEBUG)
18-
return logger
18+
return logger

gpt_code_ui/kernel_program/kernel_manager.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def cleanup_spawned_processes():
5757
os.kill(pid, signal.CTRL_BREAK_EVENT)
5858
else:
5959
os.kill(pid, signal.SIGKILL)
60-
60+
6161
# After successful kill, cleanup pid file
6262
os.remove(fp)
6363

@@ -217,11 +217,8 @@ def start_kernel(id: str):
217217
cwd=kernel_dir,
218218
env=kernel_env,
219219
)
220-
# Write PID for caller to kill
221-
str_kernel_pid = str(kernel_process.pid)
222-
os.makedirs(config.KERNEL_PID_DIR, exist_ok=True)
223-
with open(os.path.join(config.KERNEL_PID_DIR, str_kernel_pid + ".pid"), "w") as p:
224-
p.write("kernel")
220+
221+
utils.store_pid(kernel_process.pid, "kernel")
225222

226223
# Wait for kernel connection file to be written
227224
while True:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
if __name__ == "__main__":
22
from ipykernel import kernelapp as app
33

4-
app.launch_new_instance()
4+
app.launch_new_instance()

gpt_code_ui/kernel_program/main.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import time
88

99
import asyncio
10-
import json
1110
import threading
1211

1312
from queue import Queue
@@ -60,14 +59,13 @@ def start_kernel_manager():
6059
'workspace', # This will be used as part of the folder name for the workspace and to create the venv inside. Can be anything, but using 'workspace' makes file up-/download very simple
6160
])
6261

63-
# Write PID as <pid>.pid to config.KERNEL_PID_DIR
64-
os.makedirs(config.KERNEL_PID_DIR, exist_ok=True)
65-
with open(os.path.join(config.KERNEL_PID_DIR, "%d.pid" % kernel_manager_process.pid), "w") as p:
66-
p.write("kernel_manager")
62+
utils.store_pid(kernel_manager_process.pid, "kernel_manager")
63+
6764

6865
def cleanup_kernel_program():
6966
kernel_manager.cleanup_spawned_processes()
7067

68+
7169
async def start_snakemq():
7270
global messaging
7371

@@ -80,7 +78,7 @@ def on_recv(conn, ident, message):
8078
if message["value"] == "ready":
8179
logger.debug("Kernel is ready.")
8280
result_queue.put({
83-
"value":"Kernel is ready.",
81+
"value": "Kernel is ready.",
8482
"type": "message"
8583
})
8684

@@ -100,8 +98,9 @@ def send_queued_messages():
10098
while True:
10199
if send_queue.qsize() > 0:
102100
message = send_queue.get()
103-
utils.send_json(messaging,
104-
{"type": "execute", "value": message["command"]},
101+
utils.send_json(
102+
messaging,
103+
{"type": "execute", "value": message["command"]},
105104
config.IDENT_KERNEL_MANAGER
106105
)
107106
time.sleep(0.1)
@@ -120,7 +119,7 @@ async def async_link_loop():
120119

121120
@app.route("/api", methods=["POST", "GET"])
122121
def handle_request():
123-
122+
124123
if request.method == "GET":
125124
# Handle GET requests by sending everything that's in the receive_queue
126125
results = [result_queue.get() for _ in range(result_queue.qsize())]
@@ -131,7 +130,8 @@ def handle_request():
131130
send_queue.put(data)
132131

133132
return jsonify({"result": "success"})
134-
133+
134+
135135
@app.route("/restart", methods=["POST"])
136136
def handle_restart():
137137

@@ -155,9 +155,6 @@ async def main():
155155
def run_flask_app():
156156
app.run(host="0.0.0.0", port=APP_PORT)
157157

158+
158159
if __name__ == "__main__":
159160
asyncio.run(main())
160-
161-
162-
163-

gpt_code_ui/kernel_program/utils.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import re
23
import json
34
import snakemq.link
@@ -7,6 +8,7 @@
78

89
import gpt_code_ui.kernel_program.config as config
910

11+
1012
def escape_ansi(line):
1113
ansi_escape = re.compile(r"(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]")
1214
return ansi_escape.sub("", line)
@@ -16,6 +18,7 @@ def send_json(messaging, message, identity):
1618
message = snakemq.message.Message(json.dumps(message).encode("utf-8"), ttl=600)
1719
messaging.send_message(identity, message)
1820

21+
1922
def init_snakemq(ident, init_type="listen"):
2023
link = snakemq.link.Link()
2124
packeter = snakemq.packeter.Packeter(link)
@@ -26,4 +29,13 @@ def init_snakemq(ident, init_type="listen"):
2629
link.add_connector(("localhost", config.SNAKEMQ_PORT))
2730
else:
2831
raise Exception("Unsupported init type.")
29-
return messaging, link
32+
return messaging, link
33+
34+
35+
def store_pid(pid: int, process_name: str):
36+
'''
37+
Write PID as <pid>.pid to config.KERNEL_PID_DIR
38+
'''
39+
os.makedirs(config.KERNEL_PID_DIR, exist_ok=True)
40+
with open(os.path.join(config.KERNEL_PID_DIR, f"{pid}.pid"), "w") as p:
41+
p.write(process_name)

0 commit comments

Comments
 (0)