Common lisp Clozure CL中长期运行应用程序的应用程序交付
应用程序交付的所有基本示例都说明了如何用自己的函数替换顶级函数。完成该功能后,应用程序将退出。我想知道为长期运行的应用程序创建顶级函数的最佳方法是什么。我的代码是Common lisp Clozure CL中长期运行应用程序的应用程序交付,common-lisp,ccl,toplevel,Common Lisp,Ccl,Toplevel,应用程序交付的所有基本示例都说明了如何用自己的函数替换顶级函数。完成该功能后,应用程序将退出。我想知道为长期运行的应用程序创建顶级函数的最佳方法是什么。我的代码是 (ql:quickload :my-app) (defun main () (swank:create-server :dont-close t) (my-app:start-server) ; Essentially creates a hunchentoot handler and returns (loop for
(ql:quickload :my-app)
(defun main ()
(swank:create-server :dont-close t)
(my-app:start-server) ; Essentially creates a hunchentoot handler and returns
(loop for x = (read-line)
when (string= x "q") do (quit)
do (format t "Type q to quit~%" x)))
(save-application "my-app" :toplevel-function #'main :prepend-kernel t)
有更好的办法吗?我不喜欢循环,但是释放终端的东西也可以。正如您所说,一旦主功能完成,应用程序就会退出。因此,您需要保持函数运行,直到您希望退出应用程序 最简单的解决方案是将主循环留在
睡眠的无限循环中
:
(defun main ()
(swank:create-server :dont-close t)
(my-app:start-server)
(loop (sleep 60)))
在启动Swank服务器时,您可能希望包括通过SLIME连接干净地退出应用程序的功能。例如,您可以使用bt信号量
包编写如下内容:
(defvar *quit-my-app* (bt-semaphore:make-semamphore))
(defun main ()
(swank:create-server :dont-close t)
(my-app:start-server)
(bt-semaphore:wait-on-semaphore *quit-my-app*)
(my-app:clean-up)) ; or whatever you need to do for cleaning up
(defun quit-my-app ()
(bt-semaphore:signal-semaphore *quit-my-app*))
现在,您只需在SLIME连接上评估(退出我的应用程序)
,即可关闭应用程序
您还可以使用主线程进行维护。在我的服务器中,我在那里执行简单的日志旋转:
(defun seconds-until-tomorrow ()
(multiple-value-bind (second minute hour day month year daylight-p zone)
(decode-universal-time (+ (get-universal-time) (* 60 60 26))) ; safely tomorrow
(declare (ignore second minute hour daylight-p))
(- (encode-universal-time 0 0 0 day month year zone)
(get-universal-time))))
(defun main ()
(swank:create-server :dont-close t)
(let (cur-logfile
cur-logfile-name
;; assuming that start-server returns the Hunchentoot acceptor
(acpt (my-app:start-server)))
(loop
(let* ((lf-stem (log-file-name))
(logfile-name (merge-pathnames lf-stem *temp-path*))
(new-logfile (open logfile-name :direction :output
:if-exists :append
:if-does-not-exist :create)))
(setf (hunchentoot:acceptor-message-log-destination acpt) new-logfile
(hunchentoot:acceptor-access-log-destination acpt) new-logfile)
(when cur-logfile
(close cur-logfile)
(run-program "/usr/bin/xz" (list (princ-to-string cur-logfile-name))))
(setf cur-logfile new-logfile
cur-logfile-name logfile-name)
(when (bt-semaphore:wait-on-semaphore *quit-my-app* (seconds-until-tomorrow))
(return)))))
释放终端必然是特定于平台(OS)的。您在哪个平台上运行应用程序?此外,请澄清您对“更好的方式”的定义。在你看来,最佳的主要功能是什么?我想,“更好的方式”是主观的。我真正的意思是惯用/最佳实践,甚至是改进代码的建议。是否需要有一个执行阻塞读取行的循环,或者是否有其他方法不退出?我正在OSX上开发,不确定终端是否空闲。