Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit16d0b89

Browse files
authored
Merge pull request#117 from Dyalog/78-quit-interpreter
Fix for issue 78
2 parents21bc813 +58afbe4 commit16d0b89

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

‎dyalog_kernel/kernel.py‎

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
importsubprocess
88
importre
99
importhtml
10+
importsignal
1011

1112
fromcollectionsimportdeque
1213

@@ -136,6 +137,34 @@ def out_stream(self, s):
136137
}
137138
self.send_response(self.iopub_socket,'stream',_content)
138139

140+
defcleanup_dyalog(self):
141+
try:
142+
ifDYALOG_HOST=='127.0.0.1':
143+
ifself.connectedandhasattr(self,'dyalogTCP'):
144+
self.ride_send(["Exit", {"code":0}])
145+
ifself.dyalog_subprocess:
146+
# Graceful termination
147+
self.dyalog_subprocess.terminate()
148+
try:
149+
self.dyalog_subprocess.wait(timeout=3)
150+
exceptsubprocess.TimeoutExpired:
151+
# Force kill if graceful termination fails
152+
self.dyalog_subprocess.kill()
153+
self.dyalog_subprocess.wait()
154+
155+
ifhasattr(self,'dyalogTCP'):
156+
self.dyalogTCP.close()
157+
158+
self.connected=False
159+
160+
exceptExceptionase:
161+
debug(f"Error in cleanup_dyalog:{e}")
162+
163+
defsignal_handler(self,signum,frame):
164+
debug(f"Received signal{signum}, cleaning up...")
165+
self.cleanup_dyalog()
166+
sys.exit(0)
167+
139168
defdyalog_ride_connect(self):
140169

141170
timeout=time.time()+RIDE_INIT_CONNECT_TIME_OUT
@@ -196,7 +225,11 @@ def __init__(self, **kwargs):
196225
#from ipykernel import get_connection_file
197226
#s = get_connection_file()
198227
# debug("########## " + str(s))
199-
228+
229+
# Register signal handlers and cleanup
230+
signal.signal(signal.SIGTERM,self.signal_handler)
231+
signal.signal(signal.SIGINT,self.signal_handler)
232+
200233
self._port=DYALOG_PORT
201234
# lets find an available port
202235
# this makes sense only if Dyalog APL and Jupyter executables are on the same host (localhost)
@@ -229,15 +262,15 @@ def __init__(self, **kwargs):
229262
CloseKey(dyalogKey)
230263
CloseKey(lastKey)
231264
self.dyalog_subprocess=subprocess.Popen([dyalogPath,"RIDE_SPAWNED=1","DYALOGQUIETUCMDBUILD=1","Dyalog_LineEditor_Mode=1",'RIDE_INIT=SERVE::'+str(
232-
self._port).strip(),'LOG_FILE=nul',"DYALOGJUPYFOLDER="+os.path.dirname(os.path.abspath(__file__)),"LOAD="+os.path.dirname(os.path.abspath(__file__))+"\\init.aplf"])
265+
self._port).strip(),'LOG_FILE_INUSE=0',"DYALOGJUPYFOLDER="+os.path.dirname(os.path.abspath(__file__)),"LOAD="+os.path.dirname(os.path.abspath(__file__))+"\\init.aplf"])
233266
else:
234267
# linux, darwin... etc
235268
dyalog_env=os.environ.copy()
236269
dyalog_env['RIDE_INIT']='SERVE:*:'+str(self._port).strip()
237270
dyalog_env['RIDE_SPAWNED']='1'
238271
dyalog_env['DYALOGQUIETUCMDBUILD']='1'
239272
dyalog_env['ENABLE_CEF']='0'
240-
dyalog_env['LOG_FILE']='/dev/null'
273+
dyalog_env['LOG_FILE_INUSE']='0'
241274
dyalog_env['DYALOG_LINEEDITOR_MODE']='1'
242275
dyalog_env['DYALOGJUPYFOLDER']=os.path.dirname(os.path.abspath(__file__))
243276
ifshutil.which('mapl'):
@@ -498,14 +531,5 @@ def define_function(self, lines):
498531

499532
defdo_shutdown(self,restart):
500533
# shutdown Dyalog executable only if Jupyter kernel has started it.
501-
502-
ifDYALOG_HOST=='127.0.0.1':
503-
ifself.connected:
504-
self.ride_send(["Exit", {"code":0}])
505-
# time.sleep(2)
506-
# if self.dyalog_subprocess:
507-
# self.dyalog_subprocess.kill()
508-
509-
self.dyalogTCP.close()
510-
self.connected=False
511-
return {'status':'ok','restart':restart}
534+
self.cleanup_dyalog()
535+
return {'status':'ok','restart':restart}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp